Search code examples
rdplyrnse

Non standard evaluation in dplyr: how do you indirect a function's multiple arguments?


I'm trying to write a function that can pass either one or more arguments as variables to dplyr functions. I'd like to understand how to do it generally. Programming with dplyr doesn't seem to cover the issue and some of the more autoritative documentation that you can find with Google or in vignettes (ie ?`!!!`) seems to be outdated. Below I provide an example of what I'm trying to get and where I fail:

df <- as_tibble(mtcars)


df %>% group_by(cyl, gear) %>% summarise(mpg = mean(mpg)) #This is the result I want from a function


testfunc <- function(variables) {
  df %>% group_by({{variables}}) %>% summarise(mpg = mean(mpg))
}

testfunc(cyl)                                             #It works for a single variable

testfunc(cyl, gear)                                       #It fails for multiple variables

testfunc2 <- function(variables) {                        #Second attempt
  variables <- enquos(variables)
  df %>% group_by(!!variables) %>% summarise(mpg = mean(mpg))
}

testfunc2(cyl, gear)                                       #Unused argument error
testfunc2(c(cyl, gear))                                    #Doesn't work
testfunc2(c("cyl", "gear"))                                #Doesn't work

How do you solve this problem in a general way?

Thanks!!


Solution

  • You can define a arg for the data.frame and add the ... for others variables to group by

    testfunc <- function(df,...) {
      df %>%
        group_by(...) %>%
        summarise(mpg = mean(mpg))
    }
    testfunc(mtcars,cyl,gear)