I am writing a package using a lot of dplyr
functions - to pass on all tests in devtools::check()
, I have to use .data
frequently. Some of the functions are nested into other functions. In the example below, I need to use variable
both in a tidyselect
context and in standard evaluation (in the part that creates id)
.
df <- data.frame(
V1 = 1:8,
V2 = rep(1:4,2)
)
test <- function(df, variable){
x <- df %>%
mutate(y = {{variable}} + 1)
id <- rlang::as_name(rlang::enquo(variable))
id_eq <- outer(df[[id]], df[[id]], `==`)
list(x, id_eq)
}
I don't know how to deal with this without getting any warnings or notes in CMD check.
The function works if I run test(df = df, variable = V1)
, but not with test(df = df, variable = .data$V1)
One option is to let tidyverse do the work of finding the correct variable. Store the output of this work into a temporary variable, and use that variable throughout your function:
test <- function(df, variable){
tmp <- df %>% mutate( .tmp = {{variable}} ) %>% pull(.tmp)
x <- df %>%
mutate(y = tmp + 1)
id_eq <- outer(tmp, tmp, `==`)
list(x, id_eq)
}
The function works with all sensible variable references, including .data
and .env
pronouns:
test( df, V1 )
test( df, .data$V1 )
V1 <- 11:18
test( df, .env$V1 )