Home > Mobile >  Follow-up: Recognition of a function input by filter in R?
Follow-up: Recognition of a function input by filter in R?

Time:10-23

In my foo() call below, is there a way for the dot_first, which is the first element captured in ..., to be recognized inside the function?

Right now, dot_first is not recognized.

library(tidyverse)
library(rlang)

foo <- function(...){
  
  dots <- rlang::list2(...)  
  dot_first <- names(dots)[1]
  
  dat <- expand_grid(...)  
  dat %>%
    filter(!!dot_first != 1)
}

foo(study = 1:2, test = LETTERS[1:2])

CodePudding user response:

You may use -

library(tidyverse)

foo <- function(...){
  dots <- list(...)  
  dot_first <- names(dots)[1]
  
  dat <- expand_grid(...)  
  dat %>%
    filter(.data[[dot_first]] != 1)
}

foo(study = 1:2, test = LETTERS[1:2])

#  study test 
#  <int> <chr>
#1     2 A    
#2     2 B    
  • Related