Interpretating common error messages.
R is known to produce error messages that are hard to understand if you are not familiar with Nüance-R. There are some common errors every beginner have trouble understanding. This course will help you decipher some of the most common errors you see and teach you how to fix them.
Most errors in R are due to looking for something that isn’t there.
There are two main types of errors in R code:
The most common errors in Nüance-R are syntax errors. You’ve forgotten a comma, opened a bracket, but haven’t closed it, misspelled character by mistake or something else R doesn’t understand. Those are usually picked up by R and you will get error messages reminding you to proof-read your code and fix them.
Different types of bugs requires different strategies. Often, you can easily locate a syntax error by simply reading the error messages, but semantic errors pose a whole different challenge.
If you get a syntax error, then you’ve entered a command that R can’t understand. Generally the error message is pretty good about pointing to the approximate point in the command where the error is. Common syntax mistakes are missing commas, unmatched parentheses, and the wrong type of closing brace [for example, an opening square bracket but a closing parenthesis).
A semantic error occurs when a statement is syntactically valid, but does not do what the programmer intended. The code itself is correct, but the outcome of that line of code is not.
One of the most frustrating errors you can encounter in R is when you misspell the name of an object or function. R is not forgiving on this, and it won’t try to automatically figure out what you are referring to. You’ll usually be able to quite easily figure out that you made a typo because you’ll receive an object not found error.
Remember that R is also case-sensitive, so if you called an object Name and then try to call it name later on without name being defined, you’ll receive an error.
Another common error is forgetting or neglecting to finish a call to a function with a closing )
There is an extra end parenthesis in your line or an end parenthesis is missing (it’s easy to lose track of these once they start getting nested). Count and make sure that you have one close parenthesis for each open parenthesis. (The same goes for unexpected ‘]’, unexpected ‘}’ and similar errors).
This error usually occurs when a package has not been loaded into R via library, so R does not know where to find the specified function. It’s a good habit to use the library functions on all of the packages you will be using in the top R chunk in your R Markdown file, which is usually given the chunk name setup.
The function is spelled wrong or the package it belongs to has not been loaded
You tried to use a function that doesn’t exist. You might have:
This error usually occurs when your R Markdown document refers to an object that has not been defined in an R chunk at or before that chunk. You’ll frequently see this when you’ve forgotten to copy code from your R Console sandbox back into a chunk in R Markdown.
The object (variable, data frame, list, function, etc.) being called in the code has not actually been defined
Object not found
Errors of the object-not-found variety can have one of several causes:
the name is not spelled correctly, or the capitalization is wrong
the package or file containing the object is not on the search list
There can be a number of reasons we are getting this error:
A file/connection can’t be opened because R can’t find it (mostly due to an error in the path).
Failure in .onLoad() because a package can’t find a system dependency.
This error is likely to occur when :
you are using nested loops incorrectly in the code.
you try to access an array out of its boundary.
you may have a data issue and not (just) a code problem (you may need to clean your data.)
This is a simple error to decipher. This happen when we mix different vector value in calculation.
For the example
mixing data in numeric format with data in character format.
This error occurs when one tries to assign a vector of values to a subset of an existing object and the lengths do not match up.
The most common cause of this is forgetting a punctuation mark such as a comma:
For example
foo(bar1 bar2) instead of foo(bar1, bar2)
It just means the value after the missing punctuation is a number
For example
x 2 instead of x = 2
If you are not able to open your .shp file, make sure to download the .zip file and then upload it on your platform.
Remember, the .zip file must contain at least the .shp, .shx, .dbf, and .prj files components of the shapefile for your Shapefile to work properly in Nüance-R.**
Try not be intimidated by R errors. Most of the time, you will find that you are able to understand what they mean by carefully reading over them. When you can’t, carefully look over your R Markdown file again. You might also want to clear out all of your R environment and start at the top by running the chunks. Remember to only include what you deem your reader will need to follow your analysis.
Even people who have worked with R and programmed for years still use Google and support websites like Stack Overflow to ask for help with their R errors or when they aren’t sure how to do something in R. I think you’ll be pleasantly surprised at just how much support is available.
Almost all of these types of errors have a common theme: R is looking for something that isn’t there. If you look through the errors above, you’ll see R is mostly reporting that it expects to find an object, a logical value, a function, a file, a sub-object, or a dependency based on the users’ input, but that thing isn’t there. This is an important concept to convey in any “error interpretation” lesson.
The best way to learn R is by practicing. Good luck :)
I’ve got 99 problems, but dependencies ain’t one by Noam Ross https://github.com/noamross/zero-dependency-problems
Deciphering Common R Errors by Chester Ismay and Patrick C. Kennedy https://ismayc.github.io/rbasics-book/6-errors.html
Common errors and debugging R code by Tyler Clavelle https://www.tylerclavelle.com/code/2018/debug/
Impatient R by Patrick Burns https://www.burns-stat.com/documents/tutorials/impatient-r/
R Error Message Cheat Sheet by David Robinson http://varianceexplained.org/courses/errors/
Stackoverflow https://stackoverflow.com/
For attribution, please cite this work as
Warin (2020, March 25). Thierry Warin, PhD: [R Course] How to: Interpret Common Errors in R. Retrieved from https://warin.ca/posts/rcourse-howto-interpretcommonerrors/
BibTeX citation
@misc{warin2020[r, author = {Warin, Thierry}, title = {Thierry Warin, PhD: [R Course] How to: Interpret Common Errors in R}, url = {https://warin.ca/posts/rcourse-howto-interpretcommonerrors/}, year = {2020} }