Working with Code

Chunk Options

Chunk options control how code is displayed and executed. In Quarto, options are specified inside the chunk using the #| prefix:

```r
#| echo: false
#| message: false
#| warning: false
#| fig-cap: "MPG by Cylinder Count"

ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
  geom_boxplot()
```

Common chunk options:

Option Default Purpose
echo: true true Show the code in the output
echo: false Hide the code; show only the output
eval: true true Execute the code
eval: false Show the code but do not execute it
message: false Suppress package loading messages
warning: false Suppress warning messages
fig-cap: "text" Add a caption to a figure
fig-width: 8 Set figure width in inches
fig-height: 6 Set figure height in inches
label: "my-chunk" Name the chunk for cross-referencing

A common pattern is to set default options for the entire document with a setup chunk at the top:

```r
#| label: quarto-setup-example
#| include: false
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```

The include: false option means the chunk runs but produces no output — useful for loading packages and setting options silently.

Inline Code

You can embed R results directly in your text using inline code. Enclose R expressions between `r and `:

The dataset contains 32 observations and 11 variables.
The average MPG is 20.1.

When rendered, this produces: “The dataset contains 32 observations and 11 variables. The average MPG is 20.1.” Inline code is valuable for reporting summary statistics that update automatically when the data changes — you never need to manually update a number in your text.