[1] 10Chunk options
Introduction
Code chunks in Quarto can be customized using chunk options, which control how the code is executed and how its results are displayed in the final document. Options can modify nearly every aspect of a chunk’s behavior — for example, whether the code is shown, whether messages are printed, how figures are formatted, or whether results are cached.
Chunk options give authors control over presentation, reproducibility, and efficiency. They allow you to separate background computation from displayed content, keep reports readable, and ensure that code runs reliably across environments. By thoughtfully combining these options, you can create documents that balance transparency (showing code and results) with polish (suppressing unnecessary output).
Quarto uses a YAML-style syntax for specifying chunk options, which is cleaner and more consistent than the inline syntax used in traditional R Markdown. This means that instead of placing all options inside the curly braces next to {r}, you write them as commented lines beginning with #| inside the chunk, directly below the chunk header. For example:
#| label: data-summary
#| echo: TRUE
#| message: FALSEHere:
- label:assigns a unique name to the chunk.
- echo:determines whether the code appears in the rendered document.
- message:controls whether R messages are shown.
This “Quarto way” of specifying options improves readability, makes long lists of options easier to manage, and ensures compatibility across different programming languages supported by Quarto (not just R).
Display Control Options
Display-related options determine what content from a chunk appears in the final rendered document.
- echocontrols whether the source code itself is shown (- TRUEshows it,- FALSEhides it).
- resultsspecifies how the printed results appear (e.g.,- "markup"for normal output or- "hide"to suppress).
- warningand- messagecontrol whether warnings or messages are displayed.
For instance, to show only the result of a calculation but not the underlying code, you could write in your R chunk:
#| echo: FALSE
mean(c(5, 10, 15))Which would only print the output when being rendered in a Quarto document:
Execution Options
Execution options govern whether and how the code runs when Quarto renders the document.
- evaldetermines if the chunk should be executed (- TRUEor- FALSE).
- includecontrols whether both code and results appear in the final output;- include: FALSEruns the code but hides all output.
- errorspecifies whether rendering should continue if an error occurs (- TRUE) or stop (- FALSE).
For example, a setup chunk that loads packages but doesn’t need to appear in the final report could look like this:
#| label: setup
#| include: FALSE
library(ggplot2)
library(dplyr)Figure and Table Options
When your chunk generates plots or tables, figure-related options control the formatting and appearance of these outputs.
- fig-capadds a caption to a plot.
- fig-widthand- fig-heightset the dimensions (in inches).
- fig-alignspecifies the alignment (- "left",- "center", or- "right").
For example:
#| label: visualization
#| echo: FALSE
#| fig-cap: "Distribution of scores by group."Caching and Performance Options
For time-consuming computations, Quarto allows you to cache results so they don’t have to be rerun each time you render the document.
- cache: TRUEsaves the chunk’s results; subsequent renders reuse them unless the code changes.
Caching helps speed up rendering in large analytical documents, though it’s important to refresh the cache if the data or upstream results change.