Introduction to reproducible reporting with Quarto

R-Ladies Edinburgh
11 December 2023

Nicola Rennie

About me

Lecturer in Health Data Science at Lancaster University.


Academic background in statistics, and experience in data science consultancy.


Organiser of R-Ladies Lancaster.

photo of lancaster castle and canal

What is Quarto?

What is Quarto?

Quarto is an open-source scientific and technical publishing system that allows you to combine text, images, code, plots, and tables in a fully-reproducible document. Quarto has support for multiple languages including R, Python, Julia, and Observable. It also works for a range of output formats such as PDFs, HTML documents, websites, presentations,…


That sounds a bit like R Markdown!

What about R Markdown?

R Markdown isn’t going anywhere but…

  • Quarto has better multi-language support

  • More user-friendly

  • Better control of the output layouts

Simple documents

Creating a document

Gif of creating a new quarto document

Rendering a document

Within RStudio IDE: click Render (or Ctrl+Shift+K)


Using {quarto}

library(quarto)
quarto_render("document.qmd")


Using the command line

quarto render document.qmd

What makes a Quarto document?

YAML header

---
title: "A very cool title"
format: html
---

Content

  • Text, links, images

  • Code, tables, plots

  • Equations, references

Output types

  • Documents: HTML, PDF, MS Word, Markdown

  • Presentations: Revealjs, PowerPoint, Beamer

  • Websites

  • Books

Code block options

Quarto vs R Markdown

```{r}
#| eval: false
#| warning: false
2 + 2
```


Comparing to R Markdown:

```{r, warning=FALSE, eval=FALSE}
2 + 2
```


knitr::convert_chunk_header("document.qmd", output = identity)

Code block options

Code visibility options:

  • Hide the code
#| echo: false
  • Show the code
#| echo: true
  • Show the code and the YAML
#| echo: fenced

Code block options


```{r}
#| echo: fenced
#| eval: true
#| warning: false
#| error: false
#| output: asis
#| include: true
```

Code block options

Figure options:

```{r}
#| fig-alt: "Bar chart showing the number of lemurs for three different species"
#| fig-height: 7
#| fig-width: 10
#| fig-align: center
#| output-location: slide
g <- ggplot(
  data = lemurs,
  aes(x = name, y = n, fill = name)
) +
  geom_col()
g
```

Code block options

Bar chart showing the number of lemurs for three different species

Code block options

Output location:

```{r}
#| output-location: column
g
```

Code block options

Highlight code:

```{r}
#| eval: false
#| code-line-numbers: "3"
ggplot(
  data = lemurs,
  aes(x = name, y = n, fill = name)
) +
  geom_col()
```

Global code block options

RMarkdown

```{r}
knitr::opts_chunk$set(echo = FALSE)
```

Quarto

---
title: "A very cool title"
format: html
execute:
  echo: false
---

Inline code

We can also include code inline, rather than as a separate chunk.

The total number of lemurs is `r sum(lemurs$n)`.

The total number of lemurs is 21859.

These are a few of my favourite things (about Quarto)

Predictive YAML (and code block options)

Gif of predictive yaml in a quarto document

Quarto extensions (e.g. journal article templates)

Templates: github.com/quarto-journals

---
title: "A very cool title"
format:
  pdf: default
  jss-pdf:
    keep-tex: true 
---

Writing LaTeX

---
title: "A very cool title"
format: html
---

\begin{equation}
\hat{e}_i = Y_i - \hat{Y}_i
\end{equation}

\[\begin{equation} \hat{e}_i = Y_i - \hat{Y}_i \end{equation}\]

Multiple columns


::: .columns
::: {.column width = "60%"}
The content for the first column goes here.
:::

::: {.column width = "40%"}
The content for the second column goes here.
:::
:::

Code animation

g <- ggplot(data = lemurs, 
            aes(x = name, y = n, fill = name)) +
  geom_col()

Code animation

g <- ggplot(data = lemurs, 
            aes(x = name, y = n, fill = name)) +
  geom_col() +
  scale_fill_brewer(palette = "Dark2")

Diagrams

```{dot}
graph G {
  qmd -- Knitr;
  qmd -- Jupyter;
  Knitr -- md;
  Jupyter -- md;
  md -- pandoc;
  pandoc -- HTML;
  pandoc -- PDF;
  pandoc -- Word;
  pandoc -- more;
}
```

Diagrams

G qmd qmd Knitr Knitr qmd--Knitr Jupyter Jupyter qmd--Jupyter md md Knitr--md Jupyter--md pandoc pandoc md--pandoc HTML HTML pandoc--HTML PDF PDF pandoc--PDF Word Word pandoc--Word more more pandoc--more

Parameterised documents

---
title: "A very cool title"
format: html
params:
  year: 2023
---

Subset data based on parameters:

data_subset <- data |>
  dplyr::filter(
    year == params$year
  )

Parameterised documents

quarto::quarto_render(
  input = "document.qmd",
  execute_params = list("year" = 2022)
)

Map over multiple years with {purrr}:

purrr::walk(2018:2022, ~ quarto::quarto_render(
  input = "document.qmd",
  execute_params = list("year" = .x),
  output_file = glue::glue("report_{.x}.html")
))

Resources


Your turn!
Any questions?

Things to try!

  • Create a new Quarto HTML document and fill in the YAML options.

  • Insert a new R code block.

  • Load some data e.g. data("CO2") and create a plot.

  • Render your document.

  • Change the code block options.

  • Edit the YAML options to create revealjs slides instead.

  • Look at the Quarto documentation to view other YAML options for revealjs slides.

  • Bonus: create a parameterised document!