Pulling your hair out because you’re getting an error in R that you can’t figure out? Are you banging your head on the table because you can’t knit? Are you throwing darts at my picture because I haven’t given you enough direction on how to code a task in R properly? This page provides some tips and fixes for dealing with some common problems in R and R Markdown.
Note that we’ve already provided a detailed list of common R Markdown issues in the assignment guidelines. We will repeat some of that here, but add a few more tips and fixes, including those for general R coding.
When you get an error in R, it will tell you in red like so: Error: ______. Note that not all red text is an error. It could be a warning or a message. You shouldn’t necessarily ignore warnings and messages, but you can still run code when you get them. With errors, you can’t run code. What are some common causes of errors?
Error in _____ : could not find function “_____”
due to a
function being misspelled. What kinds of typos are the most common?
%>%
),
plus sign (+
) or comma (,
).Did you create the object? If you get an error
that reads Error: object '____' not found
, this means that
you did not create this object. Maybe you have a spelling error. Maybe
you just simply did not create it. Look at your Environment window - do
you see the object there? Remember, if you are knitting, the code that
creates the object should be in the R Markdown file.
Check loaded packages. A misspelling could cause
the error of not finding a function. But it also could be due to
forgetting to load a package. For example, you get an error like
Error in data %>% summary() : could not find function “%>%”
when you failed to load the tidyverse package.
Functions sharing similar names. Yes, this
exists. There are two or more packages with the same exact function
name. An example of this is the function select()
which is
in the dplyr and MASS packages. When
you have both packages loaded, you will need to specify the package you
want to use for that shared function name every time you use that
function. For example, dply::select()
.
Missing an argument. Some functions require you
to specify an input for an argument. For example, type in
cut()
in your console, and you’ll get the error
Error in cut.default() : argument "x" is missing, with no default
.
Here, you need to specify an input for the argument
x =
.
Incorrect data type. Some functions require a
certain data type (or class) as an input. For example,
seq("d", "e")
will spit out the error
Error in seq.default("d", "e") : 'from' must be a finite number
because seq()
only takes in numeric data.
Problems with a installing a package. Sometimes
you will get an error installing a package. It might be a server issue
so run install.packages()
again. You might get an error
stating that another package is not installed. If the package you are
trying to install is dependent on another package also being installed,
usually the dependent packages will also be installed automatically.
However, you might try installing the dependent packages separately
first.
Problems with a package loading. Sometimes, for
inexplicable reasons, a package will uninstall spontaneously. Perhaps
it’s because the package needs to be updated (newer versions have come
out). Perhaps the R goblin stole it. Try loading the package with
library()
again. If the error message states that the
package doesn’t exist, manually install the package with
install.packages()
.
Reading in data. You might get “cannot open”
errors when trying to bring in data into R. Make sure you are pointing R
to the right folder (check using getwd()
and set the
appropriate folder using setwd()
). Make sure the file you
are trying to bring in actually exists in that folder. Make sure you
have the correct file name and extension.
All things fail, close and reopen RStudio and try again.
Read the error message. Don’t ignore what R is telling you.
The assignment guidelines goes through troubleshooting in R Markdown pretty thoroughly, but here are a few more tips to keep in mind.
Only functioning R code should be in the grey R code blocks.
Only working R code may go inside an R code block. So in these blocks you can’t have:
View()
doesn’t work in R Markdown. Remove any
View()
calls
Existence of variables and data sets. Just because a variable exists in your console doesn’t mean it exists in your .Rmd file environment. You have to copy over any code that creates/defines variables into your .Rmd file.
Error messages. Although error messages may appear cryptic, they can sometimes at least tell you where the error is. Look for the Quitting from lines part of the error message and see if you can narrow down which line the error is on.
Filename of .Rmd File. Ensure that the filename of your .Rmd file does not have any special characters at the end. Example: no files named analysis_(1).Rmd, but rather analysis.Rmd.
Make sure that any data you read in is also in the same folder as the RMarkdown.
*If you copy and paste code onto your R Markdown, the paste might introduce weird or funky characters that R Markdown does not recognize, or reads in differently. This includes code that I introduce in the lab guides. Your best approach is to default to writing out code, as opposed to copying and pasting it.
Please try the assignment on your own before turning to other people for help. If you email the professor or TA for help about a problem with your code, you must include the following:
You should ask your peers for help before you ask your instructor or TA. Relying on a single person to solve all of your problems is dangerous, because that person won’t be available throughout your career.
Read our course textbooks R for Data Science and Geocomputation with R
Look through the R Cheat Sheets we posted on Canvas (Files -> Other Resources -> R Cheatsheets)
Read the help documentation for the function you are having
trouble with. This also includes reading the package’s vignette. You can
search for package vignettes using the function
vignette()
.
Google is your friend. Copy the error message, maybe strip out anything highly specific, such as the name of your R objects, surround with quotes and Google it!
Finding answers on Google are not always quick. Add as much context as possible to your search query. For example, let’s say that I want to know how to rename a column in my dataset. I could Google: “How to rename a column in R with dplyr/tidyverse” and read the answers posted in Stacked Overflow (www.stackoverflow.com). Notice how I covered the following in my google search: (1) The specific action (how to rename a column); (2) The programming language (R statistics); (3) The specific style/technique for coding (dplyr or tidyverse package).
The following online resources are great for getting help.
Still having trouble? Maybe you need to take a break. Go for a walk. Eat a cookie. Pet your dog. Step away for a bit and look at your code from a different emotional state.
This
work is licensed under a
Creative
Commons Attribution-NonCommercial 4.0 International License.
Website created and maintained by Noli Brazil