Why we can’t use. as a parameter in an anonymous function with%>%

Can someone explain to me why the following two instructions have different outputs:

library(plyr)
library(dplyr)
ll <- list(a = mtcars, b = mtcars)
# using '.' as a function parameter
llply(ll, function(.) . %>% group_by(cyl) %>% summarise(min = min(mpg)))
# using 'd' as function parameter
llply(ll, function(d) d %>% group_by(cyl) %>% summarise(min = min(mpg)))

The first case, apparently, has not even been evaluated (which I understood with the help of spelling summarise: llply(ll, function(.) . %>% group_by(cyl) %>% sumamrise(min = min(mpg)))I would not have given an error).

So, this is related to the rules for determining the area and where things are evaluated, but I really want to understand what is happening and why it is happening? I use .as an argument in anonymous functions quite often, and I was puzzled to see the result.

In short, why .doesn't it work with %>%?

+4
source share
2 answers

. ( "" ) , . , , - %>%. dplyr magrittr, magrittr , , . %>% somefunction():

lhs, , , .

, - a %>% , .

., , , , ..

llply(ll, function(.) (.) %>% group_by(cyl) %>% summarise(min = min(mpg)))

. %>% https://cran.r-project.org/web/packages/magrittr/magrittr.pdf. 8.

+1

. . ?"%>%":

, lhs rhs lhs , . . rhs, . , , lhs ! , . , iris% > % subset (1: nrow (.) %% 2 == 0) iris% > % subset (., 1: nrow (.) %% 2 == 0), . , rhs . , 1:10% > % {c (min (.), Max (.))} c (min (1:10), max (1:10)).

+3

Source: https://habr.com/ru/post/1658682/


All Articles