Chapter 9 Chunk Options

As illustrated in Figure 2.1, the R package knitr is one of the two major components of R Markdown (the other being Pandoc). In this chapter and the next three chapters, we show some recipes related to knitr.

There are more than 50 chunk options that can be used to fine-tune the behavior of knitr when processing R chunks. Section 2.8 of the R Markdown Definitive Guide (Xie, Allaire, and Grolemund 2018) provides a subset of these options, while the full list is available at https://yihui.org/knitr/options/.

In the following sections, we only show examples of applying chunk options to individual code chunks. However, please be aware of the fact that any chunk options can also be applied globally to a whole document, so you do not have to repeat the options in every single code chunk. To set chunk options globally, call knitr::opts_chunk$set() in a code chunk (usually the first one in the document), e.g.,

```{r, include=FALSE}
knitr::opts_chunk$set(
  comment = "#>", echo = FALSE, fig.width = 6
)
```

References

Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2018. R Markdown: The Definitive Guide. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown.