The knitr package provides a lot of chunk options for customizing nearly all components of code chunks, such as the source code, text output, plots, and the language of the chunk. It also offers some options at the package level to customize the knitting process. This page documents all chunk options and package options available in knitr. The default values of these options are in parentheses in the list items.
Chunk options are written in chunk headers. The syntax for chunk headers depends on the document format, e.g., for .Rnw documents (R + LaTeX), chunk headers are written with >= , and for .Rmd documents, chunk headers are written with ``` . The examples below are primarily for .Rmd documents (R Markdown), but in most cases, the chunk options can be used with any document format.
Chunk options are written in the form tag=value like this:
A special chunk option is the chunk label (e.g., my-chunk in the above example). Only the chunk label does not need a tag (i.e., you only provide the value ). If you prefer the form tag=value , you could also use the chunk option label explicitly, e.g.,
The chunk label for each chunk is assumed to be unique within the document. This is especially important for cache and plot filenames, because these filenames are based on chunk labels. Chunks without labels will be assigned labels like unnamed-chunk-i , where i is an incremental number.
You may use knitr::opts_chunk$set() to change the default values of chunk options in a document. For example, you may put this in the first code chunk of your document:
``` knitr::opts_chunk$set( comment = '', fig.width = 6, fig.height = 6 ) ```
Below are a few more tips about chunk options:
Alternatively, you can write chunk options in the body of a code chunk after #| , e.g.,
``` #| my-chunk, echo = FALSE, fig.width = 10, #| fig.cap = "This is a long long #| long long caption." plot(cars) ```
For this syntax, chunk options must be written on continuous lines (i.e., all lines must start with the special comment prefix such as #| ) at the beginning of the chunk body. The blank line between the options and code is optional. This syntax allows for hard-wrapping the options. You can break the options onto as many lines as you wish. If the same option is provided in both the chunk body and in the chunk header (inside ```<> ), the former will override the latter. You can also use the YAML syntax to write options inside a chunk in the form tag: value . Normally you have to provide only one option per line, e.g.,
``` #| echo: false #| fig.width: 10 ```
If you choose to use the YAML syntax, the option values must be valid YAML values instead of raw R expressions.
Below is a list of chunk options in knitr documented in the format “ option : ( default value ; type of value)”.
``` [1] 1 2 3 ```
``` cat("I'm raw **Markdown** content.\n") ```
``` rnorm(10) ```
Low-level plotting commands include lines() and points() , etc. To better understand fig.keep , consider the following chunk:
``` plot(1) # high-level plot abline(0, 1) # low-level change plot(rnorm(10)) # high-level plot # many low-level changes in a loop (a single R expression) for(i in 1:10) < abline(v = i, lty = 2) >```
Normally this produces 2 plots in the output (because fig.keep = 'high' ). For fig.keep = 'none' , no plots will be saved. For fig.keep = 'all' , 4 plots are saved. For fig.keep = 'first' , the plot produced by plot(1) is saved. For fig.keep = 'last' , the last plot with 10 vertical lines is saved.
There are two hidden options that are not designed to be set by users: fig.cur (the current figure number or index when there are multiple figures), and fig.num (the total number of figures in a chunk). The purpose of these two options is to help knitr deal with the filenames of multiple figures as well as animations. In some cases, we can make use of them to write animations into the output using plot files that are saved manually (see the graphics manual for examples).
knitr::opts_chunk$set(engine.path = list( python = '~/anaconda/bin/python', ruby = '/usr/local/bin/ruby' ))
``` echo $PATH ``` ``` h2 < color: blue; >```
At the global level, it could be a list of strings named by engines. Like engine.path , this is useful to template arguments via knitr::opts_chunk$set() .
knitr::opts_chunk$set(engine.opts = list( perl = '-Mstrict -Mwarnings', bash = '-o errexit' ))
The package options can be changed using the object knitr::opts_knit (not to be confused with knitr::opts_chunk ). For example:
knitr::opts_knit$set(progress = TRUE, verbose = TRUE)
See ?knitr::opts_knit for the alternative approach to setting package options using the R base function options() .
Available package options are as follows:
Global R options are set with options() in base R. Below is a list of options that affect the behavior of knitr:
function(total, labels) < total = total, .auto_close = FALSE ) list( update = function(i) < cli::cli_progress_update(id = id) >, done = function() < cli::cli_process_done(id) >) >
And below is an example of using a Windows progress bar (which works only on Windows, obviously):
function(total, labels) < pb = winProgressBar("Knitting. ", max = total) list( update = function(i) < setWinProgressBar(pb, i, label = labels[i]) >, done = function() < close(pb) >) >