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)?
Thank you for the explanation, but actually, that doesn't answer my question.
Both all_of() and any_of() should be used when vars is a character vector of variable names.
Why should all_of(vars) be used instead of just vars where vars is a character vector of variable names? It seems to me that either of them does the same thing as shown in the example.
With tidyverse functions it is not necessary to use quotes to refer to variables. The consequence however is that ambiguity can arise when you specify a character variable.