Indrajeet Patil
Indrajeet Patil

Reputation: 4879

using rlang to allow a list of arguments and then use it in custom function

I am writing a custom function where I want one of the arguments to take a list of variables. I have managed to use rlang and some rudimentary understanding of ... to properly read this list in the function. But I don't know how to assign this list as argument to another function (like dplyr::group_by). I am fully reproducible example below along with the final result I want.

# loading the needed libraries
library(dplyr)
library(rlang)
library(datasets)

# defining the custom function
prac.fn <- function(data, vars = ..., measure) {
  # getting the dataframe ready
  df <-
    dplyr::select(.data = data,
                  !!rlang::enquo(vars),
                  !!rlang::enquo(measure))
  # print to see if all variables are included
  print(head(df))

  # summarize by specified grouping variables
  df %>%
    dplyr::group_by(.data = ., c(!!rlang::enquo(vars))) %>%
    dplyr::summarise(mean = mean(!!rlang::enquo(measure)))

}

# use the function (doesn't work)
prac.fn(data = mtcars,
        vars = c(cyl, am),
        measure = wt)
#>                   cyl am    wt
#> Mazda RX4           6  1 2.620
#> Mazda RX4 Wag       6  1 2.875
#> Datsun 710          4  1 2.320
#> Hornet 4 Drive      6  0 3.215
#> Hornet Sportabout   8  0 3.440
#> Valiant             6  0 3.460
#> Error in mutate_impl(.data, dots): Column `c(c(cyl, am))` must be length 32 (the number of rows) or one, not 64

# output I want
mtcars %>%
  dplyr::group_by(cyl, am) %>%
  dplyr::summarise(mean = mean(wt))
#> # A tibble: 6 x 3
#> # Groups:   cyl [?]
#>     cyl    am  mean
#>   <dbl> <dbl> <dbl>
#> 1  4.00  0     2.94
#> 2  4.00  1.00  2.04
#> 3  6.00  0     3.39
#> 4  6.00  1.00  2.76
#> 5  8.00  0     4.10
#> 6  8.00  1.00  3.37

Created on 2018-02-17 by the reprex package (v0.2.0).

Upvotes: 1

Views: 355

Answers (1)

akrun
akrun

Reputation: 886928

In the group_by, after converting the 'vars' to quosure (enquo), flatten the expression with quo_squash, convert it to a list (as.list) and remove the first element ie. c, then with !!! evaluate it

prac.fn <- function(data,  vars, measure) {
    data %>%
        select(!!rlang::enquo(vars),
         !!rlang::enquo(measure)) %>%
        dplyr::group_by(!!! as.list(quo_squash(rlang::enquo(vars)))[-1]) %>%
        dplyr::summarise(mean = mean(!!rlang::enquo(measure)))      

 }

-testing

prac.fn(data = mtcars,
        vars = c(cyl, am),
         measure = wt)
# A tibble: 6 x 3
# Groups: cyl [?]
#    cyl    am  mean
#  <dbl> <dbl> <dbl>
#1  4.00  0     2.94
#2  4.00  1.00  2.04
#3  6.00  0     3.39
#4  6.00  1.00  2.76
#5  8.00  0     4.10
#6  8.00  1.00  3.37

Checking with more number of groups

prac.fn(data = mtcars,
        vars = c(cyl, am, gear),
         measure = wt)
# A tibble: 10 x 4
# Groups: cyl, am [?]
#     cyl    am  gear  mean
#   <dbl> <dbl> <dbl> <dbl>
# 1  4.00  0     3.00  2.46
# 2  4.00  0     4.00  3.17
# 3  4.00  1.00  4.00  2.11
# 4  4.00  1.00  5.00  1.83
# 5  6.00  0     3.00  3.34
# 6  6.00  0     4.00  3.44
# 7  6.00  1.00  4.00  2.75
# 8  6.00  1.00  5.00  2.77
# 9  8.00  0     3.00  4.10
#10  8.00  1.00  5.00  3.37

It is not clear whether the OP always wanted to use c() for vars argument i.e. if there is a single grouping variable, the function works if the behavior of passing the argument is the same

prac.fn(data = mtcars,
        vars = c(cyl),
         measure = wt)
#<quosure>
#  expr: ^c(cyl)
#  env:  global
# A tibble: 3 x 2
#    cyl  mean
#  <dbl> <dbl>
#1  4.00  2.29
#2  6.00  3.12
#3  8.00  4.00

But, if we have to change the behavior i.e. vars = cyl without the c() then it needs to be addressed with an if/else statement i.e.

prac.fnN <- function(data,  vars, measure) {
    vars <- as.list(quo_squash(enquo(vars)))
    vars <- if(length(vars) ==1) vars else vars[-1]
    data %>%
        select(!!! vars,
         !!rlang::enquo(measure)) %>%
        dplyr::group_by(!!! vars) %>%
        dplyr::summarise(mean = mean(!!rlang::enquo(measure))) 


 }

-testing

prac.fnN(data = mtcars,
        vars = cyl,
         measure = wt)
# A tibble: 3 x 2
#    cyl  mean
#  <dbl> <dbl>
#1  4.00  2.29
#2  6.00  3.12
#3  8.00  4.00


prac.fnN(data = mtcars,
       vars = c(cyl),
        measure = wt)
# A tibble: 3 x 2
#    cyl  mean
#  <dbl> <dbl>
#1  4.00  2.29
#2  6.00  3.12
#3  8.00  4.00


prac.fnN(data = mtcars,
        vars = c(cyl, am),
         measure = wt)
# A tibble: 6 x 3
# Groups: cyl [?]
#    cyl    am  mean
#  <dbl> <dbl> <dbl>
#1  4.00  0     2.94
#2  4.00  1.00  2.04
#3  6.00  0     3.39
#4  6.00  1.00  2.76
#5  8.00  0     4.10
#6  8.00  1.00  3.37

In addition to the above methods, the natural option would be to pass the arguments as quos/quo and then we don't have to think about enquo and other if/else

prac.fnQ <- function(data,  vars, measure) {
   stopifnot(is_quosures(vars)) 
   stopifnot(is_quosure(measure))

    data %>%
        select(!!! vars, !! measure) %>%
        dplyr::group_by(!!! vars) %>%
        dplyr::summarise(mean = mean(!! measure))   

 }

-testing

prac.fnQ(data = mtcars,
       vars = quos(cyl, am),
        measure = quo(wt))
# A tibble: 6 x 3
# Groups: cyl [?]
#    cyl    am  mean
#  <dbl> <dbl> <dbl>
#1  4.00  0     2.94
#2  4.00  1.00  2.04
#3  6.00  0     3.39
#4  6.00  1.00  2.76
#5  8.00  0     4.10
#6  8.00  1.00  3.37

If we also need to check whether the 'measure' variables (assuming that we have multiple 'measure' variables) are numeric

prac.fnQn <- function(data,  vars, measure) {
   stopifnot(is_quosures(vars)) 
   stopifnot(is_quosures(measure))

      data %>%
        select(!!! vars, !!! measure) %>%
        dplyr::group_by(!!! vars) %>%
        summarise_if(is.numeric, mean)       


 }

prac.fnQn(data = mtcars,
       vars = quos(cyl, am),
        measure = quos(wt))
# A tibble: 6 x 3
# Groups: cyl [?]
#    cyl    am    wt
#  <dbl> <dbl> <dbl>
#1  4.00  0     2.94
#2  4.00  1.00  2.04
#3  6.00  0     3.39
#4  6.00  1.00  2.76
#5  8.00  0     4.10
#6  8.00  1.00  3.37

Upvotes: 2

Related Questions