Chunk options

Ranae Dietzel and Andee Kaplan

Be like Sloth, Sloth love chunk

Code chunks

(As you know) we can insert code into our R markdown documents via chunks, to be evaluated via knitr

Chunk options

There are many many options that govern the behavior, look, and output of chunks in a dynamic document. We write chunk options in the form tag=value:

Rules

  1. You do not talk about chunk options.
  2. You do not talk about chunk options.
  3. Chunk options must be written in one line; no line breaks are allowed inside chunk options.
  4. Avoid spaces and periods in chunk labels and directory names.
  5. All option values must be valid R expressions just like how we write function arguments

Commonly used (by me) options

Your turn

Discuss with a partner:

Caching

cache=TRUE allows you to cache a code chunk. When evaluating code chunks, the cached chunks are skipped, but the objects created in these chunks are (lazy-) loaded from previously saved databases (.rdb and .rdx) files, and these files are saved when a chunk is evaluated for the first time, or when cached files are not found.

dependson lets you define a character vector of chunk labels to specify which other chunks this chunk depends on – sometimes the objects in a cached chunk may depend on other cached chunks, so when other chunks are changed, this chunk must be updated accordingly.

Global options

What if you want to set options for your entire document?

opts_chunk$set() can change the default global options in a document (e.g. put this in a code chunk: opts_chunk$set(echo=FALSE, message=FALSE, fig.width=6, fig.height=6)), and it will be applied to the entire document.

Language engines

knitr can use languages other than R as the engine to evaluate code. Currently, you can use:

Your turn

Open a new R markdown file in R Studio and change the following:

  1. Add some global options to hide messages and warnings.
  2. Change the pressure plot chunk so that it is a plot created in ggplot2
  3. Add a caption to your plot.
  4. Change the size (proportions) of your plot.