Troubleshooting and how to find help
By Gergana on November 15, 2016
In our first tutorial, we learned how to import data into
RStudio, conduct a simple analysis (calculate species richness) and plot the results. Here, we will build upon that knowledge by getting to grips with common coding errors and how to avoid them. You might have seen some of these error messages already, but after completing this tutorial, we hope they won’t appear too often on your RStudio screens.
1. Learn how to pick up on errors in R
In addition to keeping a record of your code, scripts are also useful for detecting simple coding errors before you’ve even run the code. If
RStudio picks up on a character missing, a command that doesn’t make sense due to spelling errors or similar, a little x appears next to that line of code. Scanning your code for x’s before running it is always a good idea and it’s very convenient since you know exactly on which line you made a mistake. The other way
R reports errors is through messages in the console, which appear after you run code that is not quite right. Although the error messages look scary (the red font and words like “fatal” sure give them a bad reputation), they are actually the second best option to no errors at all:
R has identified there is a problem and from the message, you can figure out what it is and solve it!
2. Get familiar with common errors and solutions
Here we have compiled a list of mistakes we often make. Do you think we have forgotten an error message or problem you encounter often? Please let us know at email@example.com and we will add it to our list!
- Your version of
RStudio is too old (or too new). If you haven’t updated
RStudio in a while, you might not be able to use some of the new packages coming out - when you try installing the package, you will get an error message saying that the package is not compatible with your version of
RStudio. This problem is quickly fixed by a visit to the RStudio website or the R website, from there you can get the most recent version. On the flip side, when you get the newest RStudio, packages that haven’t been updated recently might not work, or your old code breaks. This occurs less often and in general, code is ever evolving and getting better and better, so it’s good to keep up to date with the latest versions of both RStudio and R packages.
- Syntax errors. The easiest mistakes to make! You’ve forgotten a comma, opened a bracket, but haven’t closed it, added an extra 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 it. If you can’t pinpoint the correct way to code what you need, there are many places to find help. Following a Coding Etiquette can help you keep these errors to a minimum.
- You’re trying to use a certain function and R doesn’t recognise it.
First, it’s worth checking whether you have installed and loaded the package the function comes from - running the code
?filter will display a help screen with information on how you use the function, as well as the package it comes from.
If you have loaded several similar packages from your library, they might contain different functions with the same name and your code might break if
R is confused as to which one to use - running
dplyr::filter will return information on the function in the console. Note that
R will try to add
() at the end of
dplyr::filter. Delete them and run the code.
If you are reading up on
R online, or copying and modifying code, you might be using a function from a new package without knowing. If it looks unfamiliar, googling its name with “r package” might reveal its origin. Sometimes packages depend on other packages to run. Often those get installed automatically when you install the package, but sometimes you get an error message asking you to install another package, easily solved by
- Function breakdown and debugging. If you are running self made functions or
for loops, you might need to go through R’s traceback/debug browser. You can find help on RStudio’s Debugging Support Page.
- Missing objects. Running tests and plotting data are often hindered by R failing to find the object it’s meant to analyse. When that happens, first check that your object names are correct: spelling mistakes (capital and lower case letters, wrong letters, etc.) can all make objects unrecognisable. In this code
e <- length(unique(FloweringPlants$taxonName)) I asked R to calculate species richness of flowering plants, but forgot that I called the object
FloweringPlants. Remember that when you refer to a certain variable from an object using the dollar sign, the object comes first, the variable second:
- Data not in the right format. This might not necessarily result in an error message, but might lead to graphs/results that are wrong. For example, in our first tutorial we created a data frame and plotted species richness. If we had chosen a data matrix instead, that plot would have looked very different (and wrong). We use matrices when the variables are all the same type (all text, all numerical) and of the same length (same number of rows). Data frames are for when we have multiple variables of different types and vectors are for a series of numbers of the same type. If your results/plots make you feel suspicious, it’s good to go back to your data and check: did it import right into R (here is how to check), and is it in the right format?
Figure 1. An unfortunate looking barplot! The data were chosen to be a data matrix, but, because in matrices all variables are of the same type, R expects
taxa_f - the names of the different taxa - to have a numerical value, and lumps all the species richness values together in the second bar. A data frame was definitely a better choice!
- Wrong data distribution used in models. There are several reasons why models won’t converge, including the use of inappropriate distribution type. Usually we choose between normal (gaussian), binomial, Poisson, or Quasipoisson distributions, which we will learn more about in our workshops on modelling.
- R crashed! If you’ve overloaded
R, it can make a dramatic exit (bomb image and all) or sometimes it stops responding and you have to terminate the session. That’s why it’s very important to save your scripts often, but it’s better to save them as new files, e.g.
Edi_biodiv_16thNov.R, instead of overwriting the same file. That way if you want to revert back to old code or use some part of it, it’s easy to find it. This is the most basic type of version control. We can learn more about version control in our
- I am stuck in a loop of pluses! If the numbers of opening and closing brackets don’t match up,
R thinks there is more code coming. That is why, in the console, it is prompting you to add more code: every time you press enter, a new + appears. Press Escape on your keyboard to get back to the normal
> prompt in the console and check your code to find your error.
- The cursor in the script file changed from
_ and now text gets overwritten when I type. This happens when you accidentally press Insert on your keyboard and as a result when you add new text, it gets written over. Press Insert again to go back to normal.
3. Learn how to find help online
Googling the error message (along with the function or package name) is always a good start. Chances are someone has already encountered that problem and has asked about it online. If the error message is very long, try paraphrasing based on what you think the problem might be. There are several really useful online forums and websites where people ask for and receive help, such as Stackoverflow and Rbloggers.
For “how to …” type queries, a google search will often result in tutorials and there might be Youtube videos as well.
We have also compiled a “Useful links” list of helpful websites and tutorials where you can find additional help. We are very happy to answer any stats/programming questions you might have: feel free to contact us on firstname.lastname@example.org!
R won’t always tell you if you are doing something wrong: sometimes your code is correct, but you are doing the wrong type of analysis for your data. Nevertheless, making sure you avoid the common but oh so easy mistakes is a great point to start on - even the most complex of tests can be brought down by a missing comma.
Practice truly is the best way to learn how to avoid errors in
R - to get you started, we have written a purposefully wrong script - you can download the file from this Github repository. There you will find the data
edidiv.csv, as well as the wrong and right script. Can you fix all the mistakes?
Feeling ready to go one step further? Learn how to format and manipulate data in a tidy and efficient way with our tidyr and dplyr tutorial! Keen to make more graphs? Check out our data visualisation tutorial!
1. You know how
R reports errors, both in script files and in the console
2. You can solve common mistakes in
3. If you can’t figure out a solution yourself, you know where to find help
Check out this page to learn how you can get involved! We are very happy to have people use our tutorials and adapt them to their needs. We are also very keen to expand the content on the website, so feel free to get in touch if you’d like to write a tutorial!
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Subscribe to our mailing list: