Task: Reproducible documents with Quarto

← Back to session page

Find the solution here after the session ends.

In this task, you will create a report on the penguins dataset using Quarto.

Tip

Render your document often to check if everything still works. It’s much easier to find mistakes if you render after every small change. Use the keyboard shortcut Ctrl + Shift + K (Cmd + Shift + K on Mac) to render quickly.

1. Create a Quarto document

  • Go to File -> New File -> Quarto Document...
  • Use “Penguin Report” as title and choose the editor you prefer
  • Render the document. RStudio asks you to save it first, e.g. as penguin_report.qmd
  • Switch between the visual and the source editor to see the difference

2. Add Markdown text

Delete everything below the YAML header. Then write some words about the penguins dataset trying out some Markdown formatting:

  • A section header (e.g. ## Introduction)
  • A sentence with bold and italic text
  • A bullet list of the three penguin species
  • A link to a website of your choice

Render the document.

3. Add code

  • Add a code chunk that loads the tidyverse. Hide it completely with #| include: false
  • Add another code chunk with a plot of the penguins data (e.g. a scatterplot or a boxplot). Hide the code with #| echo: false and the warnings with #| warning: false

Render the document. What is the difference between include: false and echo: false?

4. Add inline code

Write a sentence that includes the number of penguins in the data. Use inline code like this: `r nrow(penguins)`

Render the document.

5. Extend the YAML header

Add an author and a date to the YAML header. Also add execute options to hide warnings and messages in all code chunks:

---
title: "Penguin Report"
author: "Your Name"
date: today
format: html
execute:
  warning: false
  message: false
---

If your YAML header contains editor: visual, keep this line.

Render the document.

For the fast ones

You can do these in any order, or skip them and just take a break.

Explore the visual editor

Try the buttons and the Insert menu of the visual editor. Add for example an image, a table or a footnote.

More markdown

Check the Markdown reference and add anything you like to your document.

More code

Add a code chunk that prints a summary table, e.g. the mean body mass of each species:

penguins |>
  summarize(mean_body_mass = mean(body_mass, na.rm = TRUE), .by = species)

Try more chunk options for your plot, e.g. #| fig-cap: "Your caption", #| fig-align: center or #| out-width: "80%". You can find all options in the code cell reference.

More YAML options

Add more options to the YAML header, for example:

---
title: "Penguin Report"
format: html
toc: true
number-sections: true
code-fold: true
---

You can find more options in the HTML format reference.

Render to PDF or Word

Change format: html to format: pdf or format: docx and render.

For PDF, you may need to install LaTeX first. Run this in the R console:

# install.packages("tinytex") # only run this once
tinytex::install_tinytex() # only run this once