Common errors

Nicky Wakim

Errors and troubleshooting are a huge part of coding!

A huge part of coding is learning to debug your own code and figuring out what’s going wrong.

  • Errors can feel frustrating at first, but they are R trying to help you.

We will cover:

  1. How to interpret error messages
  2. The most common types of errors and how to fix them

Error vs. Warning vs. Message

Type Meaning
Error Code stopped; something went wrong
Warning Code ran, but R is flagging something suspicious
Message Informational; not a problem

 

Example of a warning:

log(-1)  
Warning in log(-1): NaNs produced
[1] NaN
  • In this case, the code ran, but R is warning you that the result is NaN (not a number) because the logarithm of a negative number is undefined

Interpreting error messages

  • R error messages can feel cryptic, but they usually follow a pattern

Pay attention to

  • Which function is mentioned in the error
  • Keywords like not found, unexpected, unused argument, or subscript out of bounds
  • Any line numbers or column names that appear

Types of common errors

We will go through the following error messages:

  • Misspelled function or variable name
  • Mismatched parentheses
  • Function not found
  • Object not found
  • File not found
  • Subscript out of bounds
  • Wrong argument name
  • Silent logic errors

We will go through each type with real error messages and how to fix them. Keep this slide as a reference!

Misspelled function or variable name

R is case-sensitive

Mean() and mean() are different things!

Mean(c(1, 2, 3))
1
Capital M — R can’t find this function
Error in `Mean()`:
! could not find function "Mean"
mean(c(1, 2, 3)) 
[1] 2

Accidental mispelling

my_data <- data.frame(x = 1:5, y = 6:10)
sumamry(my_data)
2
Typo in the function name (sumamry instead of summary)
Error in `sumamry()`:
! could not find function "sumamry"

Mismatched parentheses

mean(c(1, 2, 3)
Error in parse(text = input): <text>:2:0: unexpected end of input
1: mean(c(1, 2, 3)
   ^
  • Missing closing ) — the console shows + waiting for more input

When the console prompt is >, R is ready for a new command. When it switches to +, a previous command is incomplete — finish typing it, or press Escape to cancel and start over.

Quick fixes:

  • Use Tab to autocomplete function names and avoid typos
  • Check that every ( has a closing )
  • Use RStudio’s bracket highlighting — click next to a ( and its match will highlight

Function not found, package not loaded

  • This usually means you typed the function name incorrectly, or forgot to install or load the package
clean_names(iris)
1
clean_names() is from janitor: if it’s not loaded, R can’t find it
Error in `clean_names()`:
! could not find function "clean_names"

Fix: make sure to load the package!

pacman::p_load(janitor)
iris_clean <- clean_names(iris)
names(iris_clean)
2
Load the package first, then call the function
[1] "sepal_length" "sepal_width"  "petal_length" "petal_width"  "species"     

You can also use :: to call it without loading the whole package

iris_clean2 <- janitor::clean_names(iris)
names(iris_clean2)
3
Recall from the Packages lesson: package::function() lets you use one function without library()
[1] "sepal_length" "sepal_width"  "petal_length" "petal_width"  "species"     

Object not found

This means R can’t find a variable you referenced. Common causes:

1. You never created the object

  • This can happen if you ran something in the console, but don’t have it in your current .qmd file when rendering
my_summary
1
This object was never created: code above may not have run
Error:
! object 'my_summary' not found

 

2. You’re missing quotes around a package or a string

install.packages(dplyr)
2
Missing quotes: R looks for an object named dplyr, not the text “dplyr”
Error:
! object 'dplyr' not found

File not found

  • This happens when R can’t locate a file you’re trying to read in
import(here("data/hrs_Data.csv"))
Error:
! No such file: /Users/wakim/Library/CloudStorage/OneDrive-OregonHealth&ScienceUniversity/Teaching/Classes/PUBH_523_26Su/PUBH_523_26Su_site/data/hrs_Data.csv

 

For now, if you run into it, check:

  • Spelling of the file name
  • Forward slashes (MAC) vs. backward slashes (Windows)
  • Correct file extension (.csv, .xlsx, .rds)
  • That you’re working inside the right R Project, so your working directory is where you think it is

Subscript out of bounds

  • This happens when you try to access an element of a vector, list, or data frame that doesn’t exist
x <- c(1, 2, 3, 4, 5)
x[3:10]
1
x is a vector of length 5
2
I try to access the 3rd to 10th elements, but some of them don’t exist.
[1]  3  4  5 NA NA NA NA NA
  • There is no error, but R returns NA for the elements that don’t exist

 

  • Here’s an example of the error wording (if an error is thrown):
x[[10]]
3
I try to access the 10th element iwth double brackets, but it doesn’t exist.
Error in `x[[10]]`:
! subscript out of bounds

Wrong argument name

  • This can happen if you misremember the argument name, or if you accidentally type it incorrectly
mean(c(1, 2, NA), remove.na = TRUE)
1
remove.na is not a real argument: R silently ignores it, and NA propagates through
[1] NA

Notice this one didn’t throw an error! It just gave you the wrong answer! Not every mistake is loud.

  • Fix: use the correct argument name
mean(c(1, 2, NA), na.rm = TRUE)
2
The correct argument is na.rm
[1] 1.5

How to find the right argument names

Run ?function_name and read the Arguments section — it lists every accepted argument, what it expects, and its default value.

?mean

Silent logic errors

  • This happens when your code runs without throwing an error, but the ‘TRUE’/‘FALSE’ output is not what you expected
0.1 + 0.2 == 0.3
1
You’d expect TRUE, but computers store decimals in binary, and 0.1 + 0.2 is actually stored as a number very slightly off from 0.3, so the comparison quietly returns FALSE
[1] FALSE

 

  • Fix: use all.equal() or a small tolerance instead of == when comparing decimals:
all.equal(0.1 + 0.2, 0.3)
1
all.equal() checks whether two numbers are “close enough,” rather than checking for exact equality
[1] TRUE

Logic errors are the sneakiest bugs. Always check your output or check an example if you’re running many calculations.

Wrap-up

  • Errors are a normal part of coding!
    • Don’t be discouraged, R is trying to help you
  • When you get an error, read it carefully and try to understand what it’s telling you
  • Recognize the most common types of errors and what might have caused them