6.3 Code chunks

Quarto can use the following ways to specify chunk options:

  • R Markdown style: Use tag=value in the chunk header ```{r}.

    ```{r #fig-my-label, out.width="100%", fig.cap = caption}
    f_name <- "images/Logistic_drill_sim.png"
    knitr::include_graphics(f_name)
    ```
    @fig-my-label shows the logistic drill simulation
  • Quarto style: Use #| tag: value

    Alternatively, you can write chunk options in the body of a code chunk after #|, e.g.,

    ```{r}
    #| label: fig-my-label
    #| fig-cap: caption 
    
    f_name <- "images/Logistic_drill_sim.png"
    knitr::include_graphics(f_name)
    ```

The labels are different when you use the R Markdown style vs. the Quarto style.

  • R Markdown style: #fig-my-label (with # in front of the label)
  • Quarto style: fig-my-label (without # in front of the label)

tag: value is the YAML syntax.

Options format:

  • space after #| and colon :
  • Logical values in YAML can be any of: true/false, yes/no, and on/off.
    • They all equivalent to TRUE/FALSE (uppercase) in Rmd.
    • Note that TRUE/FALSE need to be in uppercase in Rmd.

Note that Quarto accepts Rmd’s way of specifying chunk options. The difference is that Quarto’s label for figures must start with fig-, while Rmd accepts any labels.

```{r label = "fig-my-label", fig.cap = caption}

# R code
```

❗️ Note that NOT all Rmd chunk options are supported in Quarto, e.g., Rmd’s results are replaced with output in Quarto. See Quarto Chunk Options for all available options.


Tables

Tables are complicated by there are multiple ways to specify captions and float positions. You can use chunk options, or knitr::kable(caption) and kableExtra::kable_styling(booststrap_options = c("hold_position").

Use the following snippet for tables cross-references in Quarto:

```{r}
#| label: tbl-summary-stat
#| tbl-cap: "Summary Statistics"
#| tbl-pos: H
summary_stats %>% 
  knitr::kable(format="latex", booktabs=TRUE, linesep="") %>%  ← add kable options here, eg digits, booktabs, format, etc.
  kableExtra::kable_styling(latex_options = c("striped")) ← add kableExtra options here, eg striped, scale_down, repeat_header, etc.
```
@tbl-summary-stat shows the summary statistics

Note:

  • No # in front of the label, and must start with tbl-.

  • Use code chunks to specify caption (tbl-cap) and float position (tbl-pos)

    ‼️ Do NOT use knitr::kable(caption) to specify caption. This will generate an error in Quarto.

  • use @tbl-summary-stat to reference the table in the text.


Load default chunk options from a separate file, e.g., _chunk-opt.qmd:

```{r setup, child = "../_chunk-opt.qmd", include=FALSE}
```

Note if you use tag=value in the chunk header, TRUE/FALSE must be in uppercase. If you use #| tag: value in the body of the chunk, true/false should be be in lowercase.

Alternatively, use shortcode {{< shortcode ... >}} to include a file in Quarto: ✅

{{< include ../_chunk-opt.qmd >}}

Quarto chunk options available for customizing output include:

Option Description
eval Evaluate the code chunk (if false, just echos the code into the output).
echo Include the source code in output
output\(^{[1]}\) Include the results of executing the code in the output (true, false, or asis to indicate that the output is raw markdown and should not have any of Quarto’s standard enclosing markdown).
warning Include warnings in the output.
error Include errors in the output (note that this implies that errors executing code will not halt processing of the document).
include Defaults to true. Catch all for preventing any output (code or results) from being included (e.g. include: falsesuppresses all output from the code block).
renderings Specify rendering names for the plot or table outputs of the cell, e.g. [light, dark]

\(^{[1]}\) output is similar to results in Rmd. You cannot use results='hide' in Quarto, use output: false instead.