rdplyrrlangquasiquotes

using quasiquotation in a function with summarize in dplyr


I'm trying to write a function that can take column names as strings assigned to variables and produce a summarized output for them like so

my_function <- function(my_df, columnA,columnB){
    

    summ_df <- my_df %>% 
        group_by(cyl) %>% 
        summarise(base_mean = mean(columnA),
                  contrast_mean = mean(columnB)) 
    
    return(summ_df)
    }


    base = "drat"
    cont = "wt"
    my_function(mtcars,base,cont)

What I would want is that the above function would return the same thing as

mtcars %>% 
    group_by(cyl) %>% 
    summarise(base_mean = mean(drat),
              contrast_mean = mean(wt)) 

I'm sure it's some combination of enexpr, or ensym, and !! but i keep getting NA values


Solution

  • Use ensym with !! so that it can take both unquoted and quoted actual arguments

    my_function <- function(my_df, columnA,columnB){
        
    
        my_df %>% 
            group_by(cyl) %>% 
            summarise(base_mean = mean(!! ensym(columnA)),
                      contrast_mean = mean(!! ensym(columnB)), .groups = 'drop' )
        
       
        }
    

    -testing

    > my_function(mtcars, !!base, !!cont)
    # A tibble: 3 × 3
        cyl base_mean contrast_mean
      <dbl>     <dbl>         <dbl>
    1     4      4.07          2.29
    2     6      3.59          3.12
    3     8      3.23          4.00