Chapter 11 Common Error Messages

Sometimes running R code will result in red text printed to the Console. This red text is either a Message, Warning, or Error.

  • A Message is a note from the function author reminding you of something important, maybe a default value for an argument or a hint on interpreting the results.
  • A Warning is telling you that something in your function wasn’t quite correct, but R took its best guess to try to fix the problem and was able to get a result. The Warning indicates that the result might not be what you expect it to be. You’ll want to go back and check your work. Many times the code that results in a Warning is fine, but it can be made more correct or more explicit to not initiate the Warning message.
  • An Error occurs when R cannot compute the code you tried to run. Unlike with a Warning, an Error means no output was generated, and your back where you started before you ran the code.

If you write R code, you will inevitably receive an error message. That’s okay, it happens to everyone. Even expert R programmers make errors all the time. I (not that I’m the epitome of expert) make numerous errors almost every time I use R. With experience the errors occur less often, but a more important skill that grows over that time is learning to identify and fix the errors. When errors occur less often and you’re better at fixing them, the whole process becomes more enjoyable.

In this chapter we describe some errors you’re likely you’re likely to come across, why they happen, and how to fix them. Throughout, I use mydata, myfun, mypkg, mydir, and myfile as place holders for data objects, functions, packages, directories, and files, respectively.

Syntax Errors

Anytime you receive an error message it can be because of an error in your R syntax.

Spelling and Capitalization

As we’ve discussed, R is very picky with names of objects and functions. These names have to be spelled exactly right and have the correct capitalization (remember apple, Apple, and APPLE are three different things in R). If you ever run a line of code with a mis-spelled object, you’ll get the error

Error: object 'mydata' not found

If a function name is mis-spelled or mis-capitalized, you’ll get the error

Error in myfun(mydata) : could not find function "myfun"

Quotation Marks and Brackets

Quotation marks (single and double) and brackets (round, square, and curly) can lead to their own issues. Ordering of any of these are important for correct computation and just because a line of code runs without an error doesn’t mean R is calculating what you want it to13. If you start a quotation or bracket and don’t close it with a matching quotation or bracket, R will expect more to come. The next line in your Console will start with a plus + indicating R is waiting for more input:

> ((4 + 2) * 3
+ 

In addition, if you have one too many closing brackets, you will get an error that one of them is “unexpected”:

(4+2) * 3)
Error: unexpected ')' in "(4+2) * 3)"

Could Not Find and Cannot Open

One of the more common error messages new R uses are likely to get is that R “could not find” something or that something “does not exist.” Mis-spelling or mis-capitalization is a likely culprit, but not the only one. Below are some other possibilities.

Object not Found

Error in myfun(mydata) : object 'mydata' not found

Maybe you think you saved some data or analytical result with the name mydata, but you forgot to do this. This is especially common if you are copying and pasting code or sharing code with a classmate/collaborator. Go back through your code and see where/if you create an object named mydata. You might want to re-run that part of your code.

Could not Find Function

Error in myfun(mydata) : could not find function "myfun"

If you can’t find a function, that means you haven’t loaded the package that the function is in. Determine the name package that the function is in and run library(mypkg), then try your original line of code again.

No Such File or Directory

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'mydir/mydata.R': No such file or directory

or

Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
In addition: Warning message:
In readChar(con, 5L, useBytes = TRUE) :
  cannot open compressed file 'mydir/mydata.R', probable reason 'No such file or directory'

You get errors like this when you are trying to access a file outside of R. This is usually a data file, either RData, csv, or some other format. The most likely reason for this is that your path is starting in the wrong spot: your working directory isn’t what you think it is. File paths can be very tricky for new users of any program. For more details about file paths and working directories, see Computer File Systems and Working Directories.

Another possible issue with your code is that you may be missing the file extension or using the wrong extension. For example, you may write load('mydir/mydata') when it should be load('mydir/mydata.RData').

Errors in R Markdown

When you knit an Rmd file, the messages and errors will appear in the R Markdown tab in the bottom left pane of the R Studio window. Knitting an Rmd file creates a lot of output even when everything runs as expected. This can make understanding the errors you receive more difficult to decipher.

Markdown Syntax

I’m not sure that I’ve ever received and error from my Markdown syntax (headers, lists, bold, etc.). What you will most likely see is an output will be generated, but the formatting won’t be right. For example, (1) instead of a header, you will just see a line starting with a hashtag, or (2) instead of a new paragraph you will see a two-paragraph run-on. In most of these cases, the problem is missing white space around your Markdown formatting. In the two examples I just gave, (1) you would need a space after the hashtag in order for Markdown to recognize a header and (2) you need a blank line between paragraphs.

The point here is the same with all R (and any computer) code: Just because you don’t get an error doesn’t mean you’re getting what you want.

R Code

All of the R code errors discussed above can happen in an Rmd file as well. Remember that you need to load any packages and data directly into your Rmd file.

When you encounter an error in your R code in an Rmd file, you are given a line number. This number is the first line of the code chunk in which the error occurs. Though the error may occur here, the actual problem may be in an earlier chunk. For example, maybe you mis-spelled the name of an object when you saved it, essentially creating a mis-named object. If you try to access this object in a later chunk, the error will come here instead of the incorrect naming in the earlier chunk.

In order to limit the number of errors you receive when knitting a file, some good rules of thumb are:

  • Make your code chunks as small as is reasonable, so that there are fewer lines of code to debug to find your error.
  • Knit early and often. For example, if you are working on your homework, knit after each problem. If you knit after problem 4 and everything works, then you knit after problem 5 you get an error, you know the issue is in problem 5. If you wait until you are done to knit for the first time, it will be harder to find the error. In addition, remember that when you get an error, R stops running. If you knit and receive an error, you may fix that error but the next error will pop up the next time you try to knit. This gets far more annoying than fixing errors as you go and it isn’t as beneficial from a pedagogical standpoint - seeing and fixing errors immediately after you make them will help you learn from them more quickly than writing an entire assignment then going back and fixing errors in a way that is disconnected from the original coding.

Permission denied

Even if all your code is running correctly and producing results, you might get an error in the very last stage of knitting telling you “permission denied” or “conversion failed”:

pandoc.exe: myfile.docx: openBinaryFile: permission denied (Permission denied)
Error: pandoc document conversion failed with error 1
Execution halted

This means that you have an old version of the knitted document (here, myfile.docx) open. If a file with the name you want to knit to is already open, R cannot create or edit that file. Close the old version of the file and knit again. Just remember that when you knit, you will lose that old version completely as it gets replaced with the updated knitted document. If there’s something in that old version you think you might want, rename the old file to something like myfile-old.docx before knitting the newer version of the Rmd file14.

Other Help

Antoine Soetewey has a good list of Top 10 errors in R and how to fix them [LINK]


  1. Remember PEMDAS order of operations - R follows this convention.↩︎

  2. A better system would be to use version control software, but that’s not something you’re likely to encounter in an intro stats class.↩︎