ZetCode

Quarto

last modified November 19, 2026

Quarto is a publishing system built on pandoc and knitr. A Quarto document is a plain text file with the extension .qmd: a YAML header, Markdown prose, and code cells that start with three backticks and a language in braces. The quarto command-line tool executes the cells, converts the document, and writes the output. The source is close to R Markdown, but the tool, the option syntax inside cells, and the project model are different. This article renders a first document, adds code cells and inline values, controls cells with #| options, produces two formats from one source, builds a project, and closes with a complete report. The examples use Quarto 1.11.9, knitr 1.52, pandoc 3.10, and R 4.5.2. The scripts read the generated HTML with xml2 to check what the renderer produced.

The examples need the knitr and xml2 packages, which are not part of base R. Install them once with install.packages(c("knitr", "xml2")); the installing packages section of the introduction explains the details. The quarto command itself is a separate program with its own copy of pandoc; download it from quarto.org.

A first document

The YAML header sets the title and the output format, and the body uses Markdown headings and paragraphs. Unlike R Markdown, the conversion is started by the quarto program rather than by a function call inside R, so the R script writes the file and then runs the command with system2. quarto render prints the pandoc settings it will use and the name of the file it created.

first.R
lines <- c(
  "---",
  "title: \"Quarterly report\"",
  "format: html",
  "---",
  "",
  "## Summary",
  "",
  "Quarto renders this document with pandoc."
)
writeLines(lines, "report.qmd")
status <- system2("quarto", c("render", "report.qmd"))
cat("status:", status, "\n")
cat("html:", file.exists("report.html"), "\n")
page <- xml2::read_html("report.html")
cat("title:", xml2::xml_text(xml2::xml_find_first(page, "//title")), "\n")
unlink(c("report.qmd", "report.html", "report_files"), recursive = TRUE)

The script reports status 0, confirms that report.html exists, and prints the title from the YAML header. The pandoc settings in the output describe the conversion: a standalone HTML file, section divs for each heading, and MathJax for formulas. The script removes the generated files at the end, so the next example starts from a clean directory.

$ Rscript first.R
status: 0 
html: TRUE 
title: Quarterly report 
pandoc 
  to: html
  output-file: report.html
  standalone: true
  section-divs: true
  html-math-method: mathjax
  wrap: none
  default-image-extension: png
  
metadata
  document-css: false
  link-citations: true
  date-format: long
  lang: en
  title: Quarterly report
  
Output created: report.html

Code cells and inline code

A code cell opens with three backticks and braces around the language and an optional label, and closes with three backticks. The cells share one R session, so an object created in one cell is visible in the next. Inline code uses a single backtick pair with an r prefix, the same convention as R Markdown. The --quiet flag keeps the render output out of the console when a script only needs the resulting file.

chunks.R
lines <- c(
  "---",
  "title: \"Chunk output\"",
  "format: html",
  "---",
  "",
  "## Totals",
  "",
  "```{r}",
  "orders <- data.frame(qty = c(2, 5, 3), price = c(10, 4, 12))",
  "total <- sum(orders$qty * orders$price)",
  "total",
  "```",
  "",
  "The order total is `r total` euros.",
  "",
  "```{r}",
  "paste(\"rows:\", nrow(orders))",
  "```"
)
writeLines(lines, "chunks.qmd")
status <- system2("quarto", c("render", "chunks.qmd", "--quiet"))
cat("status:", status, "\n")
page <- xml2::read_html("chunks.html")
paras <- xml2::xml_text(xml2::xml_find_all(page, "//p"))
cat("paragraphs:", length(paras), "\n")
cat("last:", paras[length(paras)], "\n")
cat("rows shown:", grepl("rows: 3", xml2::xml_text(page), fixed = TRUE), "\n")
unlink(c("chunks.qmd", "chunks.html", "chunks_files"), recursive = TRUE)

The first cell prints 76, and the sentence after it renders as "The order total is 76 euros." The second cell prints the row count, and the script finds rows: 3 in the page. The script reads the HTML back with xml2 and reports the one paragraph that contains the inline value, which confirms that the expression ran in the document session rather than in the script that wrote the file.

$ Rscript chunks.R
status: 0 
paragraphs: 1 
last: The order total is 76 euros. 
rows shown: TRUE 

Cell options

Quarto writes cell options as YAML comments at the top of the cell, each line starting with #|. echo: false hides the code but keeps its output, eval: false shows the code without running it, and warning: false keeps warnings out of the output. The syntax is the YAML key-value form, so values are written without R quotation marks and several options stack as several lines.

cells.R
lines <- c(
  "---",
  "title: \"Cell options\"",
  "format: html",
  "---",
  "",
  "## Options",
  "",
  "```{r}",
  "#| echo: false",
  "hidden <- 42",
  "```",
  "",
  "```{r}",
  "#| eval: false",
  "skipped <- read.csv(\"missing.csv\")",
  "```",
  "",
  "```{r}",
  "#| warning: false",
  "as.numeric(\"abc\")",
  "```"
)
writeLines(lines, "cells.qmd")
status <- system2("quarto", c("render", "cells.qmd", "--quiet"))
cat("status:", status, "\n")
text <- xml2::xml_text(xml2::read_html("cells.html"))
cat("hidden code:", grepl("hidden <- 42", text, fixed = TRUE), "\n")
cat("skipped code:", grepl("skipped <- read.csv", text, fixed = TRUE), "\n")
cat("warning:", grepl("NAs introduced by coercion", text, fixed = TRUE), "\n")
unlink(c("cells.qmd", "cells.html", "cells_files"), recursive = TRUE)

The hidden cell leaves no code in the page, the skipped cell keeps its code but never reads the missing file, and the successful coercion produces no warning in the output. The three checks read the generated HTML and report FALSE, TRUE, and FALSE in that order. Without the warning option, the page would contain the "NAs introduced by coercion" message after the code.

$ Rscript cells.R
status: 0 
hidden code: FALSE 
skipped code: TRUE 
warning: FALSE 

One source, several formats

The format key accepts a list as well as a single name. html produces a web page, and gfm produces GitHub-flavoured Markdown. One render call then writes one output file per format, all from the same body. The code runs once for each format, and each writer applies its own rules to the result.

formats.R
lines <- c(
  "---",
  "title: \"Two formats\"",
  "format:",
  "  html: default",
  "  gfm: default",
  "---",
  "",
  "## Notes",
  "",
  "The same source becomes a web page and a Markdown file.",
  "",
  "```{r}",
  "2 + 2",
  "```"
)
writeLines(lines, "formats.qmd")
status <- system2("quarto", c("render", "formats.qmd", "--quiet"))
cat("status:", status, "\n")
cat("html:", file.exists("formats.html"), "\n")
cat("markdown:", file.exists("formats.md"), "\n")
cat(readLines("formats.md"), sep = "\n")
unlink(c("formats.qmd", "formats.html", "formats.md", "formats_files"), recursive = TRUE)

Both formats.html and formats.md are created. The Markdown file keeps the section heading and wraps the cell output in an indented block, while the HTML file gets the full page template. The script prints the Markdown body so the difference is visible: the title is metadata for the HTML template and does not appear in the Markdown text.

$ Rscript formats.R
status: 0 
html: TRUE 
markdown: TRUE 
# Two formats


## Notes

The same source becomes a web page and a Markdown file.

``` r
2 + 2
```

    [1] 4

Building a project

A directory with a _quarto.yml file is a Quarto project. The configuration can set the project type and an output directory, and quarto render with the directory as its argument renders every document inside it. The generated files go to the configured directory instead of sitting next to the sources, which keeps a report folder tidy.

project.R
dir.create("book", showWarnings = FALSE)
writeLines(c(
  "project:",
  "  type: default",
  "  output-dir: _output"
), "book/_quarto.yml")
writeLines(c(
  "---",
  "title: \"Home\"",
  "---",
  "",
  "The project builds every page."
), "book/index.qmd")
writeLines(c(
  "---",
  "title: \"Notes\"",
  "---",
  "",
  "A second page."
), "book/notes.qmd")
status <- system2("quarto", c("render", "book", "--quiet"))
cat("status:", status, "\n")
cat("files:", list.files("book/_output"), "\n")
unlink("book", recursive = TRUE)

The project renders two pages, index.html and notes.html, and writes them to book/_output. Each page also gets a support directory for its assets. The configuration file sits at the project root, so the command works from any directory as long as the path to the project is given. The unlink call removes the whole folder after the check.

$ Rscript project.R
status: 0 
files: index_files index.html notes_files notes.html 

A complete report

The final example combines the pieces into a small sales report. The script saves a data frame as an RDS file, writes a document that reads it back, aggregates revenue by product, and shows the result as a table and a bar chart. The total is computed inline, and the render runs quietly because the script only needs the facts that follow.

complete.R
sales <- data.frame(
  product = c("pen", "book", "bag", "cup", "book", "pen"),
  qty = c(10, 3, 2, 8, 6, 4),
  price = c(2.5, 12, 30, 8, 12, 2.5)
)
saveRDS(sales, "sales.rds")
lines <- c(
  "---",
  "title: \"Sales summary\"",
  "format: html",
  "---",
  "",
  "## By product",
  "",
  "```{r}",
  "sales <- readRDS(\"sales.rds\")",
  "by_product <- aggregate(qty * price ~ product, data = sales, FUN = sum)",
  "names(by_product)[2] <- \"revenue\"",
  "by_product <- by_product[order(-by_product$revenue), ]",
  "knitr::kable(by_product, row.names = FALSE)",
  "```",
  "",
  "Total revenue is `r sum(sales$qty * sales$price)` euros.",
  "",
  "```{r}",
  "barplot(by_product$revenue, names.arg = by_product$product,",
  "        main = \"Revenue by product\", ylab = \"euros\")",
  "```"
)
writeLines(lines, "sales.qmd")
status <- system2("quarto", c("render", "sales.qmd", "--quiet"))
cat("status:", status, "\n")
page <- xml2::read_html("sales.html")
cat("tables:", length(xml2::xml_find_all(page, "//table")), "\n")
cat("table rows:", length(xml2::xml_find_all(page, "//table//tr")), "\n")
cat("images:", length(xml2::xml_find_all(page, "//img")), "\n")
line <- xml2::xml_find_first(page, "//p[contains(., 'Total revenue')]")
cat("summary:", xml2::xml_text(line), "\n")
unlink(c("sales.rds", "sales.qmd", "sales.html", "sales_files"), recursive = TRUE)

The rendered page has one table with a header and four product rows, one image, and the sentence "Total revenue is 267 euros." The table and the chart use the same sorted data, so the largest bar matches the first table row. The report reads its input from a file, which means the document can be rendered again whenever the data changes without editing the source.

$ Rscript complete.R
status: 0 
tables: 1 
table rows: 5 
images: 1 
summary: Total revenue is 267 euros. 

Next steps

The R Markdown article covers the older system that Quarto grew out of, including the chunk options and the render function that Quarto does not use. The data I/O article explains the RDS file in the complete report, and the ggplot2 article shows richer figures for the same kind of document. For a series of pages rather than one report, the project model is the starting point, and the Quarto documentation covers websites and books built on it.

Source

Quarto: guide

Quarto: R computations

Quarto: HTML format reference

Quarto: projects

In this article we wrote a Quarto document and rendered it with the quarto command. We added code cells and inline values, controlled the cells with #| options, and produced HTML and Markdown from one source. A project configuration rendered two pages into a separate output directory, and the final report combined a table, a chart, and an inline total. The older R Markdown system produces the same kind of report with a function call instead of a command-line build.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all R Language tutorials.