Topic 6: Troubleshooting and Error Messages
When coding in R it is normal to spend a large part of your time dealing with error messages. This is the same whether you are a beginner or advanced R user. It can be time consuming trying to decipher these error messages and also very frustrating. Troubleshooting error messages is a skill in itself but it does get easier, promise!
Here are a few tips which will hopefully help to speed up the process. First, here’s a quick checklist, with more detail included below.
check you’ve not done something daft (spelling, comma, bracket, etc.)
really look at the error message for clues (sometimes they’re surprisingly helpful)
Restart R and try running the code again (this is often very successful)
if using an unfamiliar function - check documentation (F1)
try Googling the error message directly (Google is actually quite clever)
try simplifying your code and run smaller chunks (build from what you know works)
ask a fellow R user or the community (Slack: NHS-R or R4DS)
Most of the error messages that you see will be because there’s a typo or other minor issue like a misspelled word, or forgotten a bracket or comma, or typed one =
instead of two ==
. So quickly check over your code to see if any of these might be the culprit first. Watch out for capitalisation (case sensitivity); R does recognise upper and lower case.
Take a closer look at your error message to see if it might be helpful in working out what has gone wrong. Error messages can look a bit daunting at first. Aside from the alarming red text, the error messages often include unfamiliar words, but hidden amongst this, you will often find some more useful plain English or a hint as to what might be wrong. For example, the error messages will often let you know which line your code broke down on and there is also a visual reference shown alongside your code, indicated by a red line and cross.
Unfortunately, warnings are also printed in the same red font. In many cases, warnings can be safely ignored. Especially if the output, whether a plot or a table, otherwise looks like what you’d expect it to look like.
Restart R and do this often! There is no harm in restarting R, you will not lose any work as all of your code is written down, ready to be run again at the click of a button (or press of a keyboard shortcut). The joy of reproducibility! It is good practice to restart R often as a messy environment can cause unexpected behaviour, particularly when you are starting your analysis and still slightly undecided on the best object names to use so might change them a number of times before settling.
When trying out a new function, make sure you know how it works and have explored examples of the function “in action”.You can check the documentation (press F1 with cursor inside function name), and scroll down to the bottom to see code examples. Google will also provide you with copious examples of how a function might be used. Or another way of finding the most relevant help is to restrict your search to more recent years. For ggplot2, dplyr, assignment operators, and many other introductory R topic-related questions, there is also the new introverse package which provides beginner-oriented help to complement the official documentation.
If you have no clue why you are getting a particular error message, it can be worth copying and pasting the error messages directly into Google. This can help to give some indication as to why your code isn’t working and then you can start to refine your search to make it more relevant. Try to remove text which is specific to your own project,e.g., your specific file or variable name, although often Google is clever enough to ignore these anyway.
If you’re still struggling to work out what’s going on, you can always ask someone.A friendly colleague is best, even if they’re not an “R expert” a second pair of eyes can often spot that extra ” or missing ) you’re too tired to see.
There are several online communities for free R help:
- https://community.rstudio.com/ (search through existing questions, or post your own, everyone is really friendly there)
- https://www.rfordatasci.com/ - join the Slack group
- Alternatively, there’s a similar group for those who identify as a woman or gender minority: https://rladies.org/
- Find further events and training course from the NHS-R community: https://nhsrcommunity.com/
Follow the #rstats
hashtag on Twitter, and when posting about R on Twitter, use this hashtag so other people can find your Tweet.