There are a few common problems that people have had with their labs and problem sets.

Document won’t knit

There could be many reasons for this, but the most common are:

Salaries %>%
   summarize(mean(salary), sd(salary))
  mean(salary) sd(salary)
1     113706.5   30289.04
> Salaries %>%
   summarize(mean(salary), sd(salary))


Salaries %>%
+   summarize(mean(salary), sd(salary))

in your document, but also dplyr expressions that aren’t saved into an object can do it, like

Salaries %>%

and often you also need


Code not being evaluated

If you have code showing up in your document like this

Salaries %>%
   summarize(mean(salary), sd(salary))

where the code isn’t colored nicely and the output isn’t showing, that’s often because your chunk delimiters weren’t specified correctly. You need three “ticks” (the key on the upper left corner of the keyboard, usually, the same one with the ~): ``` Then you need curly braces with an “r” in the middle, {r}, then a line break, then your code, and finally three more ticks.

If you’re having trouble getting the formatting correct, you can click the Insert button at the upper right corner of the RMarkdown window and select R to insert an R chunk. This will do all the formatting for you.

No Knit button

This means that RStudio doesn’t understand your document is supposed to be an RMarkdown document, often because your file extension is .txt. To fix this, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document’s name. Then select Rename and remove the .txt and make sure the extension is .Rmd.


Want your RMarkdown document to be even fancier? Try some of these tips.

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 tidyverse 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 evaluate 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.