Background
The provided function achieves the following:
- Subsets the provided data frame using user-provided expression
- Selects the desired column
- Applies custom summary function on resulting vector and returns scalar
base
approach
summarise_filtered <-
function(df,
subset_arg,
summary_fun = c("min", "max", "median"),
select_col) {
summary_fun <- match.arg(summary_fun)
sbst_vals <-
subset.data.frame(
df,
subset = eval(parse(text = subset_arg)),
drop = TRUE,
select = eval(parse(text = select_col))
)
do.call(match.fun(summary_fun), list(sbst_vals))
}
Results
summarise_filtered(mtcars, "am == 1", "min", "cyl")
# [1] 4
summarise_filtered(mtcars, "am == 1", "max", "cyl")
# [1] 8
Challenge
I'm interested in re-writing the function above using dplyr
pipe syntax. My initial attempt fulfils the basic requirements:
summarise_filtered_dplyrish <-
function(df,
subset_arg,
summary_fun,
select_col) {
df %>%
filter({{subset_arg}}) %>%
summarise(across(.cols = {{select_col}}, .fns = summary_fun)) %>%
pull({{select_col}})
}
when called:
summarise_filtered_dplyrish(mtcars, am == 1, min, cyl)
# [1] 4
Problem
I would like for the function to work using:
summarise_filtered_dplyrish(mtcars, "am == 1", "min", "cyl")
syntax, in addition to the the already working solution. How to do this? So far, the call above generates error:
Error
Error: Problem with
filter()
input..1
. x Input..1
must be a logical vector, not a character. ℹ Input..1
is"am == 1"
. Runrlang::last_error()
to see where the error occurred.