tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
957 stars 157 forks source link

pipe to Filter(...) is ok but not another function #199

Closed DroiPlatform closed 4 years ago

DroiPlatform commented 5 years ago

Please take a look at the following code. Version 5 should be equivalent to version 6 But only v5 works. Not v6...

x <- list(a=1:10, b=11:20, c=21:30)
########## version 5 ##########
f2 <- function(f){function(x){x %>% f}}

ff  <- function(x){ mean(x) > 10 }
x %>% Filter(f=f2(ff))
###############################

########## version 6 ##########
# failed
f2 <- function(f){function(x){x %>% f}}

ff  <- function(x){ mean(x) > 10 }
FunIt <- function(y){Filter(f=f2(y))}
x %>% FunIt(ff)
###############################
smbache commented 5 years ago

They are not equivalent. FunIt accepts a single argument, y, yet you pass it two: x (via the pipe) and ff. So not working is the correct behavior.

So these variations of your example will work:

ff  <- function(x) { mean(x) > 10 }
FunIt <- function(y) { Filter(y, f=f2(ff)) }
x %>% FunIt

or

ff  <- function(x) { mean(x) > 10 }
FunIt <- function(y, f) { Filter(y, f=f2(f)) }
x %>% FunIt(ff)