Introduction to RMarkdown and Quarto

Ambra Perugini

RMarkdown and Quarto

Why a markup language?

  • Reproducibility: allows scientific analyses to be documented and replicated.

  • Automation: generates reports, articles, and presentations without copy-pasting.

  • Flexibility: supports export to HTML, PDF, Word, and other formats.

  • Collaboration: facilitates teamwork and the sharing of results.

What is a markup language?

  • A system for structuring and formatting text using textual commands.

  • Unlike a word processor, it separates content from formatting.

  • Markdown is an example of a simple, human-readable markup language.

How does it work?

  1. Write the code: a text file containing Markdown sections and R code chunks.

  2. Run the code: generates results directly within the document.

  3. Render the document: choose the output format (HTML, PDF, Word, presentations, etc.).

Quarto vs RMarkdown: main differences

Feature Quarto R Markdown
Language dependency Language-independent; works with R, Python, Julia, and Observable JS Primarily tied to R and the knitr system
File extension .qmd .Rmd
Rendering engine Unified Quarto engine (based on Pandoc) rmarkdown + knitr + Pandoc
Multi-language support Native and seamless (multiple languages in the same document) Primarily R, with more limited Python support
Projects and websites Built-in support for projects, websites, books, and blogs Requires different packages (bookdown, blogdown, xaringan, etc.)

Quarto vs RMarkdown: main differences

Feature Quarto R Markdown
Cross-references and citations Built-in and easy to use Often require additional configuration or packages
Output formats Unified system (documents, presentations, websites, books) Different packages for different formats
Configuration style Clearer and more consistent YAML More fragmented YAML, depending on the output format
Future outlook Modern, actively developed, recommended for new projects Maintained for compatibility, with slower development

to sum up

Quarto is the modern, unified evolution of R Markdown: more powerful, more flexible, and not limited to the R programming language.

What will we do during the course?

  • Dynamic reports with automatically updated data

  • Interactive presentations with Reveal.js

  • Setting up a file for writing theses and academic papers (PDF, Word)

  • Hands-on exercise with a final presentation

Github

What is GitHub?

  • GitHub is a website where we store the files for our projects.
  • It allows us to:
    • Save different versions of files
    • Roll back if something goes wrong
    • Share and collaborate
    • Keep work synchronized across different computers

In this course, we will use GitHub to manage the files of our Quarto projects.

What is Git?

  • Git is a program that allows us to manage different versions of files.
  • We will use it from the terminal to connect our computer to GitHub.

It allows us to:

  • Download files from the online repository
  • Save changes to files
  • Upload changes to GitHub
  • Update the project when the instructor makes changes

Git is the tool that works behind GitHub.

Cloning the course repository

To get the course files onto your computer:

  1. Go to GitHub in your browser
  2. Open the course repository
  3. Click the Code button
  4. Copy the repository address (URL)

Then open the terminal and type:

git clone repository-URL

This command creates a copy of the repository on your computer.

The main workflow (always the same)

  1. Open the terminal in the project folder and check its status:
git status
  1. If your local repository is synchronized with the latest version on GitHub, you can start making your changes. Otherwise, before getting started, run the following command to download the latest version:
git pull
  1. Once you have finished making changes to the repository, stage the new and modified files::
git add .
  1. Create a commit with a message:
git commit -m "Added results section"
  1. Upload your changes to GitHub:
git push

Create your own repository:

  1. Go to Github (website)

  2. Click on New repository

  3. Choose:

3.1. Project Name (es. psychology-project)

3.2. A short description (optionsl)

  1. Choose visibility:

4.1 Public -> anyone can have access to the project

4.2 Private -> you are the only one to see it (and people you invite)

  1. Click Create repository

Next:

To connect a local folder to the repository, you can follow the same procedure you used to download the course repository:

git clone URL-of-your-repository

Setting up a project in RStudio

  1. Open RStudio and go to File -> New Project

  2. Select New Directory and then `Quarto Project``

  3. Choose the destination folder and give your project a name

  4. Click Create Project

Structure of a Quarto document (.qmd)

1. YAML

YAML: main options

The YAML header allows you to define:

  • title: document title
  • subtitle: subtitle
  • author: author(s)
  • date: date
  • output: output format
  • theme: visual theme
  • fontcolor: text color
  • fig-width: default figure width
  • code-fold: code folding (true/false)
  • toc: table of contents (true/false)
  • number-sections: section numbering

YAML: output formats

  • html
  • pdf
  • word
  • revealjs
  • pptx
  • beamer

You can specify multiple output formats in a single YAML header, allowing the document to be rendered in several formats.

2. Document body

Document body: headings and subheadings


# Main heading

## First-level subheading

### Second-level subheading

#### Third-level subheading

Document body: paragraphs and text formatting


This is an **example paragraph** with bold text.

You can also use *italics* or `monospaced text`.


This is an example paragraph with bold text.

You can also use italics or monospaced text.

Document body: bulleted and numbered lists


- Item 1

- Item 2

- Item 3

- Item 4


  • Item 1

  • Item 2

  • Item 3

  • Elemento 4

3. Code chunks

Code chunks are used to insert code and run analyses:

  • warning: if false, warnings from the model and/or function are not displayed.
  • include: determines whether the output is displayed in the report or document.
  • eval: if true, the code is executed; if false, it is not executed at all.
  • echo: if false, the code is hidden; if true, the code used is displayed.

3. Code chunks

```r

#| warning: false

#| include: false

#| eval: false

#| echo: false

summary(cars)

```

To set chunk options in Quarto, using YAML-style syntax, add #| at the beginning of a line inside the chunk.

Inserting equations

  • Equations on a separate line: $$…$$
  • Inline equations $…$

$$ Y = \beta_{0} + \beta_{1} X $$

\[ Y = \beta_{0} + \beta_{1} X\]

Execution and rendering

  1. To insert code chunks: Ctrl + Alt + I (Windows) or Cmd + Option + I (Mac)

  2. To run code chunks: use Ctrl + Enter (Windows) or Cmd + Enter (Mac)

  3. To generate the final document, click Render (Quarto) or Knit (R Markdown)

Practical applications

  • Dynamic reports with automatically updated data
  • Interactive scientific presentations with Reveal.js
  • Writing theses and academic papers in academic formats (PDF, Word, LaTeX)
  • Interactive dashboards and documentation for research

Conclusion

  • R Markdown and Quarto simplify scientific communication.
  • Powerful tools for creating clear, reproducible, and well-formatted documents.
  • Essential for transparent and collaborative research.