The problem this solves is specific and familiar. An analysis produces a number, somebody pastes it into a document, the data is corrected, the analysis is re-run, and the document still says the old number. Every copied result is a copy that can go stale.
A Quarto document holds the prose and the code in one file. Rendering runs the code and puts the real output into the document, so the numbers in the text are produced by the data every time. Change the data, render again, and the whole thing is current - including the charts and the sentence that says "sales rose by 14%".
Quarto is the successor to R Markdown and the one to learn now; it works the same way for R, adds Python, Julia and Observable, and renders the same .qmd to HTML, PDF, Word, slides, a website or a book. Existing .Rmd files keep working, and knitr is still the engine underneath for R.
A document has three parts. YAML front matter between --- lines sets the title, author and output formats. Prose is Markdown. Code chunks, fenced with ```{r}, run and place their output below themselves.
Chunk options control what the reader sees, and in Quarto they are written as #| comments inside the chunk. echo: false hides the code and keeps the output - the usual setting for a report aimed at people who do not want to read R. include: false runs it and shows nothing, which is right for the setup chunk. warning: false and message: false hide the library-loading chatter. fig-width and fig-height size a chart in inches.
Inline code is the feature that actually removes stale numbers. `r nrow(data)` inside a sentence runs and substitutes the result. Write "the sample contains `r nrow(data)` observations" and that sentence cannot be wrong.
Two rules make the output trustworthy. Never use setwd() - it hard-codes a path that exists on one machine. Use an RStudio project and the here package. And restart R and render from clean before sharing: a document that renders only because of an object left in your session is a document nobody else can reproduce.
---
title: "Quarterly Sales"
author: "Analytics"
date: today
format:
html:
toc: true
code-fold: true # readers can expand the code if they want it
pdf:
documentclass: article
docx: default # yes, Word. Quarto renders all three.
execute:
echo: false # the default for every chunk in this document
warning: false
---
```{r}
#| label: setup
#| include: false # runs, shows nothing. Right for setup.
library(dplyr)
library(ggplot2)
library(readr)
sales <- read_csv(here::here("data", "sales.csv"))
# ^^^^^^^^^^ here(), not setwd(). A path that works on
# your machine and nobody else's is not reproducible.
total <- sum(sales$revenue)
best <- sales |> slice_max(revenue, n = 1)
last_year <- sum(sales$revenue[sales$year == 2025])
growth <- (total - last_year) / last_year
Summary
Revenue this quarter was r scales::dollar(total) across
r nrow(sales) orders, which is r scales::percent(growth, 0.1)
against the same quarter last year. The strongest region was
r best$region.
Every number in that paragraph was computed from the data a moment ago. None of them can disagree with the chart below, and none of them can be left behind when the data is corrected - which is the entire point.
By region
#| label: fig-regions
#| fig-cap: "Revenue by region, this quarter."
#| fig-width: 8
#| fig-height: 4.5
sales |>
summarise(revenue = sum(revenue), .by = region) |>
ggplot(aes(reorder(region, revenue), revenue)) +
geom_col(fill = "steelblue") +
coord_flip() +
labs(x = NULL, y = "Revenue") +
theme_minimal(base_size = 12)
@fig-regions shows the split. Cross-references work because the chunk
has a label beginning fig-.
#| label: tbl-top
#| tbl-cap: "The ten largest orders."
sales |> slice_max(revenue, n = 10) |> knitr::kable()
Discussion
No questions on this topic yet. Sign in to ask the first one.