How to use dplyr within a function?

Asked

Viewed 95 times

4

Let’s say I wanted to create a function that internally uses some functions of or any package from 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 in the body of a function without giving up the power and simplicity of its syntax?

1 answer

4


The dplyr has a very nice syntax for programming interactively and the tradeoff of this is precisely programming when we are within functions.

The best place to understand how it all works is this Vignette.

In your example, you could do so:

exemplo <- function(df, predicado, sumario) {
  predicado <- enquo(predicado)
  sumario <- enquo(sumario)

  df %>% 
    filter(!! predicado) %>% 
    summarise(!! sumario)
}

exemplo(mtcars, cyl == 4, mean(wt))

That function enquo keeps the argument passed by the user in something like a string, which can then be used inside the functions of the dplyr preceded by !!.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.