[1] 4
Reproducible documents with Quarto
April 14, 2027
Draws on slides by Matthew DeHaven and talks by Scott Cunningham
Course Home Page
Today: a document where no number is typed by hand.
Every table and figure in the paper is produced by code that can be re-run from the raw data.
If it is not produced by the pipeline, it is not evidence.
Adapted from Scott Cunningham’s Baylor talk (2026).
The usual way: run the regression, read the coefficient, type it into the paper.
None of these is a coding error. The code is fine. The document is wrong.
A number computed in the document has provenance: you can point to the line that made it.
A typed number has none, whoever typed it.
Quarto is an open-source publishing system.
.qmd) with text and code.
A quarto is a book made by folding a sheet twice: one source, many pages.
You may have seen R Markdown (.Rmd) files.
Most .Rmd files render in Quarto with few changes.
Every .qmd file has:
--- lines: settings for this document.YAML sets options with key: value. Indentation means nesting.
The body is the Markdown you already know from READMEs.
Math uses LaTeX syntax.
$\beta_1$ gives \(\beta_1\).\[ y_{it} = \beta \log(\text{gdp}_{it}) + \gamma_t + \varepsilon_{it} \]
Three backticks make a code block: it is shown, not run.
paper.qmd
When you render, the cell runs and its output appears in the document.
Quarto runs code with an engine.
{r}: the knitr engine.{python} (and no R cells): the Jupyter engine.{julia} cells: Julia’s own engine.One engine per document.
reticulate package.Options go at the top of a cell, on lines starting with #|.
The most common:
| Option | What it does |
|---|---|
echo |
show the code? |
eval |
run the code? |
output |
show the result? |
include |
show anything at all? (it still runs) |
warning, message |
show warnings and messages? |
echo: false: Show the Result, Not the Codeoutput and includeMost documents start with a hidden cell that loads packages and data.
gap.Set an option once in the YAML instead of in every cell:
paper.qmd
#| echo: true shows that one cell.All cells run in order, in one fresh R session.
Fresh means fresh: objects in your console do not exist when you render. If the document renders, the code runs from scratch. That is a check.
Inline code puts a computed value in the middle of a sentence: `{r} expression`.
The panel covers 142 countries.
Change the sample, re-render, and the sentence changes with it.
In a hidden cell, run the regression and pull out the pieces:
coef(), se(), and nobs() work on any fixest model.broom::tidy(m) gives the same numbers as a data frame.paper.qmd
Renders as:
A one-log-point increase in GDP per capita is associated with 7.77 more years of life expectancy (SE 0.38, N = 1,704).
Every number in that sentence traces to the cell on the previous slide.
Do the formatting in R, once, with a small helper:
log(gdpPercap)
"7.77"
[1] "0.500"
[1] "4.3%"
[1] "1,234,567"
round(0.5, 2) prints 0.5; fmt() keeps the trailing zero.fmt() in your setup cell or a sourced script.All of these should come from code:
Numbers that are not results can be typed: the year a law passed, an equation number, a citation.
We use a panel of 142 countries observed every five years from 1952 to 2007. A one-log-point increase in GDP per capita is associated with 7.8 more years of life expectancy, about 13% of the sample mean.
etable and modelsummaryYou know etable() from 2/24. It prints well in the console:
m m2
Dependent Var.: lifeExp lifeExp
log(gdpPercap) 7.768*** (0.3772) 1.450* (0.6795)
Fixed-Effects: ----------------- ---------------
year Yes Yes
country No Yes
_______________ _________________ _______________
S.E.: Clustered by: country by: country
Observations 1,704 1,704
R2 0.71952 0.93600
Within R2 0.65153 0.01859
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
For documents, modelsummary() makes a table in the output format: HTML, LaTeX, or Word.
| Year FE | Country and year FE | |
|---|---|---|
| + p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001 | ||
| Log GDP per capita | 7.768*** | 1.450* |
| (0.377) | (0.679) | |
| Num.Obs. | 1704 | 1704 |
| R2 | 0.720 | 0.936 |
Give a cell a label with a prefix, and a caption:
Then refer to it in the text with @:
tbl- for tables, fig- for figures, eq- for equations, sec- for sections.Renders as:
Figure 1 plots life expectancy against income in 2007.
fig-width and fig-height (in inches) set the size.fig-cap is the caption; the fig- label makes it numbered.A. Compute in the document. Small projects: the .qmd loads clean data and runs the models.
B. Scripts compute, the document reads. Bigger projects, and most of yours:
code/01-clean.R data/raw/ -> data/clean/
code/02-estimate.R data/clean/ -> output/models.rds
paper.qmd output/ -> paper.pdf
paper.qmd
saveRDS() or write.csv().main.R, or a Makefile) runs every step, then renders the paper.The same file renders to different formats. Only format: changes.
Or several at once:
pdf): Quarto writes LaTeX and compiles it. Needs a LaTeX installation: run quarto install tinytex once.docx): for coauthors who track changes in Word. Tables from modelsummary come out as Word tables.typst): a newer typesetting system, built into Quarto. Compiles much faster than LaTeX; fewer journal templates.For your final project, PDF or Typst is the natural choice.
Keep your references in a BibTeX file and cite them by key.
A folder with a _quarto.yml file is a Quarto project.
_quarto.yml apply to every document in the folder.quarto render with no file name renders the whole project._quarto.yml_quarto.yml
execute-dir: projectWhere does code look for files?
execute-dir: project: relative to the project root.You built a personal website on 3/24. If you used Quarto, it was a project like this:
_quarto.yml
.qmd files, rendered to linked HTML pages.Rendering re-runs every cell. When that is slow:
execute: freeze: auto in _quarto.yml: a project-wide render only re-runs documents whose source changed.#| cache: true on a cell: knitr saves that cell’s result and re-runs it when its code changes.Both look only at your code, not at your data.
quarto render --cache-refresh, or delete _freeze/ and the _cache folders.Your presentation can be Quarto, from the same project as your paper.
format: revealjs: HTML slides (these slides).format: beamer: PDF slides through LaTeX.The figures in your talk then come from the same code as the figures in your paper.
Each ## header starts a new slide.
columns div, three for each column.In a talk, show results, not code: set echo: false in the YAML.
Showing code is occasionally the point. Then put the output beside it:
embed-resources: true packs everything into one file you can email or upload.E, then print to PDF.About 1 in 12 men has some form of color blindness, and projectors wash colors out.
viridis (in ggplot2 as scale_color_viridis_d()).More: Jesse Shapiro, “How to Give an Applied Micro Talk”; Rachel Meager, “Public Speaking for Academic Economists”; Jonathan Schwabish, “An Economist’s Guide to Visualizing Data”.
Your final project paper is a Quarto document, and every number in it comes from code.
quarto render on a fresh clone rebuilds the paper.Your slides can be Quarto too, from the same code.
my-project/
├── AGENTS.md # rules for agents (from 4/5)
├── README.md # how to rebuild everything
├── renv.lock
├── _quarto.yml
├── data/raw/ # never edited
├── code/ # clean, estimate, save
├── output/ # saved models, tables, figures
├── paper.qmd
└── slides.qmd
Write it down, as on 4/5. For example, in AGENTS.md:
AGENTS.md
Then check, as on 4/7: in the diff, does any new number appear in the prose without {r}?
Search the prose for decimals:
`{r} ...` or a code cell is fine.On 4/19 we go further: tests, and the same estimate computed in R and in Python.
renv::restore().quarto render paper.qmd.Before presentations, an agent (or a classmate) has to do exactly this from your repository alone.
Paper due date: [TBD: final paper due date].
.qmd file is a YAML header plus Markdown plus code cells.echo, include, label, fig-cap) control what the reader sees.modelsummary and ggplot2 make tables and figures; @tbl- and @fig- refer to them.Quarto documentation: quarto.org/docs/guide