Warming up — Quarto
A quick check on Pair Programming 2
Welcome
This past week, you worked on your first Quarto document. You created the file, put your name in the header, added a code chunk and rendered it to HTML.
This warm-up four short parts. At each one, commit to an answer before you open the reveal. Say it out loud to your neighbour before you open the reveal.
Stop 1. Anatomy of a .qmd file
≈ 2 minutes
Here is a complete Quarto document, like the one you made last week:
---
title: "My First Quarto Document"
author: "Ana Smith"
format: html
---
## Running Code
When you click the **Render** button, a document will be generated.
```{r}
1 + 1
```Your turn. This file has three kinds of content. Name each one and say where it starts and ends. Then answer: which part is the only one R runs?
- The YAML header, between the two
---lines. It holds settings for the whole document: title, author and output format. Quarto reads it; R never does. - Markdown text: everything outside the header and the chunks.
##makes a section heading and**Render**makes bold text. It’s written for humans. - A code chunk, from the
```{r}line to the closing```. This is the only part R runs. When you render, R runs the code and pastes the output into the document right below it.
Stop 2. Fix the header
≈ 2 minutes
A pair wrote this header, clicked Render, and got an error instead of a document:
---
title: Pair Programming 2: Quarto in R
author Ana Smith
format: html
## Adding a Code ChunkYour turn. There are three mistakes. Find all three before you open the reveal.
- The title has a colon in it and no quotes. In YAML, a colon means “key: value”, so a second colon confuses it. When a value contains a colon, put it in quotes.
authoris missing its colon. Every line in the header iskey: value.- The header is never closed. Without the second
---, Quarto can’t tell where the settings end and the document begins.
The fixed version:
---
title: "Pair Programming 2: Quarto in R"
author: "Ana Smith"
format: html
---
## Adding a Code ChunkGood habit: always put quotes around the title and author. They cost you nothing and prevent the first mistake.
Stop 3. The code chunk
≈ 3 minutes
Four students each tried to add a chunk that prints "Hello, Quarto!":
A
```{r}
print("Hello, Quarto!")
```B
```r
print("Hello, Quarto!")
```C
'''{r}
print("Hello, Quarto!")
'''D
```{r}
print("Hello, Quarto!")Your turn. Only one of these works. Which one? For each of the other three, say what goes wrong.
- A works. It has three backticks,
{r}in curly braces, and three backticks to close it. - B has no curly braces. Quarto shows the code as grey text but never runs it, so no
[1] "Hello, Quarto!"appears below it. The{ }tells Quarto to run the code. - C uses apostrophes (
'''), not backticks (```). Quarto doesn’t treat it as a chunk at all, so the three lines appear as ordinary text. The backtick key is at the top left of most keyboards, next to 1. - D is never closed. Quarto treats everything after it, to the end of the document, as part of the chunk. That usually causes an error, or swallows the rest of the document.
Tip: the green +C button in RStudio (or Cmd+Option+I on Mac, Ctrl+Alt+I on Windows) inserts a correct chunk, so you never have to type the fences.
The grey box below is like the inside of a chunk. Whatever you type here is exactly what you would put between ```{r} and ```.
Your turn. Make R print a greeting with your own name in it, e.g. "Hello, Ana!". Use paste() so that your name is stored in an object first.
paste() glues pieces of text together, adding a space between them by default. paste0() adds no space.
paste("Hello,", "Quarto!")Solution:
my_name <- "Ana"
paste0("Hello, ", my_name, "!")Put these two lines inside a ```{r} chunk, render, and the rendered page shows the code followed by [1] "Hello, Ana!".
Stop 4. Predict the render
≈ 2 minutes
Your turn. Imagine you click Render on the document from Stop 1. What will the finished HTML page show, from top to bottom? Sketch it or describe it before you open the reveal. In particular:
- Do the
---lines appear on the page? - Does the
##appear? - Does the page show
1 + 1,2, or both?
Roughly this:
My First Quarto Document
Running Code
When you click the Render button, a document will be generated.
1 + 1
[1] 2
- The header disappears. Its settings turn into the big title and the author line.
##disappears. It becomes a section heading, and**Render**becomes bold.- You see both the code and its result. By default Quarto shows the code and its output. The
[1]just means “first element”.
Last check
Answer these from memory, then open the reveal.
- What’s the keyboard shortcut for Render?
- After rendering
analysis.qmd, what new file appears, and where? - You fixed a typo but the rendered page still shows it. What’s the most likely reason?
- Cmd+Shift+K on Mac, Ctrl+Shift+K on Windows.
analysis.html, in the same folder asanalysis.qmd. That HTML file is the one you share or submit. The.qmdis your source.- You didn’t re-render. The HTML is a snapshot of the last render, and editing the
.qmddoesn’t change it until you click Render again. (RStudio saves the file when you render, but it’s a good habit to save first anyway.)
Why this matters: your final project is a Quarto document, and the rubric checks that it renders cleanly. Every mistake on this page is one you can now spot in seconds.