简体   繁体   English

%.% (dplyr) 和 %>% (magrittr) 之间的差异

[英]Differences between %.% (dplyr) and %>% (magrittr)

The dplyr package introduced the %.% operator to pass the left hand side as an argument of the function on the right hand side, similar to a *NIX pipe. dplyr包引入了%.%运算符以将左侧作为右侧函数的参数传递,类似于 *NIX 管道。 The magrittr package is a much more lightweight package that exists to define only that pipe-like operator. magrittr包是一个更轻量级的包,它的存在仅用于定义类似管道的运算符。

Yet one uses %.% and the other %>% .然而,一个使用%.% ,另一个使用%>%

Is there any difference between the two?两者之间有什么区别吗? Can I just use %>% even in dplyr code, or will that cause subtle bugs?即使在dplyr代码中,我也可以只使用%>% ,还是会导致细微的错误?

On inspecting the code, they take very different paths early on, so simple eyeball comparison would suggest that they're different.在检查代码时,它们在早期采用了截然不同的路径,因此简单的眼球比较会表明它们是不同的。 I haven't been able to find anything documented when I search the web for them, nor have I run across anything in the documentation itself.当我在网上搜索它们时,我找不到任何记录在案的内容,也没有在文档本身中找到任何内容。

See the very first item in the current NEWS file:查看当前新闻文件中的第一项:

dplyr now imports %>% from magrittr (#330). dplyr 现在从 magrittr (#330) 导入%>% I recommend that you use this instead of %.% because it is easier to type (since you can hold down the shift key) and is more flexible我建议你使用它而不是%.%因为它更容易输入(因为你可以按住 shift 键)并且更灵活

dplyr now imports %>% from magrittr and uses it by default. dplyr 现在从 magrittr 导入%>%并默认使用它。 See this answer for details.有关详细信息,请参阅此答案


Differences include差异包括

  • you can use a .你可以使用一个. as placeholder for the left-hand side, eg作为左侧的占位符,例如

     iris %>% plot(Sepal.Length ~ Sepal.Width, data = .)
  • %>% respects (rhs) , eg %>%尊重(rhs) ,例如

     1:10 %>% (call("sum")) 1:10 %>% (function(x) x^2 + 2*x)

    For a more useful example of this, see有关更有用的示例,请参阅
    https://gist.github.com/anonymous/0c69b019d0b4f6ae5050 https://gist.github.com/anonymous/0c69b019d0b4f6ae5050

  • For single argument function calls, you can omit parens:对于单参数函数调用,您可以省略括号:

     "2014-05-18" %>% as.Date

I hate to say this but the %>% should be banned. 我讨厌这样说,但是%>%应该被禁止。 No one or nothing describes what it actually does and it seems that it changes. 没有人描述了它的实际作用,似乎没有改变。 We should not have crap like this as it is too confusing. 我们不应该这样胡扯,因为它太混乱了。 Every explanation i keep reading contradicts or makes no sense. 我一直在阅读的每种解释都矛盾或没有道理。 Can we just get rid of this operator or whatever it is? 我们能摆脱这个运算符或它是什么吗? I use R a lot and find every other function or operator as self explanatory. 我经常使用R,并找到其他所有函数或运算符作为自我解释。 This is garbage and just makes it unusable until someone can either explain it clearly or standardize it. 这是垃圾,只会使它无法使用,直到有人可以清楚地说明或标准化它。 Best just to get rid of it until then. 最好只是在那之前摆脱它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM