R
errors, troubleshooting, and getting helpBy the end of today you will…
We will use the packages below.
library(tidyverse)
R
or R Markdown
will provide an error message if a problem with a computation occurs.
Encountering errors is extremely common and figuring out how to fix them is frustrating and time-consuming. But errors are helpful! They provide you with information that will help you fix your code.
Scenario: You just finished your assignment (or so you thought). You click knit to PDF and get an error. Worse yet you try to run individual code chunks to debug the error and even more errors are showing up!
Step #0: Relax. You can solve this!
Step #1: Find the relevant code.
Navigate to the code chunk where the error or problem occurred. If the error occurred while running a code chunk this is already done. If the error occurred while knitting, R
usually provides a line number. If possible, navigate to the code chunk in question so you can see both the code and error at the same time.
Step #2: Read the error.
Pause and read the error carefully and in full. What does it say in plain English? Usually, there is enough information provided to both diagnose and fix the problem.
Below are some common errors that have enough information to fix the problem.
...could not find function...
R
can’t find a function that you used. Did you include a code chunk loading the necessary packages? If you did is eval = FALSE
included as a code chunk option?
...object 'some-name-of-object' not found...
R
can’t find some-name-of-object
. Is some-name-of-object
spelled and formatted consistently, including correct capitalization? Was some-name-of-object
actually created in a code chunk where eval
is not set to FALSE
? Is the code chunk located above the code chunk where the error occurred? Is it stored via <-
? The “Environment” pane can be helpful here.
...unexpected 'some-symbol' in...
Here some-symbol
can be a comma, parenthesis, bracket, etc. This means the code you are running is not correct syntactically. Do you have an extra comma, parenthesis, bracket, or other symbol? Did you make a typo?
Error: attempt to use zero-length variable name
This generally happens when you highlight the backticks in the code chunk in addition to the code. It can also occur if there is a stray %>%
or +
at the end of a code chunk.
...requires the following missing aesthetics...
ggplot()
is missing a necessary aesthetic. Is the ggplot()
formatted correctly? Is the aesthetic provided consistent with what is required?
object of type 'closure' is not subsettable
The above error is included because it is pretty common. It (usually) means that you tried to subset a function.
non-numeric argument to binary operator
Occurs when you mix different data types in a calculation.
Often, closely reading the error will allow you to fix the problem.
Step #3: When in doubt, comment out.
Run the code line by line.
For a code chunk with a number of lines, it is sometimes tricky to tell where exactly the error is occurring. In this situation, it is helpful to run the code line-by-line to figure out where the error is occurring.
The code chunk below has a few small errors. Let’s demonstrate how running the code line-by-line helps us troubleshoot.
mpg %>%
filter(class == "subcmpct") %>%
group_by(drv)
summarize(median_cty_mpg = median(cty),
sd_cty_mpg = sd(cty),
avg_cty_mpg = average(cty))
Step #4: Examine the Documentation
If an error is from a particular function or argument, pulling up the documentation is a good step.
Documentation in R
is extremely helpful. If you want to understand what a function does, its arguments, or examples of usage, examine the documentation. In many situations, it should be higher than the fifth troubleshooting step.
Documentation can be examined using ?
or help()
.
Step 5: Google it
Generally you are not the first person to encounter a particular error in R
. A well-thought out Google search can lead to others who have encountered the same or a similar problem and potentially a solution.
Include general search terms related to the error. Include quotation marks to search for an exact phrase and include aspects of the error that are unique. If the error is with a function in a particular package (rvest
, dplyr
, etc) include that with your search. Include R
as a search term.
Avoid search terms that are specific to your current project. This includes datasets, variables, and aspects of your personal system (file paths, etc). You can exclude search terms using a “-” in Google.
You can also use advanced search operators. A helpful official Google link is here and an unofficial blog post is here. Check out partial search, domain search, and words by proximity.
StackOverflow is an extremely helpful site. Check out the R
related topics with the tag R
.
Examine a Vignette
For a broad overview of the capabilities of a package it is helpful to examine a vignette. Vignettes are “discursive documents meant to illustrate and explain facilities in [a] package” (R-Project).
Use browseVignettes()
to see vignettes from all installed packages and browseVignettes(package = some-package)
to see vignettes from a particular package. Use vignette("vignette-name")
to see a particular vignette.
Let’s try examining a vignette from a previous lecture.
Step #6: Post to Slack General. (Not during exam!)
First (if possible) push your most recent work to GitHub.
In your Sakai Forum posting, state the following:
Include both the code and error in a code chunk using the “Markdown editor”. You can create a code chunk in the same way we do in an R
Markdown document.
If possible, include a reproducible example of the error.
A reproducible example is a very small, toy example used to recreate the issue for yourself and others. Often, devising such an example helps you diagnose the issue.
Check out How to make a great R Reproducible Example and How to create a Minimal, Reproducible Example.
A reproducible example should be:
Check out the examples here and here.
The function tribble()
is helpful for creating small, toy datasets row-by-row.