11 Rmarkdown Day 2
There are a lot of things you can do in a code chunk: you can produce text output, tables, or graphics. You have fine control over all these output via chunk options, which can be provided inside the curly braces (between ```{r and }). For example, you can choose hide text output via the chunk option results = ‘hide’, or set the figure height to 4 inches via fig.height = 4. Chunk options are separated by commas, e.g.,
The value of a chunk option can be an arbitrary R expression, which makes chunk options extremely flexible. For example, the chunk option eval controls whether to evaluate (execute) a code chunk, and you may conditionally evaluate a chunk via a variable defined previously, e.g.,
```{r} # execute code if the date is later than a specified
day do_it = Sys.Date() > ‘2018-02-14’ ```
```{r, eval=do_it} x = rnorm(100) ```
There are a large number of chunk options in knitr documented at https://yihui.name/knitr/options. We list a subset of them below:
- eval: Whether to evaluate a code chunk.
Challenge 1 Set eval to false in your code chunk.
echo: Whether to echo the source code in the output document (someone may not prefer reading your smart source code but only results).
results: When set to ‘hide’, text output will be hidden; when set to ‘asis’, text output is written “as-is,” e.g., you can write out raw Markdown text from R code (like cat(‘Markdown is cool.’)). By default, text output will be wrapped in verbatim elements (typically plain code blocks).
collapse: Whether to merge text output and source code into a single code block in the output. This is mostly cosmetic: collapse = TRUE makes the output more compact, since the R source code and its text output are displayed in a single output block. The default collapse = FALSE means R expressions and their text output are separated into different blocks.
warning, message, and error: Whether to show warnings, messages, and errors in the output document. Note that if you set error = FALSE, rmarkdown::render() will halt on error in a code chunk, and the error will be displayed in the R console. Similarly, when warning = FALSE or message = FALSE, these messages will be shown in the R console.
include: Whether to include anything from a code chunk in the output document. When include = FALSE, this whole code chunk is excluded in the output, but note that it will still be evaluated if eval = TRUE. When you are trying to set echo = FALSE, results = ‘hide’, warning = FALSE, and message = FALSE, chances are you simply mean a single option include = FALSE instead of suppressing different types of text output individually.
cache: Whether to enable caching. If caching is enabled, the same code chunk will not be evaluated the next time the document is compiled (if the code chunk was not modified), which can save you time. However, I want to honestly remind you of the two hard problems in computer science (via Phil Karlton): naming things, and cache invalidation. Caching can be handy but also tricky sometimes.
fig.width and fig.height: The (graphical device) size of R plots in inches. R plots in code chunks are first recorded via a graphical device in knitr, and then written out to files. You can also specify the two options together in a single chunk option fig.dim, e.g., fig.dim = c(6, 4) means fig.width = 6 and fig.height = 4.
Challenge 2 Set figure width and figure height to 6 and 4 in a plot of your data.
out.width and out.height: The output size of R plots in the output document. These options may scale images. You can use percentages, e.g., out.width = ‘80%’ means 80% of the page width.
fig.align: The alignment of plots. It can be ‘left’, ‘center’, or ‘right’.
dev: The graphical device to record R plots. Typically it is ‘pdf’ for LaTeX output, and ‘png’ for HTML output, but you can certainly use other devices, such as ‘svg’ or ‘jpeg’.
fig.cap: The figure caption. Challenge 3 Give your figure a caption
child: You can include a child document in the main document. This option takes a path to an external file.
If a certain option needs to be frequently set to a value in multiple code chunks, you can consider setting it globally in the first code chunk of your document, e.g.,
```{r, setup, include=FALSE} knitr::opts_chunk$set(fig.width
= 8, collapse = TRUE) ```