magrittr点/句点(“.")运算符在管道的最开始处会做什么? [英] What does the magrittr dot/period (".") operator do when it's at the very beginning of a pipeline?

查看:71
本文介绍了magrittr点/句点(“.")运算符在管道的最开始处会做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白以下代码中的.在做什么或在哪里可以找到其文档:

I don't understand what the . in the following code is doing or where to find documentation for it:

library(tidyverse)

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

这似乎与> dplyr期间的字符.".引用?,其中.不会出现在最左侧的位置.

This appears to be behaving quite differently from the usage described in What does the dplyr period character "." reference? where the . does not appear in the left-hand-most position.

文档在这里只说

带有点(.)作为LHS的管道将创建一元函数.这是 用于定义聚合函数.

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

但这对我来说还不是很清楚,我希望获得更多信息.

but this is not at all clear to me and I'm hoping for more information.

推荐答案

这里的困惑实际上可能来自两个地方.

The confusion here can actually come from two places.

首先,是的,. %>% something()语法创建一个一元"语法.需要一个参数的函数.所以:

First, yes, the . %>% something() syntax creates a "unary" function that takes one argument. So:

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

等同于

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

这里的第二部分是ggplot2层实际上可以将函数用作其data参数.来自?geom_point:

The second part here is that ggplot2 layers can actually take a function as their data argument. From e.g. ?geom_point:

要在此层中显示的数据.共有三个选项:

...

将使用单个参数(绘图数据)调用函数.返回值必须是data.frame,并将用作图层数据.

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

So the function that is passed to geom_point will always be applied to the default plot data (i.e. the data defined in ggplot()).

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

Note that your linked question concerns the use of . in funs(), which is not directly related to it's use here.

这篇关于magrittr点/句点(“.")运算符在管道的最开始处会做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆