Let me start with an example.
library(tidyverse)
my_tbl = tibble(a = 1, b = 1, c = 1)
vars = c("a", "b")
my_tbl %>% select(vars)
## A tibble: 1 x 2
# a b
# <dbl> <dbl>
#1 1 1
my_tbl %>% select(all_of(vars))
## A tibble: 1 x 2
# a b
# <dbl> <dbl>
#1 1 1
vars = c("a", "b", "d")
my_tbl %>% select(vars)
#Error: Can't subset columns that don't exist.
#x Column `d` doesn't exist.
#Run `rlang::last_error()` to see where the error occurred.
my_tbl %>% select(all_of(vars))
#Error: Can't subset columns that don't exist.
#x Column `d` doesn't exist.
#Run `rlang::last_error()` to see where the error occurred.
As shown above, using select(vars)
and select(all_of(vars))
result in the same outputs. So when should I use select(all_of(vars))
instead of just select(vars)
?