25.9 The problem of indirection

When you start writing functions that use dplyr verbs you rapidly hit the problem of indirection.

grouped_mean <- function(df, group_var, mean_var) {
  df |> 
    group_by(group_var) |> 
    summarize(mean(mean_var))
}
diamonds |> 
  grouped_mean(cut, carat)
## Error in `group_by()`:
## ! Must group by variables found in `.data`.
## ✖ Column `group_var` is not found.