+
sign instead of the cursor >
in the console?pull()
function, instead of just
using select()
?mapping
must be created by aes()
/
ℹ
Did you use ‘%>%’ or ‘|>’ instead of
‘+’?ℹ
Did you accidentally put ‘+’ on a new
line?
If you need to check what packages are part of the
tidyverse
, you can do so at this link.
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_rivers <- sample(rivers, 5)
new_rivers
## [1] 310 325 900 1770 217
Using just this will only print the result and not actually change
new_rivers
:
new_rivers + 1
## [1] 311 326 901 1771 218
If we want to modify new_rivers
and save that modified
version, then we need to reassign new_rivers
like so:
new_rivers <- new_rivers + 1
new_rivers
## [1] 311 326 901 1771 218
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.
+
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.
Check out our Guide on when to use quotes or backticks.
pull()
function,
instead of just using select()
?Check out our Guide on what functions require pulling values out first.
Forgetting to load a library or misspelling a function or package
For example, we forgot library(tidyverse)
below:
iris %>% pull(Species)
## Error in iris %>% pull(Species): could not find function "%>%"
Below 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")
## Error in str_detec(., "setosa"): could not find function "str_detec"
This will not work because library()
is misspelled:
libary(tidyverse)
## Error in libary(tidyverse): could not find function "libary"
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 rivers2
and try to modify it like so:
rivers2 + 1
## Error in eval(expr, envir, enclos): object 'rivers2' not found
Make sure you run something like this, with the <-
operator:
rivers2 <- new_rivers + 1
rivers2
## [1] 312 327 902 1772 219
Trouble with parentheses
This error can be caused by missing parentheses. Such as:
library(dplyr)
all.equal((rivers+1, rivers)
## Error: <text>:2:20: unexpected ','
## 1: library(dplyr)
## 2: all.equal((rivers+1,
## ^
It should be:
all.equal((rivers+1), rivers)
## [1] "Mean relative difference: 0.001688663"
You will also often get this error if you have the correct number of parentheses but wrong placement:
all.equal((rivers+1, rivers))
## Error: <text>:1:20: unexpected ','
## 1: all.equal((rivers+1,
## ^
If you have too many parentheses like this (the last one is extra)… you will get this error:
all_equal((rivers+1), rivers))
## Error: <text>:1:30: unexpected ')'
## 1: all_equal((rivers+1), rivers))
## ^
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`
## [1] "A" "B" "C"
df_quotes %>% pull("The Values") # this works
## [1] 1 2 3
df_quotes %>% pull(`The Values`) # this works
## [1] 1 2 3
df_quotes %>% pull(The Values) # this does not work!
## Error: <text>:1:24: unexpected symbol
## 1: df_quotes %>% pull(The Values
## ^
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
## [1] 1 2 3
df_quotes %>% pull(“The Values”) # this doesn't work
## Error: <text>:1:20: unexpected input
## 1: df_quotes %>% pull(“
## ^
mapping
must be created by
aes()
/ ℹ
Did you use ‘%>%’ or ‘|>’
instead of ‘+’?Forgetting to use +
to add ggplot2
layers
This will not work:
ggplot(data = iris, aes(x = Species, y = Petal.Length)) %>%
geom_boxplot()
## Error in `geom_boxplot()`:
## ! `mapping` must be created by `aes()`.
## ℹ Did you use `%>%` or `|>` instead of `+`?
This will work:
ggplot(data = iris, aes(x = Species, y = Petal.Length)) +
geom_boxplot()
ℹ
Did you accidentally put ‘+’
on a new line?This will not work:
ggplot(data = iris, aes(x = Species, y = Petal.Length))
+ geom_boxplot()
## Error:
## ! Cannot use `+` with a single argument.
## ℹ Did you accidentally put `+` on a new line?
This will work:
ggplot(data = iris, aes(x = Species, y = Petal.Length)) +
geom_boxplot()
To change this, you will need to adjust the RStudio Settings.
Go to the menu Tools > Global Options > Appearance. Choose a theme and apply!