4
Let’s say I wanted to create a function that internally uses some functions of dplyr or any package from tidyverse using this kind of syntax.
For illustration purposes:
exemplo <- function(df, predicado, sumario) {
df %>% filter(predicado) %>%
summarise(sumario)
}
Ideally this function would run like this:
exemplo(mtcars, cyl == 4, mean(wt))
But when I spin it I get it
Error in filter_impl(.data, quo) : Object 'cyl' not found
The expected result is the same obtained when we do
mtcars %>% filter(cyl == 4) %>%
summarise(mean(wt))
# mean(wt)
# 1 2.285727
So I ask: how to use the dplyr in the body of a function without giving up the power and simplicity of its syntax?
Fantástico Daniel!
– Daniel Ikenaga