tidyverse / magrittr

Improve the readability of R code with the pipe

Home Page:https://magrittr.tidyverse.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

DroiPlatform opened this issue · comments

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)
###############################

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)