3  Configuring Rmarkdown chunks

Author

Jonathan Seiden

Published

March 13, 2022

When you write R Markdown (or Quarto) reports, you are going to have a lot of “chunks” of code. These are the things that start with “```{r chunk_name, blah}” or “```{r, blah}.” When you render your report, these are run and the output is then taken and put in your report depending on how the chunk is configured.

This file gives some options for how to control these chunks.

3.1 Options for including/suppressing code and output

include: Should chunk be included in knit file? Defaults to TRUE. If FALSE, code chunk is run, but chunk and any output is not included in the knit file.

eval: Should chunk be evaluated by R? Defaults to TRUE. If FALSE, code chunk is included in the knit file, but not run.

echo: Should the code from this chunk be included in knit file along with output? Defaults to TRUE. If FALSE, the output from the chunk is included, but the code that created it is not. Most useful for plots.

3.2 Options for including/suppressing R messages

R has “errors” meaning it could not run your code, “warnings” meaning that the code was wrong, but there are some potential issues with it, and “messages” which are simply information about what your code ran. You can include or suppress each of these types of message.

error: Should R continue knitting if code produces an error? Defaults to FALSE. Generally don’t want to change this because it means you can miss serious issues with your code.

warning: Should R include warnings in knit file? Defaults to TRUE.

message: Should R include informational messages in knit file? Defaults to TRUE. Easy way to clean up your markdowns.

#This code produces an error
dat %>%
  filter(dest = 1)
Error in `filter()`:
! We detected a named input.
ℹ This usually means that you've used `=` instead of `==`.
ℹ Did you mean `dest == 1`?
#Example warning
parse_number(c("1", "$3432", "tomato"))
[1]    1 3432   NA
attr(,"problems")
# A tibble: 1 × 4
    row   col expected actual
  <int> <int> <chr>    <chr> 
1     3    NA a number tomato
#Example message
library(gridExtra)

3.3 Options for modifying figure outputs

You can control figure size and shape (see more in ?sec-plot-tips).

In particular, consider these:

out.width: What percentage of the page width should output take?

fig.height: What should be the height of figures?

fig.width: What should be the width of figures?

fig.asp: What should be the aspect ratio of figures?

fig.align: How should figures be aligned?

We might want a bigger plot for this:

And a smaller plot for this:

`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

$x
[1] "Minutes of delay"

$y
[1] "Destimations"

attr(,"class")
[1] "labels"

3.4 Changing your defaults

At the beginning of your code, you can set custom defaults so all your chunks will render the same way (unless you override by specifically adding arguments to a chunk itself). This is handy in that you will then not need to repeat the custom arguments in each code chunk. For example, you can set a default figure size.

Here is an example:

 knitr::opts_chunk$set(echo = TRUE, 
                       fig.width = 5,
                       fig.height = 3,
                       out.width = "5in", 
                       out.height = "3in", fig.align = "center")