Here are some common code issues that can cause trouble.



Error in x : could not find function "y": Forgetting to load a library or misspelling a function or package

If you get an error like: Error in BLANK : could not find function "BLANK"

Then you likely forgot to load the package that the function is from or the function is misspelled.

This will result in an error if we don’t also run library(tidyverse) (which contains stringr) or library(stringr).

library(tidyverse) # need this!
iris %>% pull(Species) 

This will not work because str_detect is missing a t at the end:

library(tidyverse) # need this! This can be at the top of your file
iris %>% pull(Species) %>% str_detec("setosa")

This will not work because library() is misspelled:

libary(tidyverse)
iris %>% pull(Species) %>% str_detect("setosa")

Why are my changes not taking effect? It’s making my results look weird!: Forgetting to assign your data to an object

Often we are just printing our data and not either reassigning an existing object or creating a new object.

Here we are creating a new object from an existing one:

new_tree <- trees

If we want to modify new_tree and save that modified version, then we need to reassign new_tree like so:

new_tree <- new_tree + 1

Using just this will only print the result and not actually change new_tree:

new_tree + 1
##    Girth Height Volume
## 1   10.3     72   12.3
## 2   10.6     67   12.3
## 3   10.8     65   12.2
## 4   12.5     74   18.4
## 5   12.7     83   20.8
## 6   12.8     85   21.7
## 7   13.0     68   17.6
## 8   13.0     77   20.2
## 9   13.1     82   24.6
## 10  13.2     77   21.9
## 11  13.3     81   26.2
## 12  13.4     78   23.0
## 13  13.4     78   23.4
## 14  13.7     71   23.3
## 15  14.0     77   21.1
## 16  14.9     76   24.2
## 17  14.9     87   35.8
## 18  15.3     88   29.4
## 19  15.7     73   27.7
## 20  15.8     66   26.9
## 21  16.0     80   36.5
## 22  16.2     82   33.7
## 23  16.5     76   38.3
## 24  18.0     74   40.3
## 25  18.3     79   44.6
## 26  19.3     83   57.4
## 27  19.5     84   57.7
## 28  19.9     82   60.3
## 29  20.0     82   53.5
## 30  20.0     82   53.0
## 31  22.6     89   79.0

If we forget to reassign this can cause subsequent steps to not work as expected because we will not be working with the data that has been modified.


Error: object 'X' not found: Forgetting to assign an object to start with

This error is usually caused by forgetting to assign an object to start with, or not running the piece of code that assigns an object. If you never created trees2 and try to modify it like so:

trees2 +1

You will get the error: Error: object 'trees2' not found

Make sure you run something like this, with the <- operator:

trees2 <- new_tree + 1

Why do I have a + sign instead of the cursor > in the console?: Trouble with parentheses

You may get a + sign instead of the cursor > in the console suggesting that you have not closed an open parentheses. Each instance of ( should be followed by ).

Hit the “esc” key to restore your >, then fix/rerun your code.


Error: unexpected ',' in ..., Error: unexpected ')' in ..., Error: unexpected 'X' in ...: Trouble with parentheses

This error can be caused by missing parentheses. Such as:

library(dplyr)
all_equal((trees+1, new_tree)
# should be all_equal((trees+1), new_tree)

which will result in this error: Error: unexpected ',' in "all_equal((trees+1,"

You will also often get this error if your parentheses are in the wrong place.

library(dplyr)
all_equal((trees+1, new_tree))
# correct number of parentheses but wrong placement
# should be all_equal((trees+1), new_tree)

If you have too many parentheses like this (the last one is extra)… you will get this error:

all_equal((tress+1), new_tree))

Error: unexpected ')' in "all_equal((trees+1), new_tree))"


Error: unexpected symbol in "x": Not using quotes or backticks when needed

You will need to use quotes for variable names that have spaces or unusual punctuation. It is best to avoid these and rename variables if a variable name has spaces.

df_quotes
## # A tibble: 3 × 2
##   `The Values` names
##          <dbl> <chr>
## 1            1 A    
## 2            2 B    
## 3            3 C

If we want to specifically pull out the column with the variable named The Values we need to use quotes or back ticks.

df_quotes %>% pull(names) # this works fine! no spaces in `names`
df_quotes %>% pull(The Values) # this does not work!
df_quotes %>% pull("The Values") # this works
df_quotes %>% pull(`The Values`) # this works

Error: unexpected input in "x": Copy+pasting quotation marks

If you copy paste code form somewhere with curly quotation marks, it will not work.

df_quotes %>% pull("The Values") # this works
df_quotes %>% pull(“The Values”) # this doesn't work

Error invalidate_mapping(): Forgetting to use+to addggplot2` layers

This will not work:

ggplot(data = iris, aes(x = Species, y = Petal.Length)) %>%
  geom_boxplot()

This will work:

ggplot(data = iris, aes(x = Species, y = Petal.Length)) +
  geom_boxplot()

Error in+.gg!: Using+at the start of a line, not the end of the previous line to addggplot2` layers

This will not work:

ggplot(data = iris, aes(x = Species, y = Petal.Length))
 +  geom_boxplot()

This will work:

ggplot(data = iris, aes(x = Species, y = Petal.Length)) +
  geom_boxplot()