Introduction to Quarto

Dr Nicola Rennie

Welcome!

Who am I?

Lecturer in Health Data Science within Centre for Health Informatics, Computing, and Statistics.


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


Using R for over 10 years, and author of multiple R packages.

website logo

What to expect during this workshop

  • Combines slides, live coding examples, and exercises for you to participate in.

  • Ask questions throughout!

What to expect during this workshop


I hope you end up with more questions than answers after this workshop!

Workshop resources

Course website: nrennie.rbind.io/training-intro-to-quarto

Screenshot of course website

What do you currently use?

How do you write your journal articles?

  • Microsoft Word?
  • LaTeX?
  • R Markdown?

How do you currently perform analysis?

  • Excel?
  • SPSS?
  • R?
  • Python?

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,…

Why use Quarto? Why use R?

  • More journals require code to be submitted (for transparency and reproducibility). Keeping the code with the paper makes this easier.

  • Copying and pasting is tedious (and a great source of accidental errors).

  • If you fix an error in code or data, the results and figures in the paper update automatically.

  • Easy to share publicly.

  • Open source so anyone can use it.

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

Your first Quarto document

Creating a document

Gif of creating a new quarto document

Quarto in RStudio

Source editor

Visual editor

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

Live Demo!

Exercise 1

  • Create a new Quarto HTML document.
  • Fill in the YAML options with a title and author.
  • Render your document.
  • Change the format to docx.
  • Re-render your document.
  • What about PDF?
05:00

Document content

Document content

In Quarto, you can include many things that aren’t code:

  • Text
  • Sections
  • Lists
  • Images
  • Tables
  • Videos
  • Equations
  • Citations
  • Raw HTML/LaTeX code

These are primarily written using Markdown syntax.

Markdown syntax

  • You don’t need special software to read it.

  • Separates out the content and structure from the document styling.

  • Focus on the content not the way it looks.

Sections

Sections are added to a document using different numbers of #:

  • # creates a Level 1 heading (section)

  • ## creates a Level 2 heading (subsection)

  • ### creates a Level 3 heading (sub-subsection)

Lists

Lists can be created using either * or -:

* Item 1
* Item 2
* Item 3

or

- Item 1
- Item 2
- Item 3

gives:

  • Item 1
  • Item 2
  • Item 3

Numbered lists

Works but isn’t great:

1. Item 1
2. Item 2
3. Item 3


Much nicer:

1. Item 1
1. Item 2
1. Item 3

gives:

  1. Item 1
  2. Item 2
  3. Item 3

Including image files

Add an image using ![](path/image.png).

Tip

  • Use relative file paths rather than absolute file paths - other people won’t share the same absolute file path as you!

  • File paths are relative to where the Quarto document is!

  • Use the tab key to show files.

Image options

Add a figure caption in the square brackets:

![Figure caption goes here](path/image.png).


Image options can be added in curly brackets afterwards:

![Figure caption goes here](path/image.png){fig-align="center" width="50%"}.

Tables

You can create simple tables with Markdown syntax:

| Surname | Forename |
|---------|----------|
| Rennie  | Nicola   |
| Smith   | Jane     |
| Barclay | Rebekah  |
Surname Forename
Rennie Nicola
Smith Jane
Barclay Rebekah


I almost never make tables by writing out - and |

Tables

  • You can create nice-looking tables from data files e.g. .csv, .xlsx. We’ll look at this in the next section.

  • The Visual Editor is much easier for writing tables.

Tables

Table –> Insert Tables:

Tables

Writing equations with LaTeX

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

\begin{equation}
Y = \beta_0 + \beta_1 \text{age} + \beta_2 \text{education}
\end{equation}

\[\begin{equation} Y = \beta_0 + \beta_1 \text{age} + \beta_2 \text{education} \end{equation}\]

Live Demo!

Exercise 2

  • Create level 1 and level 2 section headings in your document.
  • Add some text into your document. If you don’t want to write your own, there is some sample text in sample-text.qmd.
  • Format the text to include a list, and bold or italic text.
  • Add a link to the International Personality Item Pool: https://ipip.ori.org/.
  • Include an image in your document. Find your own or use eggs.jpg.
  • Add a caption to the image and center align it.
  • Render your document - check the image appears and the link works.
05:00

Code blocks

Code blocks

  • Code blocks are the main way of including executable R code in a document.

  • Code blocks always start with three backticks, followed by the an r in curly brackets.

```{r}
2 + 2
```
[1] 4

Tip

Add a new code block using the Ctrl + Shift + I keyboard shortcut.

Some Quarto code tips

  • Remember paths are relative - this includes data files!
  • You can run code blocks interactively, one at a time. These execute in your Global environment.
  • When you render a document, it runs in it’s own environment. Objects in the Global environment cannot be accessed.

Code block options

Code block options can control:

  • whether your code is executed
  • whether your code is displayed
  • if warnings and errors are displayed
  • how figures are presented
  • how tables are presented

Code block options

```{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
```

Predictive code block options (and YAML)

Gif of predictive yaml in a quarto document

Global code block options

Quarto

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

R Markdown

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

Code block options: label

  • You can add a label to each code block.
  • They are searchable and help to find where errors exist (compared to unnumbered code block 39).
  • You can’t have duplicate labels.
  • Labels are also useful for other things (as we’ll see this afternoon).

Code block options: label

Let’s say we want to load some data. We can add a load-data label:

```{r}
#| label: load-data
library(psych)
data(bfi)
```


Tip

Separate label words using a - rather than a _.

Data

We’ll be using the sat.act data from {psych} for some examples. You can also load the data directly from a CSV file:

```{r}
#| label: load-data-csv
sat.act <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/psych/sat.act.csv")
```

See ?psych::sat.act.

Code block options: fig-

Figure options:

```{r}
#| label: age-hist
#| fig-align: center
#| fig-width: 6
#| fig-asp: 0.9
#| fig-cap: "A histogram of participant age"
#| output-location: slide
hist(sat.act$age, xlab = "Age", main = "")
```

Code block options: fig-

A histogram of participant age

Code block options: tbl-

There are lots of options for creating tables in R:

  • {knitr} - basic
  • {kableExtra} - adds to kable() in {knitr}
  • {gt} - more complex “great tables”
  • {gtsummary} - creates summary tables using {gt}
  • {flextable} - good for Word documents
  • {tinytable} - very lightweight

Code block options: tbl-

Prepare the data:

```{r}
data("sat.act")
sat_summary <- aggregate(
  cbind(SATV, SATQ) ~ education,
  FUN = mean,
  data = sat.act
)
```

Code block options: tbl-

Make a table:

```{r}
#| tbl-cap: "Mean scores by education level"
#| output-location: slide
library(tinytable)
tt(sat_summary)
```

Code block options: tbl-

Mean scores by education level

education SATV SATQ
0 618.0536 620.4286
1 600.3488 607.2558
2 575.4651 577.2093
3 611.6245 606.0743
4 615.9781 611.8540
5 622.9281 623.6331

Live Demo!

Exercise 3

  • Add a code block that loads the {psych} package and a dataset from the package e.g. bfi.
    • Alternatively, use some other data that you have/like.
  • Add another code block that makes a plot e.g. a bar chart of participants’ education categories, education. You can use either base R or {ggplot2} (or another package!).
  • Change the code block options and see what happens!
  • Add a third code block that creates a table showing the first 10 rows and first 5 columns of your data. Hint: head(bfi[, 1:5], 10). Use any table package you prefer.
05:00

Document referencing

Referencing results in text

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

This is especially useful for using results values in a paragraph of text.

The total number of participants is `r nrow(bfi)`.

The total number of participants is 2800.

Referencing results in text

For more complex numbers, you can calculate or extract results from a code block.

```{r}
# Calculate median age of females
# Males = 1, Females = 2
age_f <- median(bfi[bfi$gender == 2, "age"])
```


The median age of females in the study is `r age_f`.

The median age of females in the study is 26.

Figure references

Earlier we saw how to add images to Quarto documents, where image options can be added in curly brackets afterwards:

![Figure caption goes here](path/image.png){fig-align="center" width="50%"}.


Add a figure label with a #:

![Figure caption goes here](path/image.png){#fig-my-image fig-align="center" width="50%"}.

This label must start with fig- to be a Figure label.

Figure references

To reference the figure in the text, use an @ with the figure label:

@fig-my-image.


Tip

This is basically the pattern of cross-referencing throughout Quarto:

  • # give a reference label to an object.

  • @ refer the object’s reference in the text.

Cross-referencing

We can add a cross-reference to a different section of our manuscript by adding a reference label that has a sec- prefix:

## Exploratory data analysis {#sec-eda}


Then we can make a reference to this section using:

… as discussed in @sec-eda.

This automatically adds the word Section before the relevant number.

Cross references

To make sure that the cross reference numbers make sense, you should also switch on the numbering for the document sections:

---
title: "My Document"
number-sections: true
---

Plot references

Earlier we made a histogram of age:

```{r}
#| label: age-histogram
#| fig-align: center
#| fig-cap: "A histogram of participant age"
#| output-location: column
hist(bfi$age, xlab = "Age", main = "")
```

A histogram of participant age

Plot references

To make this a Figure reference, we need to prefix the label with fig-:

```{r}
#| label: fig-age-histogram
#| fig-align: center
#| fig-cap: "A histogram of participant age"
#| output-location: column
hist(bfi$age, xlab = "Age", main = "")
```
Figure 1: A histogram of participant age

Plot references

Then:

A histogram of participant ages is shown in @fig-age-histogram.


gives:

A histogram of participant ages is shown in Figure 1.

Table references

Table references are similar, except we prefix with tbl-:

```{r}
#| label: tbl-sat-summ
#| tbl-cap: "Mean scores by education level"
#| output-location: column
tt(sat_summary)
```
Table 1: Mean scores by education level
education SATV SATQ
0 618.0536 620.4286
1 600.3488 607.2558
2 575.4651 577.2093
3 611.6245 606.0743
4 615.9781 611.8540
5 622.9281 623.6331

Table references

Then:

Summary statistics are shown in @tbl-sat-summ.


gives:

Summary statistics are shown in Table 1.

Citations

To add citations in Quarto, we need two things:

  • A source bibliography with citation IDs

  • A way to refer citations IDs in Markdown syntax

Bibliography files

  • Quarto supports bibliographies in a wide variety of formats including BibTeX and CSL

  • .bib files are probably most common.

  • The first entry inside the curly brackets is the citation ID.

@Manual{psych,
  title = {psych: Procedures for Psychological, Psychometric, and Personality Research},
  author = {{William Revelle}},
  organization = {Northwestern University},
  address = {Evanston, Illinois},
  year = {2024},
  note = {R package version 2.4.1},
  url = {https://CRAN.R-project.org/package=psych},
}

Tip

Find a citation for the R packages you using by running citation("pkgname") e.g. citation("psych")

Bibliography files

  • Add a bibliography to your document using bibliography in the document YAML.

  • You might also choose to link the citations to the reference list.


---
title: "Some long interesting title"
bibliography: references.bib
link-citations: true
---

Citations

To refer to a citation, use @ and the citation ID:

… as described in @psych.

which gives:

… as described in @psych.


Or inside brackets:

…as seen in the bfi data [@psych].

which gives:

…as seen in the bfi data [@psych].

Visual editor

Adding citations is one task where the Visual Editor in RStudio can be really helpful. It provides:

  • Integration with Zotero (no set up if using desktop versions).

  • Search with DOI, CrossRef, PubMed.

  • It updates the .bib file for you!

Visual editor

Click Insert –> Citation:

Live Demo!

Exercise 4

  • Update (or add!) a label to the figure code block you created earlier.
  • Add a reference to this in the text.
  • Add section references, and cross reference to this section somewhere else in the document.
  • Add a citation by either:
    • creating a .bib file and listing it in the YAML.
    • using the Zotero integration
05:00

Formatting documents

Formatting documents

Let’s assume that we’ve written a wonderful, fully-reproducible manuscript and we’re ready to submit it to our favourite journal.

Now comes the journal formatting…

Output formats

Most (but not all)journals aren’t quite ready for Quarto (.qmd) files. They accept Word documents, LaTeX files, and sometimes PDFs to start with.

Word documents

---
format: docx
---

LaTeX

---
format:
  pdf:
    keep-tex: true
---

Formatting documents

How can you alter the styling of a Quarto document?

Word documents

---
format:
  docx:
    reference-doc: apa-style.docx
---

Formatting documents

LaTeX

---
format:
  pdf:
    keep-tex: true
    template: apatemplate.tex
---

The template.tex file might be provided by a journal.

But there’s an easier way…

Quarto extensions

Quarto extensions are a powerful way to modify and extend the behavior of Quarto. Extensions can be used to add styling to your documents.


Extensions aren’t just used to change document styling. There are also extensions to:

  • Embed shinylive applications in a Quarto document
  • Embed HTML forms in documents
  • Use Font Awesome icons in HTML and PDF outputs

Journal template extensions

Journal template extensions

Submitting to the JSS:

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

Submitting to an Elsevier journal:

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

Installing Quarto extensions

  • Quarto extensions can be shared in different ways.
  • The most common way is via GitHub. This is where you’ll find the journal templates we looked at.
  • Two options for installing from GitHub:
    • From the command line
    • Using the {quarto} R package
  • Both ways download all the style files you need and put them in the right place.

Installing Quarto extensions

From the command line

To create a new document from a template:

quarto use template username/repository


To convert an existing document

quarto add username/repository

Installing Quarto extensions

Using the {quarto} R package

Version 1.4 onwards

To create a new document from a template:

quarto::quarto_use_template("username/repository")


To convert an existing document

quarto::quarto_add_extension("username/repository")

APA style manuscripts

APA Quarto extension: wjschne.github.io/apaquarto

Install: quarto::quarto_use_template("wjschne/apaquarto")

Live Demo!

Exercise 5

  • Install the APA Quarto extension.
  • Change the output format of your document to APA (docx, html, or pdf).
  • Re-render your document. Do you get an error?
  • Look at the installation instructions at wjschne.github.io/apaquarto/installation.html to see what other YAML options are added.
  • Add the required information about the author (you).
  • Re-render your document.
05:00

Using Quarto in the real world

Collaborating on Quarto documents

We often work with other people when we write papers.

How do we do that effectively using Quarto?

Caution

I don’t have perfect answers to this…

Structuring Quarto documents

  • Your .qmd files will get excessively long when you start writing (especially when you include code).

  • This quickly gets annoying (especially with multiple people adding to a single document).

  • There are several options for breaking them down:

    • multiple files
    • multiple files in a project

Multiple Quarto files

You can use {{< include doc.qmd >}} to include one Quarto document in another.

---
title: "My manuscript"
---

## Introduction

{{< include intro.qmd >}}

## Exploratory data analysis

{{< include analysis.qmd >}}

Quarto projects: Manuscript

  • Quarto projects are a collection of Quarto files structured in a specific way that provide:

    • A way to render all or some of the files in a directory.
    • A way to share YAML configuration across multiple documents.
    • The ability to freeze rendered output.
  • Manuscripts are a special type of Quarto project.

  • Has configuration file _quarto.yml that identifies the project as a Quarto manuscript and controls how your manuscript is put together.

Multiple authors: GitHub

  • A place to store and manage versions of code.

  • Public or private repositories

  • Alternatives like GitLab and Bitbucket exist.

Multiple authors: GitHub

Why use GitHub?

  • Easily roll back to previous versions.

  • History of who added what.

  • Add comments and suggestions via pull requests. Ask for a review.

  • Can render your documents for you.

Multiple authors: {trackdown}

See claudiozandonella.github.io/trackdown.

Multiple authors: it’s a known problem

GitHub discussion: github.com/quarto-dev/quarto-cli/discussions/405

Tricky things: managing versions

When collaborating with other people, it’s useful (sometimes crucial) that you all have:

  • the same R package versions ({renv} can help with this)
  • the same Quarto versions (harder to manage)
  • the same LaTeX versions (harder to manage)

Tricky things: long computations

Problem: you have R code that takes a long time to run, and you don’t want to re-run it every time you re-render the document.

Solutions:

  • cache: true: store the results of cell executions so they aren’t re-executed with every document render.

  • freeze: true: in project, state that computational documents should never be re-rendered during a global project render, or alternatively only be re-rendered when their source file changes.

Tricky things: long computations

Problem: you have R code that takes a long time to run, and you don’t want to re-run it every time you re-render the document.

Solutions:

  • {targets}: a Make-like pipeline tool for statistics and data science in R
    • skips costly runtime for tasks that are already up to date
    • run computations upstream before the .qmd document
    • the .qmd is the final “target”

Nice features: shortcodes

  • We’ve already seen the include shortcode.

  • There are other shortcodes to make your life easier.

  • Adding {{< pagebreak >}} adds a page break in your document (as the name suggests).

  • This works across multiple output formats.

Nice features: diagrams

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

Nice features: 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

Examples

Open discussion

  • Would you use Quarto to write a manuscript?

  • What things seem difficult or less nice than what you currently use?

  • Is there anything that didn’t work the way you expected?

  • Questions?

Workshop resources

Course website: nrennie.rbind.io/training-intro-to-quarto

Screenshot of course website

Additional resources