为什么我们不能使用 .作为带有 %>% 的匿名函数中的参数
Why can't we use . as a parameter in an anonymous function with %>%
有人可以向我解释为什么以下两个指令有不同的输出:
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)))
前一种情况显然甚至没有被评估(我通过拼写错误 summarise
计算得出:llply(ll, function(.) . %>% group_by(cyl) %>% sumamrise(min = min(mpg)))
会 而不是 抛出错误)。
所以这与范围规则和评估事物的位置有关,但我真的很想了解发生了什么,为什么会这样?我经常在匿名函数中使用 .
作为参数,看到结果我感到很困惑。
长话短说,为什么 .
不能与 %>%
一起使用?
这似乎是因为在使用管道时特殊使用.
作为占位符。来自 ?"%>%"
:
Using the dot for secondary purposes
Often, some attribute or property
of lhs is desired in the rhs call in addition to the value of lhs
itself, e.g. the number of rows or columns. It is perfectly valid to
use the dot placeholder several times in the rhs call, but by design
the behavior is slightly different when using it inside nested
function calls. In particular, if the placeholder is only used in a
nested function call, lhs will also be placed as the first argument!
The reason for this is that in most use-cases this produces the most
readable code. For example, iris %>% subset(1:nrow(.) %% 2 == 0) is
equivalent to iris %>% subset(., 1:nrow(.) %% 2 == 0) but slightly
more compact. It is possible to overrule this behavior by enclosing
the rhs in braces. For example, 1:10 %>% {c(min(.), max(.))} is
equivalent to c(min(1:10), max(1:10)).
.
("the dot") 有多种用途,其中之一确实是作为论据。它的实际解释方式在很大程度上取决于它的上下文——在您的上下文中,它紧接在 %>%
前向管道运算符之前使用。 dplyr
从 magrittr
中获取其前向管道运算符,从 magrittr
文档中我们有以下片段,说明当有 . %>% somefunction()
:
时会发生什么
When the dot is used as lhs, the result will be a functional sequence, i.e. a function which applies the entire chain of right-hand sides in turn to its input.
所以它几乎就像一个操作顺序 - 紧跟在点之后的 %>%
会将点解释为功能序列的一部分。
将 .
理解为论点的一种方法是在其周围添加方括号,即
llply(ll, function(.) (.) %>% group_by(cyl) %>% summarise(min = min(mpg)))
要更全面地解释 .
和 %>%
的不同用途以及它们之间的相互作用,请查看 https://cran.r-project.org/web/packages/magrittr/magrittr.pdf。相关部分从第 8 页开始。
有人可以向我解释为什么以下两个指令有不同的输出:
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)))
前一种情况显然甚至没有被评估(我通过拼写错误 summarise
计算得出:llply(ll, function(.) . %>% group_by(cyl) %>% sumamrise(min = min(mpg)))
会 而不是 抛出错误)。
所以这与范围规则和评估事物的位置有关,但我真的很想了解发生了什么,为什么会这样?我经常在匿名函数中使用 .
作为参数,看到结果我感到很困惑。
长话短说,为什么 .
不能与 %>%
一起使用?
这似乎是因为在使用管道时特殊使用.
作为占位符。来自 ?"%>%"
:
Using the dot for secondary purposes
Often, some attribute or property of lhs is desired in the rhs call in addition to the value of lhs itself, e.g. the number of rows or columns. It is perfectly valid to use the dot placeholder several times in the rhs call, but by design the behavior is slightly different when using it inside nested function calls. In particular, if the placeholder is only used in a nested function call, lhs will also be placed as the first argument! The reason for this is that in most use-cases this produces the most readable code. For example, iris %>% subset(1:nrow(.) %% 2 == 0) is equivalent to iris %>% subset(., 1:nrow(.) %% 2 == 0) but slightly more compact. It is possible to overrule this behavior by enclosing the rhs in braces. For example, 1:10 %>% {c(min(.), max(.))} is equivalent to c(min(1:10), max(1:10)).
.
("the dot") 有多种用途,其中之一确实是作为论据。它的实际解释方式在很大程度上取决于它的上下文——在您的上下文中,它紧接在 %>%
前向管道运算符之前使用。 dplyr
从 magrittr
中获取其前向管道运算符,从 magrittr
文档中我们有以下片段,说明当有 . %>% somefunction()
:
When the dot is used as lhs, the result will be a functional sequence, i.e. a function which applies the entire chain of right-hand sides in turn to its input.
所以它几乎就像一个操作顺序 - 紧跟在点之后的 %>%
会将点解释为功能序列的一部分。
将 .
理解为论点的一种方法是在其周围添加方括号,即
llply(ll, function(.) (.) %>% group_by(cyl) %>% summarise(min = min(mpg)))
要更全面地解释 .
和 %>%
的不同用途以及它们之间的相互作用,请查看 https://cran.r-project.org/web/packages/magrittr/magrittr.pdf。相关部分从第 8 页开始。