Chapter 8 Troubleshooting

You can skip this chapter if:

  • You understand the difference between an R warning and an R error

  • You know the most common R errors

  • You know where you would go for help

8.1 Working independently

When you being working independently in R you will inevitably run into trouble. Maybe R doesn’t run the command you think it should, maybe it just gives you a warning.

If something goes wrong, it does not mean you are bad at R.

You have probably just made a typo, or forgotten a line of code. This isn’t a disaster.

Everyone makes mistakes in R. The important thing is figuring out how to fix them.

8.2 Common warnings

Sometimes (perhaps even ‘often’), R will give you a warning when you run a line of code. For example:

warn <- tibble(x = c(1,2,3,NA,1,2,3),
               y = c(2,3,1,2,3,1,1))

warn |> 
  ggplot(aes(x = x, y = y)) +
  geom_point()
## Warning: Removed 1 rows containing missing values (`geom_point()`).

We receive the Warning: Removed 1 rows containing missing values (geom_point).

In this case, its obvious that ggplot has had to get rid of the row of data with the NA value, and so we probably feel comfortable moving on with this warning. However, warnings can be a sign of something not behaving itself, and is usually a sign we should go back and double check our code. We might decide to move forward and ignore the warnings.

8.3 Common errors

There are a lot of common errors in R, that can usually be easily fixed10.

Errors mean that R has not been able to successfully run the code.

8.3.1 Object not found.

Sometimes you might ask R to find something that doesn’t exist, for example:

thing

Error: object 'thing' not found

The quote Error: object 'thing' not found tells us that R can’t find anything called ‘thing’ in the environment, or any of the loaded packages.

  • Check that you’ve loaded all your required packages

  • Make sure you’re spelling ‘thing’ right (and remember, R is case sensitive, ‘Thing’ might be right)

  • Check you have definitely loaded all your datasets (if you’re working in an R Markdown document you may have forgotten to run the line that tells R what ‘thing’ is.)

8.3.2 Function not found

Similarly, sometimes you might an error telling you that a function doesn’t exist.

thing(1)

Error in thing(1) : could not find function "thing"

Again, you want to:

  • Check you’ve definitely loaded the right packages

  • Make sure you’re spelling ‘thing’ right (remembering R is case sensitive…)

  • Make sure you definitely want to use a function. You can also get this error if you type x (1 / 2), even if you’ve predefined ‘x’ as a value. R thinks that opening brackets usually means using a function, so if you want to perform the calculation x * (1/2) you need to include the * symbol.

8.3.3 Unexpected symbols

There are a few errors which are all similar, such as:

mean(2,2,4,5,6))

Error: unexpected ')' in "mean(2,2,4,5,6))"

and

mean(2,2,3,5 6)

Error: unexpected numeric constant in "mean(2,2,3,5 6"

and

mean(2,2,3;5,6)

Error: unexpected ';' in "mean(2,2,3;"

These are all clues to go back through the last run chunk of code and look for typos.

8.4 Quick warnings and errors

There is a great resource from rex-analytics on errors and warnings, which I’ll display here:

Rex-analytics great infographic on R warnings and errors

Figure: 8.1: Rex-analytics great infographic on R warnings and errors

8.5 Searching for help

Of course sometimes you can’t figure out with what you already know. When this happens your next step is to google11.

Many, many people know that they should just google things. These people have it cracked. If you know how to find an answer on Google, you will be immediately more employable than the person who doesn’t.

This has even become a meme . . .

10 years of programming, stolen from /r/programmerhumor

Figure: 8.2: 10 years of programming, stolen from /r/programmerhumor

Quite a few memes actually . . .

Jerry gets rewarded for googling, stolen from /r/programmerhumor

Figure: 8.3: Jerry gets rewarded for googling, stolen from /r/programmerhumor

One more to make the point . . .

Young Thug knows how to google, stolen from /r/programmerhumor

Figure: 8.4: Young Thug knows how to google, stolen from /r/programmerhumor

The first step is to copy your error message and paste it into google.

Scroll through the answers until you see one that looks similar to yours.

Depending on how complex your code is, this might not be as simple as it seems. You may need to look at your error message for the key components.

More often than not, you will probably find yourself on stackoverflow. I have personally asked questions on there. Lots of people make it their hobby to sit on stackoverflow and help people with their code. Sometimes you might find that the writer of the package you’re working with takes the time to answer.

When you start to ask a question on stackoverflow, it gives you a really helpful set of tips. I have often started to write an answer and by the time I’ve worked through stackoverflow’s tips, I’ve figured out the answer.

8.5.1 Tips for asking for help

  1. Summarise the problem
+ What are you trying to do (what's your ideal output?)

+ What is happening at the moment (what's going wrong?)

+ What errors/warnings is R giving you?
  1. Describe what you’ve tried
+ What different methods have you tried? 
    + (Hint: if you haven't tried anything it's going to annoy people)

+ Why do you think your attempts haven't worked?
  1. Show some code
+ Share a minimum reproducible example (reprex) so people can tell exactly what you're trying. 

Even if you don’t want to ask your question on stackoverflow, these tips can be really useful.

8.6 Reproducible Examples (reprex)

I’ve talked a lot about reproducible examples or reprexes. Again, stackoverflow has great guidance on creating one of these. But we’ve actually already made one of these all the way back in chapter three when we were trying to figure out why summary was treating characters different from numerical variables.

There are three key elements of a reprex:

  1. Be minimal * Use as little code as possible to reproduce the problem * You can do this in two main ways:

    • Create a new file and add only what you need to reproduce the problem
    • Remove code bit by bit from your file until you find the part that isn’t working
    • In reprexes its often very useful to test on an inbuilt dataset in R, such as mpg or cars, because we all have access to these datasets, and it means you don’t have to send big data files to anyone (particularly if you’re working on data that shouldn’t be shared, e.g. personal data)
  2. Make sure your question is complete when you send it to someone * It sounds daft, but this happens more than you think. * The person trying to solve your problem should be able to replicate it using your code * Its a good idea to restart your R session, clear your environment, and run all your code fresh to check the problem still exists before sending it along. * Oh - and always send the actual code. Screenshots are not useful

  3. Describe the problem again before sending it * You may be surprised at how the problem has changed while troubleshooting!


  1. This section is nearly wholly stolen from David Robinson↩︎

  2. or web search of your choice↩︎