Problem Part 2: evaluation

  • Functions need to take data frame columns as arguments…
  • … but need to evaluate those names at the proper time and in proper context:
    • upon execution of the function
    • in the data (frame) environment
my_mean <- function(data, var1, var2) {
  dplyr::summarise(data, mean(var1 + var2))
}

my_mean(mtcars, cyl, am)
#> Error in `dplyr::summarise()`:
#> i In argument: `mean(var1 + var2)`.
#> Caused by error:
#> ! object 'cyl' not found