Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reconcile .data pronouns with rlang::enquo

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)

like image 968
Arthur Carvalho Brito Avatar asked Dec 27 '25 19:12

Arthur Carvalho Brito


1 Answers

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 )
like image 197
Artem Sokolov Avatar answered Dec 30 '25 14:12

Artem Sokolov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!