Quarto: The Life-Changing Magic of Tidying Up (Your Work with Notebooks)

Intro

Two Principles

  1. Our work should be accurate and reproducible1.
  1. We can’t prove accuracy without reproducibility.

Reproducibility should be the minimum standard for all computational social sciences.

Two Implications

  1. We should adopt code-first data analysis.
  2. We should organize our work in notebooks and/or packages.

This is More Work!

This is More Work!

  1. These tools are the best way to create accurate and reproducible.
  2. There are many positive side effects
    • Easy to share
    • Scalable
  3. Cool stuff!

Challenges

1. Insufficient Documentation

A collaborator (probably you from six months ago) skipped some key details in an analysis because in-line documentation is a pain.

The Why, For Humans

We want to capture the why in a way that is written for humans instead of computers.

1. .qmd

Quarto relies on .qmd files (or Jupyter notebooks) that combine

  1. Formatted text
  2. Code
  3. Code output

2. Copying-and-pasting

Copying-and-pasting is a weak link in research workflows.

  1. Transposition errors
  2. 🔥🔥🔥Updating an analysis because of reviewer #2🔥🔥🔥

2. One Source of Truth

Code
library(tidyverse)

cars_slr <- lm(dist ~ speed, data = cars)

ggplot(data = cars, aes(x = speed, y = dist)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Driving Fast is Dangerous") +
  theme_minimal()

On average, the stopping distance of cars increases by 3.93 feet for each addition mile per hour of driving speed.

3. Out-of-order

It is too easy to run steps out-of-order or to accidentally skip code run in interactive mode.

3. Rendering

  1. Quarto starts a brand new R session and runs all code in order from scratch.
  2. Quarto combines the executed code and markdown into an output document using pandoc.

4. Iteration

Sometimes we want to create the same artifact for many geographies, many time periods, and many anything else.

quarto_render(
  input = template_name,
  output_file = output_file,
  execute_params = execute_params
)

4. Many times

Quarto Basics

Opening a .qmd file

  1. Click “File > New File > Quarto Document…”
  2. Click create
  3. Save the document in your project directory

1. YAML Header

YAML headers contain meta information about a Quarto document. The YAML header starts with --- and ends with ---.

---
format: pdf
---

1. YAML Header

---
title: "Quarto: The Life-Changing Magic of Tidying Up (Your Work with Notebooks)"
format: 
  revealjs:
    embed-resources: true
---
---
format:
  html:
    embed-resources: true
    code-fold: true
    toc: true
---

2. Markdown text

Markdown is a shortcut for HyperText Markup Language (HTML). Essentially, simple meta characters corresponding to formatting are added to plain text.

Titles and subtitltes
------------------------------------------------------------

# Title 1

## Title 2

### Title 3

Text formatting 
------------------------------------------------------------

*italic*  

**bold**   

`code`

Lists
------------------------------------------------------------

* Bulleted list item 1
* Item 2
  * Item 2a
  * Item 2b

1. Item 1
2. Item 2

Links and images
------------------------------------------------------------

[text](http://link.com)

3. Code chunks

More frequently, code is added in code chunks:

```{r}
#| code-fold: false

2 + 2
```
[1] 4

Running code and rendering

Can also run R code in RStudio in interactive mode. This is essential for developing analyses.

Differences from .Rmd

Self-contained documents

Quarto does not embed all resources (images, CSS, etc.) into rendered .html documents by default. This is a change from .Rmd! Be sure to include the following in your YAML header!

---
format: 
  html:
    embed-resources: true
---

YAML parameters

TRUE and FALSE don’t work as parameters in Quarto YAML headers but you can use "yes" and "no".

source

Autocomplete and Validation

🙌GOOD NEWS!🙌

Quarto autocompletes and validates contents in the YAML header.

Cool Stuff!

Tables of Contents and Code Folding

---
format:
  html:
    embed-resources: true
    code-fold: true
    toc: true
---
Code
library(tidyverse)

cars %>%
  ggplot(aes(x = speed, y = dist)) +
  geom_point(alpha = 0.3) +
  labs(title = "Cars Traveling at Higher Speeds Take Longer to Stop") +
  theme_minimal()

YAML options

---
title: "Demonstrate the Urban Institute quarto Theme"
subtitle: "This is an example subtitle"
author-title: "Authors"
authors: "Aaron R. Williams and Erika Tyagi"
affiliation: "Urban Institute"
date: today
abstract-title: "Quarto at Urban"
abstract: "This document contains examples for how to use leverage many of Quarto's capabilities at the Urban Institute."
---

Tabsets

Code
2 + 2
[1] 4
Code
3 + 3
[1] 6

Tabsets

::: {.panel-tabset}

## Attempt 1

```{r}
2 + 2
```

## Attempt 2

```{r}
3 + 3
```

:::

More cool stuff!

Let’s pivot to the Urban Institue Quarto example.

Resources

  • https://quarto.org/
  • https://r4ds.hadley.nz/quarto.html
  • https://github.com/UI-Research/urbnquarto