Rmarkdown global_options vs opts_chunk

时间:2015-10-30 21:21:52

标签: r knitr r-markdown

I've searched around a bit, and read this but I can't seem to find the difference between global_options and opts_chunk.

Below is a code chunk I use at the beginning of most of my RMD files, but I've never understood the difference. Any help would be appreciated.

```{r global_options, include=FALSE}
# This is a way to set options for all code chunks at once
# Note that you can also dynamically control options by setting them to a value
# DPI setting increased for Word output, cairo is anti aliasing
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE,
                      dev="png", fig.width = 10, fig.height = 7, dpi=200, dev.args=list(type="cairo"))
```

1 个答案:

答案 0 :(得分:5)

Setting global options will apply those options to all of the chunks of code in the document. Global options will be superseded by chunk options. The chunk you included is intended only to set global options (by calling the knitr opts_chunk command). In generally, chunks look like this:

```{r Chunk_Name, ...options...}
 code code code 
```

Your chunk is called global_options, and itself has a chunk option include=FALSE which means that when the document is rendered, the chunk will be executed, but the results and code not included in the rendered document. So when you render your Rmd, the global options will be set since the code is executed, but the final document won't show those options or the code used to set them.

Here are two nice cheatsheets to help remind you what the different options do and how to use Markdown docs.

https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf

https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf