I'm trying to run this function with exec(). I have a workaround that uses rlang::expr and then !! but that implies changing the function to accept an expression as an argument instead of a quoted variable. Is there a way to call this function without loosing the ability to pass quoted arguments?
library(tidyverse)
calc_mean <- function(df, col) {
df %>%
summarise(mean({{ col }}))
}
#doesn't work
map(list('calc_mean'), ~exec(.x, df = mtcars, col = mpg))
#> Warning in mean.default(~structure(list(manufacturer = c("audi", "audi", :
#> argument is not numeric or logical: returning NA
#> [[1]]
#> mean(...)
#> 1 NA
Created on 2022-01-15 by the reprex package (v2.0.1)
My expected output would be the same as calling the function outside exec:
calc_mean(mtcars, mpg)
#> mean(mpg)
#> 1 20.09062