Text Formatting

RMarkdown documents are just Markdown documents. You can format text the same way you would in plain Markdown.

For more options, see this page on Markdown syntax or the RMarkdown cheatsheet.

RMarkdown Options

Overall document options

You have probably seen the odd chunk I often include in the top of my lecture documents, which has something like knitr::opts_chunk$set(echo = TRUE) in it. This code sets the overall document options, so I can do something for every chunk in my document.

Particular chunk options

You can set the same options on a per-chunk basis, by putting the options in the chunk header, like ```{r, eval=FALSE}

Important options to know about

  • message=FALSE stops packages like mosaic from printing all their messages when they load
  • warning=FALSE stops packages from displaying warnings if there is a version conflict
  • error=FALSE can be used to make a document knit even if there is a problem in one chunk (that chunk will just run and print its error)
  • echo=TRUE shows the code, where echo=FALSE would hide the code.
  • eval=TRUE means to evaluate (run) the code, where eval=FALSE will just show the code but not evalutate it.
  • cache=TRUE see below for “shortening knit time.”

The RMarkdown cheatsheet has information on many more options, and Yihui (who wrote knitr) has a great website with more verbose information.

Shortening knit time

[This is one of those, “with great power comes great responsibility” tips.]

If you set cache=TRUE in a chunk’s options (or in your overall document options), R will “cache” the results (basically, save them) so they don’t get run every time. This is awesome if you are not changing your code anymore, just trying to get the document to look nice. If you do change something about the code, R will realize and re-run just the chunk or chunks that got changed, and save those new results.

The issue is that sometimes the cache does not behave how you expect, so it can lead to debugging errors that are hard to track down. I recommend setting it for your document overall when you are basically done with the code (maybe just changing models around), and then turning the option off before you knit for the final time. This will allow R to run through everything one last time to ensure all the results work out like you want them to.