This page is dedicated to helping you to debug errors in R. Whenever you get an error the first things to check are:
library()command) that I need to do the thing I want to do?
If you are using a networked computer (for example, in one of your University’s computer labs) and when trying to knit your document to html you get an error such as:
Error: pandoc document conversion failed with error 67 Execution halted
The problem here is that networked machines often use UNC paths and pandoc (the programme that converts to html) doesn’t like them. The solution is to create a .Renviron file that specifies where the R library folder is. For University of Sussex students, follow this FAQ.
To knit to PDF you need to have LaTeX installed on your computer. This should be installed already on University of Sussex machines, but worth reading this FAQ just in case. On your own computer/laptop you will need to install something like TeX Live (for Windows) or MacTex if you use a Mac.
You might have a function name clash, that is you’re trying to use a function that exists in two different packages. For example the packages car and dplyr both have functions called
recode(). The error is likley that your code asks to use
recode() from one of the packages but R is trying to use the one from the other package. The way to avoid this is to be verbose in your code and always specify the package as well as the function. For example if you use
dplyr::recode() instead of
recode() then it is clear that you want to use
recode() from the dplyr package. To use the function from the car package you’d use
car::recode(). You can watch this short video about clashing functions:
Error: package or namespace load failed for ‘ggplot2’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]): there is no package called ‘rlang’This sort of message, the clue is in 'there is no package called ‘rlang’
If the error message says something like
there is no package called ‘package_name’ then the obvious thing to try is installing the package with
install.packages('package_name'). In this specific case
Error in ggplot(ais_15_masks, aes(mask, recognition)) : could not find function "ggplot"
If the error message says something like
could not find function "function_name" then it almost always means that you forgot to load the package from which the function came (in this case ggplot2, the package that contains the function
ggplot()), or you did load it but the loading failed (in which case, go back to where you tried to load it, look for an error message and debug why the package didn’t load properly). The solution is to execute (or better still, include this command in your setup code chunk):
In this specific case
> mem_plot + geom_point(colour = "#E69F00", position = position_jitter(width = 0.1, height = 0)) + stat_summary(fun.data = "mean_cl_normal", geom = "pointrange" +
+ and flashing cursor means R is expecting more commands. This usually happens when you have forgotten something like a closing bracket. So, in the above I forgot the closing bracket after “pointrange” so R is expecting more commands. Check the syntax of the instruction that you have tried to execute (especially pay attention to closing brackets).
object 'x' not found(argument) errors
These are probably the most common and relate to you mis-specifying an argument within a function. Arguments are the stuff in brackets after a function name. For example, in
goem_point(size = 5),
size = 5 is an argument. These errors can be tricky to debug because the resulting messages are often fairly unhelpful. For example:
Error: Aesthetics must be either length 1 or the same as the data (28): colourError in layer(data = data, mapping = mapping, stat = stat, geom = GeomPoint, :object 'position' not found
The clues here are the references to ‘colour’ and ‘position’ both of which were arguments within the function that was being executed. Debug these by looking for where you specified
position and check the syntax. These particular errors were thrown by this command:
geom_point(colour = #E69F00", position + position_jitter(width = 0.1, height = 0))
The first error was because of a missing
" before the hash:
colour = #E69F00"should have been:
colour = "#E69F00"
The second error was because of including a
+ instead of an
position + position_jitter(width = 0.1, height = 0)should have been: `position = position_jitter(width = 0.1, height = 0)’
In general, look for keywords in error messages and then check your syntax.
Sometimes functions throw warnings. these are not errors as such - the code executes, but the function wants to alert you to a potential problem. For example, within my tutorials the
ci() function throws a warning about not recognizing the class of data being entered into it. It assumes the data are numeric, and they are so we can ignore these warnings. The warning looks like this:
No class or unkown class. Using default calcuation.No class or unkown class. Using default calcuation.No class or unkown class. Using default calcuation.No class or unkown class. Using default calcuation.No class or unkown class. Using default calcuation.No class or unkown class. Using default calcuation.No class or unkown class. Using default calcuation.No class or unkown class. Using default calcuation
You should check these warnings before knitting to make sure they’re not important, but if they can be ignored and you want to suppress them in the knitted document, then you can do this using the knitr options in your first code chunk. The template markdown file will have a line of code something like this:
knitr::opts_chunk$set(echo = TRUE)
These are the global options for knitting. At present they just say to include the code (
echo=TRUE) unless you over-ride that in the header of the code chunk. Adding
warning = FALSE and
message = FALSE will suppress warnings and messages when you knit. So, if you change the existing line of code that sets knitr options to be:
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
the warnings will vanish from your knitted document.
Copyright © 2000-2019, Professor Andy Field.