3.11 Quarto: Combining Code and Narrative
So far, we have written R code in scripts — files that contain only code and comments. But in professional BI work, the analysis is only part of the deliverable. You also need to explain what you did, why you did it, and what the results mean. Quarto is a document format that lets you combine R code, its output, and narrative text in a single file, producing polished reports in HTML, PDF, or Word format.
A Quarto document (.qmd file) has three components:
- YAML header — metadata at the top of the file (title, author, output format), enclosed between
---markers - Text — written in Markdown, a simple formatting language where
**bold**produces bold,*italic*produces italic, and#creates headings - Code chunks — blocks of R code enclosed between
```{r}and```markers, which are executed when the document is rendered
Here is a minimal Quarto document:
---
title: "Absenteeism Analysis"
author: "Your Name"
format: docx
---
## Summary Statistics
The following code loads the absenteeism data and computes summary statistics.
``` r
data <- read.csv("data/Absenteeism_at_work.csv", sep = ";")
summary(data$Absenteeism.time.in.hours)
```
```
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 2.000 3.000 6.924 8.000 120.000
```
The average absence is 6.9243243 hours.When you click the Render button in RStudio (or run quarto render from the terminal), Quarto executes all code chunks, captures their output, and combines everything into a finished document. Changing format: docx to format: html or format: pdf produces a different output format from the same source file.
Quarto is the successor to R Markdown, which served the same purpose for over a decade. You may encounter R Markdown files (.Rmd) in older projects and resources — the concepts are nearly identical, and skills transfer directly between the two. This textbook uses Quarto for all student assignments. For a complete reference on Quarto document syntax, code chunk options, and output formats, see the Quarto appendix.