Code chunks allow you execute code and embed the results within your R Markdown document. A code chunk is created by typing the delimeters ```{r}
and ```
and sandwiching your code in between. When specifying code chunk options, you can either:
r
(e.g. ```{r, warning = FALSE}
), orknitr::opts_chunk$set()
, typically in the first, auto-generated chunk in your R Markdown file (e.g. knitr::opts_chunk$set(warning = FALSE)
)While there are over 50 chunk options to help you customize the behavior of knitr when processing code chunks, we most commonly use the following options:
message = FALSE
: prevents warnings that are generated by code from appearing in the knitted documentwarning = FALSE
: prevents warnings that are generated by code from appearing in the knitted documentecho = FALSE
: prevents code, but not the results from appearing in the knitted document; this is how we are able to load packages, but not have library(...)
from printing in our knitted file; you can also use this to embed figures, but not their code in your documentsTIP: You can use the keyboard shortcuts, Ctrl + Alt + I (PC) or Cmd + Option + I (OS X), for quickly inserting code chunks.
RESOURCE: Check out knitr: Elegant, flexible, and fast dynamic report generation with R by Yihui Xie for an excellent overview of code chunk options.