2  Reproducibility with Quarto

A schematic representing the multi-language input (e.g. Python, R, Observable, Julia) and multi-format output (e.g. PDF, html, Word documents, and more) versatility of Quarto.

Quarto is a versatile publishing system that allows users to create high-quality documents, reports, websites (like this one!) and presentations using multiple programming languages like R, Python, and Julia. It combines the functionalities of various tools into a single platform, making it easier to produce reproducible and collaborative scientific content. If you have heard of R Markdown - Quarto is the “next generation” of R Markdown.

🎓 Learning Objectives

After completing this lesson learners will be able to:

  • Write code and text in a Quarto document
  • Identify components of a Quarto document
  • Compile Quarto documents into both HTML and PDF formats
  • Understand the difference between running code interactively, and rendering an entire new document.

While Quarto is also it’s own standalone program, it comes “built-in” to R Studio so no additional installations needed!

2.1 Document structure

Quarto is an example of literate programming where the explanation of the program (or analysis) logic is presented in a natural language (such as English), with supporting pieces of code embedded in the document itself. Quarto combines normal text such as this sentence, code and the output from the code all in one file. It is similar to a scientific lab notebook where a scientist can write down their hypotheses, code to produce graphs and analysis results, and the discussion of those findings.

A Quarto document with the extension .qmd (on the left), along with its rendered version as HTML (on the right). Credit: Quarto documentation.
👉 Open the Quarto tutorial file
  1. Make sure you are in your Math 130 Project. (ref Section 1.7)
  2. From the file pane, open the _hello.qmd quarto document
  3. Click “Render” at the top of this file.

The Render button in the RStudio IDE compiles or renders the file and opens a preview of the output with a single click or keyboard shortcut SHIFT+COMMAND+K.

The Render button

2.2 Components of a Quarto file

Quarto files contains three types of content:

  1. A YAML header
  2. Code chunks
  3. Markdown text

2.2.1 YAML Header

An YAML header is demarcated by three dashes (—) on top and bottom. R is pretty finicky about these three dashes, so try not to disturb them.

When rendered, the title, "Hello, Quarto", will appear at the top of the rendered document with a larger font size than the rest of the document. The other two YAML fields denote that the output should be in html format and the document should open in the visual editor by default.

The basic syntax of YAML uses key-value pairs in the format key: value. Other YAML fields commonly found in headers of documents include metadata like author: and date:.

👉 Add your name
  1. Add a line after the title for an author: and put your name here.
  2. Click the “Source” button at the top left corner of the Source window.
  3. Render the document again.

Visual vs Source mode RStudio’s Visual editor offers a WYSIWYM authoring experience for markdown formatting of text. Each view has it’s benefits and drawbacks, and which you choose to use is purely a matter of personal preference.

WYSIWYM: What You See Is What You Mean

The same Quarto document in the two modes of the RStudio editor: visual (on the left) and source (on the right).

2.2.2 Code Chunks

Code chunks start with three back ticks ``` and an {r} in braces. Chunks close (end) with another three back ticks.

a back tick is the key to the left of the 1 on your keyboardNote the background color of this section has changed to a different shade. This helps you identify you have closed your code chunk properly.
```{r}
#| eval: false
2+2
```
  • You can insert code chunks by using the button in the top right of an RMD file (Insert -> R), or by typing CTRL+ALT+I.
  • R code chunks can also have a YAML style header, prefixed by a “hash-pipe” #| at the top of the code chunk. In this case I set eval to false to prevent the code inside this code chunk from being run when I render the whole document.
| is the first of three pipes we’ll see in this course
Only code goes in code chunks

That’s why they’re called code chunks. No normal text. All explanatory text goes outside code chunks in normal Markdown text.

👉 Make a change

Play around with modifying the text and the R code.

  1. Change echo:false to echo:true in the plot-penguins code chunk header.
  2. Type colors() in your console to pull up all the named colors in R. Change at least one color in the penguin plot. You can find them in the code line values = c("darkorange","purple","cyan4")
  3. Write “Hello Penguins!” on line 45, at least one blank line after the the end of the code chunk and your sentence.
  4. Render this document and observe your changes.

In addition to rendering the complete document to view the results of code chunks, you can also run each code chunk interactively in the RStudio editor by clicking the play button icon in the top right corner of the code chunk, or keyboard shortcut CTRL + SHIFT + ENTER. RStudio executes the code and displays the results either inline within your file or in the Console, depending on your preference.

👉 Add some code
  1. Insert a new code chunk after your “Hello Penguins!” sentence. Make sure there is a blank line between your sentence and the code chunk
  2. Type the code 2+2 in this code chunk and click the play button to execute it immediately.
  3. Render your document and observe your changes.

2.2.3 Markdown Text

Markdown is a popular markup language that allows you to add formatting elements to text. Markdown is useful because it is lightweight, flexible, and platform independent. [See more basic syntax of Markdown at https://quarto.org/docs/authoring/markdown-basics.html]{.aside}

What you type What you see
**bold** bold
_italics_ italics
`code` code
$2\beta - \frac{\mu}{n}$ \(2\beta - \frac{\mu}{n}\)
What you type What you see
1.  one
2.  two
  1. one
  2. two
*  bullet
*  points
  • bullet
  • points

Additionally, using # and ## symbols at the start of a line creates first (#) and second (##) level headers (larger font size, bold, numbered), such as Section 2.2 and line 15 in the hello.qmd file where it says ## Meet Quarto . Properly formatted headers are an important part of document organization and accessibility for screen reader navigation.

👉 Add some formatting
  1. Add some formatting to your “Hello Penguins!” sentence.
  2. Render your document and observe your changes.

2.3 Create professional looking PDF reports.

To convert your work into a professional looking PDF, or to write math symbols in your homework, or you need a typesetting program called \(\LaTeX\) (pronounced “lay-tek” or “lah-tex”). It’s a super neat program, but also nearly 4Gb. Too big for our needs. That’s where the tinytex package came from. we’re going to use it to install a lightweight version of \(\LaTeX\). This will let us write math equations in Markdown/Quarto, and create nice PDF’s of your work.

👉 Your Turn
  1. In the console type tinytex::install_tinytex() and press Enter. This may take a few minutes to fully install.
  2. In the YAML header change format: html to format: pdf
  3. Render your document again.