magrittr dot/period (".") 运算符在管道的最开始时会做什么?

What does the magrittr dot/period (".") operator do when it's at the very beginning of a pipeline?

我不明白以下代码中的 . 是做什么的,也不知道在哪里可以找到它的文档:

library(tidyverse)

ggplot(iris) + 
  geom_point(
    aes(x=Sepal.Length, y=Sepal.Width), 
    data = . %>% filter(Species == 'setosa')
  )

这似乎与 中描述的用法完全不同,其中 . 没有出现在最左侧的位置。

文档here只是说

A pipeline with a dot (.) as LHS will create a unary function. This is used to define the aggregator function.

但这对我来说一点都不清楚,我希望获得更多信息。

这里的混淆实际上可以来自两个地方。

首先,是的,. %>% something() 语法创建了一个接受一个参数的“一元”函数。所以:

. %>% filter(Species == 'setosa')

等同于

function(.) filter(., Species == 'setosa')

这里的第二部分是 ggplot2 层实际上可以将函数作为它们的 data 参数。来自例如?geom_point:

The data to be displayed in this layer. There are three options:

...

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data.

因此传递给 geom_point 的函数将始终应用于默认绘图数据(即 ggplot() 中定义的数据)。

请注意,您的链接问题涉及 funs(). 的使用,这与其在此处的使用没有直接关系。