Magrittr Pipes 的五种不同类型和使用案例是什么? %>%,%<>%,%$%,%!>% 和 %T>%?

huangapple go评论153阅读模式
英文:

What are the differences and use cases of the five Magrittr Pipes %>%, %<>%, %$%, %!>% and %T>%?

问题

Magrittr 有以下不同的管道符号:

  • %&gt;% 管道符
  • %&lt;&gt;% 赋值管道符
  • %$% 曝露管道符
  • %!&gt;% 急切管道符
  • %T&gt;% 分叉管道符

它们的区别和用途是什么?

英文:

Magrittr has those different pipes:

  • %&gt;% Pipe
  • %&lt;&gt;% Assignment pipe
  • %$% Exposition pipe
  • %!&gt;% Eager pipe
  • %T&gt;% Tee pipe

What are their differences and use cases?

答案1

得分: 4

%&gt;% 管道

将一个对象传递到一个函数或调用表达式中。

  1. library(magrittr)
  2. 1:10 %&gt;% head() # 基本用法
  3. #[1] 1 2 3 4 5 6
  4. 1:10 %&gt;% head # 也可以工作
  5. #[1] 1 2 3 4 5 6
  6. #1:3 %&gt;% approxfun(1:3, 4:6) #但在这种情况下需要空括号
  7. #Error in if (is.na(method)) stop(&quot;invalid interpolation method&quot;) :
  8. 1:3 %&gt;% approxfun(1:3, 4:6)()
  9. #[1] 4 5 6
  10. 1:10 %&gt;% head(3) # 使用lhs作为第一个参数
  11. #[1] 1 2 3
  12. &quot;Ceci n&#39;est pas une pipe&quot; %&gt;% gsub(&quot;une&quot;, &quot;un&quot;, .) # 使用点占位符
  13. #[1] &quot;Ceci n&#39;est pas un pipe&quot;
  14. 1:3 %&gt;% paste0(LETTERS[.], 0) # 当点嵌套时,lhs仍然首先放置
  15. #[1] &quot;1A0&quot; &quot;2B0&quot; &quot;3C0&quot;
  16. 1:3 %&gt;% {paste0(LETTERS[.], 0)} # 这可以用{}来避免
  17. #[1] &quot;A0&quot; &quot;B0&quot; &quot;C0&quot;

另请参阅:使用magrittr的%>%管道时不带空括号的函数有什么缺点?

%&lt;&gt;% 赋值管道

将一个对象传递到一个函数或调用表达式中,并使用结果更新lhs对象。

  1. x &lt;- -2:2
  2. x %&lt;&gt;% abs %&gt;% sort
  3. x # 0 1 1 2 2

%$% 公开管道

将lhs中的名称暴露给rhs表达式。当函数没有内置的数据参数时,这很有用。

  1. iris %$% cor(Sepal.Length, Sepal.Width)
  2. #[1] -0.1175698

另请参阅:我应该使用%$%而不是%>%吗?

%!&gt;% 急切管道

从左到右进行评估。而%&gt;%是惰性的,只在需要时评估管道表达式,%!&gt;%是急切的,在每个步骤中评估传递的输入。它也在相同的环境中评估。

  1. 0 %!&gt;% (\(x) {cat(1); x}) %!&gt;% (\(x) cat(2)) # 从左到右评估
  2. #12
  3. 0 %&gt;% (\(x) {cat(1); x}) %&gt;% (\(x) cat(2)) # 仅评估cat(2),因为第一个结果从未使用
  4. #2
  5. 1 %!&gt;% assign(&quot;a&quot;, .) # 工作
  6. a
  7. #[1] 1
  8. 0 %&gt;% assign(&quot;a&quot;, .) # 由于有额外的环境,不起作用
  9. a
  10. #[1] 1
  11. 0 %&gt;% assign(&quot;a&quot;, ., envir=parent.env(environment())) # 明确指定要在哪里评估
  12. a
  13. #[1] 0

%T&gt;% 泄漏管道

将一个值传递到一个函数或调用表达式中,并返回原始值而不是结果。当表达式用于其副作用时,比如绘图或打印时,这很有用。

  1. matrix(1:4, 2) %T&gt;% plot %&gt;% sum # sum得到与plot相同的数据
  2. #[1] 10

另请参阅:magrittr泄漏管%T>%等效

英文:

%&gt;% Pipe

Pipe an object forward into a function or call expression.

  1. library(magrittr)
  2. 1:10 %&gt;% head() # Basic use
  3. #[1] 1 2 3 4 5 6
  4. 1:10 %&gt;% head # Works also
  5. #[1] 1 2 3 4 5 6
  6. #1:3 %&gt;% approxfun(1:3, 4:6) #But in this case empty parentheses are needed
  7. #Error in if (is.na(method)) stop(&quot;invalid interpolation method&quot;) :
  8. 1:3 %&gt;% approxfun(1:3, 4:6)()
  9. #[1] 4 5 6
  10. 1:10 %&gt;% head(3) # Use with lhs as first argument
  11. #[1] 1 2 3
  12. &quot;Ceci n&#39;est pas une pipe&quot; %&gt;% gsub(&quot;une&quot;, &quot;un&quot;, .) # Using the dot place-holder
  13. #[1] &quot;Ceci n&#39;est pas un pipe&quot;
  14. 1:3 %&gt;% paste0(LETTERS[.], 0) # When dot is nested, lhs is still placed first
  15. #[1] &quot;1A0&quot; &quot;2B0&quot; &quot;3C0&quot;
  16. 1:3 %&gt;% {paste0(LETTERS[.], 0)} # This can be avoided with {}
  17. #[1] &quot;A0&quot; &quot;B0&quot; &quot;C0&quot;

See also: What are the disadvantages when using a function without empty parentheses with %>% the pipe of magrittr?.

%&lt;&gt;% Assignment pipe

Pipe an object forward into a function or call expression and update the lhs object with the resulting value.

  1. x &lt;- -2:2
  2. x %&lt;&gt;% abs %&gt;% sort
  3. x # 0 1 1 2 2

%$% Exposition pipe

Expose the names in lhs to the rhs expression. This is useful when functions do not have a built-in data argument.

  1. iris %$% cor(Sepal.Length, Sepal.Width)
  2. #[1] -0.1175698

See also: Should I use %$% instead of %>%?.

%!&gt;% Eager pipe

Evaluate from left to right. Whereas %&gt;% is lazy and only evaluates the piped expressions when needed, %!&gt;% is eager and evaluates the piped input at each step. Also it evaluates in the same environment.

  1. 0 %!&gt;% (\(x) {cat(1); x}) %!&gt;% (\(x) cat(2)) # Evaluates from left to right
  2. #12
  3. 0 %&gt;% (\(x) {cat(1); x}) %&gt;% (\(x) cat(2)) # Evaluates only cat(2) as the first result is never used
  4. #2
  5. 1 %!&gt;% assign(&quot;a&quot;, .) # Work
  6. a
  7. #[1] 1
  8. 0 %&gt;% assign(&quot;a&quot;, .) # Does not work as there is an additional environment
  9. a
  10. #[1] 1
  11. 0 %&gt;% assign(&quot;a&quot;, ., envir=parent.env(environment())) # Give explicitly where to evaluate
  12. a
  13. #[1] 0

%T&gt;% Tee pipe

Pipe a value forward into a function- or call expression and return the original value instead of the result. This is useful when an expression is used for its side-effect, say plotting or printing.

  1. matrix(1:4, 2) %T&gt;% plot %&gt;% sum # sum gets the same data like plot
  2. #[1] 10

See also: magrittr tee pipe %T>% equivalent.

huangapple
  • 本文由 发表于 2023年5月25日 03:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定