[R Course] How to: Interpret Common Errors in R

R Courses

Interpretating common error messages.

Thierry Warin https://warin.ca/aboutme.html (HEC Montréal and CIRANO (Canada))https://www.hec.ca/en/profs/thierry.warin.html
03-25-2020

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.

Syntax vs Semantic Errors

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.

Syntax errors

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).

Semantic errors

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.

Common syntax errors

Misspellings

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.

Unmatched parenthesis

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).

Error: could not find function

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:

  1. misspelled the function name
  2. incorrectly capitalized the function name
  3. forgotten to load the library that provides this function. For example, before you use as.data.table, you have to load the data.table library with library(data.table).
  4. accidentally put a variable name before an open parentheses, perhaps meaning to perform multiplication. For instance, writing function (bar + 1) when you mean function * (bar + 1). R doesn’t treat these parentheses as implicit multiplication: it attempts to find a function called function (which doesn’t exist).

Error: object not found

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

Error : cannot open the connection

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.

Error : subscript out of bounds

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.)

Error : non-numeric argument to a binary operator

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.

Error : replacement has

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.

Error: unexpected symbol in…

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)

Error: unexpected numeric constant

It just means the value after the missing punctuation is a number

For example

x 2 instead of x = 2

TL;DR

Other common errors

Unable to open your shapefiles in your Rmd.

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.**

Conclusion

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 :)

Sources

Citation

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}
}