Options

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

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:

  1. The chunk header must be written on one line. You must not break the line.
  2. Try to avoid spaces, periods ( . ), and underscores ( _ ) in chunk labels and paths. If you need separators, you are recommended to use hyphens ( - ) instead. For example, setup-options is a good label, whereas setup.options and chunk 1 are bad; fig.path = 'figures/mcmc-' is a good path for figure output, and fig.path = 'markov chain/monte carlo' is bad.
  3. All option values must be valid R expressions. You may think of them as values to be passed to function arguments.

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)”.

Code evaluation

Text output