Troubleshooting in R Markdown

There are a few common problems that people have had with their labs. This page will be updated with new problems when they arise, and it’s a good place to look if you’re having trouble.

Downloading the HTML so you can upload it to Moodle

To download the knitted HTML, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document’s name. Make sure it is the HTML file with the same filename as the Rmd file you were editing. Then click the More button and select Export. This will download the file onto your computer and you can then upload it to Moodle. For a short video showing this process, see here.

Document won’t knit

There could be many reasons for this. Usually, the error message will pinpoint the location of the problem. Read the error messages!!

Some most common problems are:

  • including output in your code chunks, like
mean(~speed, data = cars) 169.683
Error: <text>:1:27: unexpected numeric constant
1: mean(~speed, data = cars) 169.683
                              ^
  • including the > or + prompts that come from the console in your code chunks, like
> mean(~speed, data = cars)
Error: <text>:1:1: unexpected '>'
1: >
    ^

or

cars %>%
+ filter(speed > 100)
Error: object 'speed' not found
  • Asking R to print out an entire data set. This one is often tricky to spot, because the code will work fine in the console, saying something like [ reached getOption("max.print") -- omitted 18684 rows ], but in the RMarkdown it will try to include everything and then run out of memory. The easiest way to spot this is if you have included
  cars

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

  cars %>%
   filter(speed > 100)
  • Not including all the packages you need for your code. This error will occur if you try to use a function in a package that you haven’t loaded previously in your Markdown document.
now()
[1] "2023-07-16 15:52:55 UTC"
library(lubridate)
now()
[1] "2023-07-16 15:52:55 UTC"

For SDS classes, you often need to include

library(tidyverse)

Note that tidyverse in turn loads dplyr and ggplot2.

Note also that require() and library() are equivalent for our purposes, but library() is preferred.

  • Packages not installed. If you try to load a package that is not available in your R session, then you will get a warning.
library(imadethisup)
Error in library(imadethisup): there is no package called 'imadethisup'

To install the package, click on the “Install” button under the “Packages” tab, or use the install.packages() function.

install.packages("imadethisup")
  • Having spaces or other non-safe characters in filenames. This will often break when you try to compile your Markown document into HTML. Do not use spaces, slashes, or pound signs in filenames!!

  • Referring to objects that don’t exist. Recall that your Markdown document knits in a clean R session!! Objects in your working environment are shown under the “Environment” tab in your RStudio window. However, when you knit your Markdown document, it launches in a new, completely different R session. In order to objects to be available in that session, you have to create them. If you see an error like:

my_object
Error in eval(expr, envir, enclos): object 'my_object' not found

it means that R cannot find any object in its environment called my_object. Make sure that my_object is created in the Markdown document prior to where you are trying to access it.
One common way this can occur is if you failed to load a data frame provided by a package (that doesn’t use lazy loading). For example, the oilabs package does not support lazy loading, so you have to use the data() command to bring the data frames into your environment before using them.

library(oilabs)
head(ames)
data(ames)
head(ames)
  • Incorrect file path. Note that absolute filenames like
read.csv("/home/bbaumer/sds220/data/my_data.csv")
Warning in file(file, "rt"): cannot open file
'/home/bbaumer/sds220/data/my_data.csv': No such file or directory
Error in file(file, "rt"): cannot open the connection

will not work on anyone else’s computer. It is usually better to use a relative file path like:

read.csv("~/sds220/data/my_data.csv")

However this will only works if someone else has the same directory structure as you. Note also that the working directory of the R session in which your Markdown document knits is not necessarily the same as the working directory of the R session which you see in RStudio. Thus, if

read.csv("my_data.csv")

doesn’t work, you may have to try

read.csv("project/my_data.csv")

or something along those lines.

Code not being evaluated

If you have code showing up in your document like this

mean(~speed, data = cars)

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 always click the Chunks button at the upper right corner of the RMarkdown window and select Insert Chunk. This will do all the formatting for you.

No Knit HTML 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.

Getting more help

When in doubt, consult the authoritative source. The R Markdown website contains a detailed tutorial with videos. There are also Cheatsheets available from within RStudio. Simply go to: Help -> Cheatsheets -> R Markdown Cheat Sheet for the most commonly used R Markdown commands.