Day 4 - Thursday, May 21, 2026
Today: Thursday May 21
Before class
Plan for today
.qmd fileHelpful links
Yesterday we covered:
ggplot2Today we’ll learn how to wrap your code and your prose into a single reproducible document.
Imagine you’ve just finished a homework assignment in R. You:
Then your professor says: “oh, please redo this with the updated data.”
Uh oh
Now you have to re-do everything by hand. And to make things worse, every manual step is a place where mistakes happen.
A reproducible document is one where the report and the code that made it are the same file.
When the data changes, you just re-render. Every number, every plot, every table updates automatically.
This is what Quarto is for.
Quarto is a system for writing documents that mix:
Quarto turns the whole thing into a polished HTML page, PDF, Word doc, slide deck, or even a website.
It’s what I used to make the slides you’re looking at right now.
You might hear about R Markdown (.Rmd files) - it’s Quarto’s older sibling.
In this class we’ll use Quarto (.qmd files) exclusively.
Note
If you see .Rmd examples online (including in the Data Computing book), they translate almost directly to .qmd. Don’t worry too much about the difference.
.qmd file?A Quarto document is a plain text file with three kinds of content:
---```{r} and ```# headings and **bold**You can open a .qmd file in any text editor.
---
title: "Penguin sizes"
author: "Charles Costanzo"
format: html
---
# Introduction
We have data on 344 penguins
from three species.
```r
library(palmerpenguins)
library(ggplot2)
ggplot(penguins, aes(x = body_mass_g)) +
geom_histogram(bins = 30,
color = "black",
fill = rgb(0, 206/255, 209/255)) +
theme_classic() +
labs(title = "Distribution of Body Mass (grams)",
x = "Body Mass (grams)",
y = "Count of Penguins")
```
The distribution looks roughly **bimodal**, with two peaks.The block at the very top, between the two --- lines:
This tells Quarto things like:
Fun Fact
YAML originally stood for “Yet Another Markup Language.” However, unlike HTML or XML, which are designed to mark up and format text documents, YAML was designed strictly for data serialization: storing data, lists, and configuration settings in a way humans can easily read. To clarify its actual purpose, the creators later changed it to “YAML Ain’t Markup Language.” This makes it a “recursive acronym” (where the Y stands for YAML itself).
YAML is picky about formatting:
YAML gotchas
title: "Hi", not title:"Hi"."My: Report", not My: Report.If your YAML breaks, your document won’t render. Most error messages will point to the line number.
You can also render to multiple formats at once:
Note
PDF rendering needs LaTeX installed on your machine. RStudio will prompt you to install it the first time you try.
A code chunk is a block of R code embedded in your document. It looks like this:
When you render the document, Quarto:
So your readers see your work and its results, side-by-side, always in sync.
Keyboard shortcut (recommended):
Cmd + Option + ICtrl + Alt + IToolbar button: the green +C icon at the top of the editor.
Type it yourself: ```{r} on one line, your code, ``` to close.
Tip
Learn the keyboard shortcut. You’ll use it a lot.
You don’t have to render the whole document every time. While editing, you can run chunks one at a time:
| Action | Shortcut |
|---|---|
| Run the current line | Cmd/Ctrl + Enter |
| Run the whole chunk | Cmd/Ctrl + Shift + Enter |
| Run all chunks above | (button at top of chunk) |
This is exactly like working in the Console, but your code stays saved in the document.
You can control what each chunk does by adding options at the top, prefixed with #|:
Common options:
| Option | What it does |
|---|---|
echo: false |
Hide the code, show the output |
eval: false |
Show the code, don’t run it |
include: false |
Run the code, hide everything |
message: false |
Hide messages (e.g., from library()) |
warning: false |
Hide warnings |
Quick decision guide
echo \(\rightarrow\) false everywhere.include \(\rightarrow\) false to hide the noise.eval \(\rightarrow\) false.message \(\rightarrow\) false.You can set options chunk-by-chunk, or globally in the YAML header (more on that in a bit).
A common pattern: have one chunk at the top of your document that loads packages and sets defaults.
label: setup is a special name - Quarto runs this chunk first, automatically.include: false hides it from the rendered output (your reader doesn’t need to see library(tidyverse)).Outside code chunks, your prose is written in Markdown - a simple syntax for formatting plain text.
| You type | You get |
|---|---|
*italic* |
italic |
**bold** |
bold |
`code` |
code |
# Heading 1 |
(big heading) |
## Heading 2 |
(smaller heading) |
- item |
bulleted list item |
1. item |
numbered list item |
[link text](https://example.com) |
link text |
Shortcut
In RStudio: Help > Markdown Quick Reference opens a cheat sheet you can keep open while you write.
RStudio has two ways to edit a Quarto document:
There’s a toggle in the top-left of the editor pane.
Note
Both editors save the same plain-text .qmd file underneath. You can switch back and forth freely.
I recommend starting in Visual mode if you’re new to Markdown - you’ll learn the syntax by seeing it appear.
When you click Render, Quarto:
.qmd file.knitr, which runs each code chunk in order.pandoc, which converts everything to your target format..qmd file.Fresh session
Quarto runs your code in a brand new R session every render. It does not know about objects you made in the Console.
If your code relies on something you typed in the Console, rendering will fail. Everything the document needs must be in the document.
In RStudio:
Cmd/Ctrl + Shift + KThe output opens automatically in the Viewer pane (or your browser).
Render early, render often.
Don’t write 200 lines and then try to render. Write a little, render, check it works, write a little more.
If you make a mistake in chunk #47, it’s much easier to find when you only added chunks #45–47 since your last successful render.
You’ll build a short Quarto report from scratch. Take 25 minutes for this activity.
To turn in: Save your .qmd file and the rendered HTML. We’ll check at the end of class.
Common stumbling blocks:
```{r} needs a matching ``` on its own line.Error: there is no package called 'palmerpenguins'. Install it in the Console.+ between ggplot() layers..qmd file as a mix of YAML, prose, and code chunks.Tomorrow: Quiz 1, then the pipe operator and chaining syntax.
Upcoming Deadlines
Reach out
Stuck? Office hours, or email me (cgc5478@psu.edu) or Xinyue (xpw5228@psu.edu).
Reference
Syllabus · AI policy · Schedule · R4DS Ch. 28