英文:
What are the differences and use cases of the five Magrittr Pipes %>%, %<>%, %$%, %!>% and %T>%?
问题
Magrittr
有以下不同的管道符号:
%>%
管道符%<>%
赋值管道符%$%
曝露管道符%!>%
急切管道符%T>%
分叉管道符
它们的区别和用途是什么?
英文:
Magrittr
has those different pipes:
%>%
Pipe%<>%
Assignment pipe%$%
Exposition pipe%!>%
Eager pipe%T>%
Tee pipe
What are their differences and use cases?
答案1
得分: 4
%>%
管道
将一个对象传递到一个函数或调用表达式中。
library(magrittr)
1:10 %>% head() # 基本用法
#[1] 1 2 3 4 5 6
1:10 %>% head # 也可以工作
#[1] 1 2 3 4 5 6
#1:3 %>% approxfun(1:3, 4:6) #但在这种情况下需要空括号
#Error in if (is.na(method)) stop("invalid interpolation method") :
1:3 %>% approxfun(1:3, 4:6)()
#[1] 4 5 6
1:10 %>% head(3) # 使用lhs作为第一个参数
#[1] 1 2 3
"Ceci n'est pas une pipe" %>% gsub("une", "un", .) # 使用点占位符
#[1] "Ceci n'est pas un pipe"
1:3 %>% paste0(LETTERS[.], 0) # 当点嵌套时,lhs仍然首先放置
#[1] "1A0" "2B0" "3C0"
1:3 %>% {paste0(LETTERS[.], 0)} # 这可以用{}来避免
#[1] "A0" "B0" "C0"
另请参阅:使用magrittr的%>%管道时不带空括号的函数有什么缺点?。
%<>%
赋值管道
将一个对象传递到一个函数或调用表达式中,并使用结果更新lhs对象。
x <- -2:2
x %<>% abs %>% sort
x # 0 1 1 2 2
%$%
公开管道
将lhs中的名称暴露给rhs表达式。当函数没有内置的数据参数时,这很有用。
iris %$% cor(Sepal.Length, Sepal.Width)
#[1] -0.1175698
另请参阅:我应该使用%$%而不是%>%吗?。
%!>%
急切管道
从左到右进行评估。而%>%
是惰性的,只在需要时评估管道表达式,%!>%
是急切的,在每个步骤中评估传递的输入。它也在相同的环境中评估。
0 %!>% (\(x) {cat(1); x}) %!>% (\(x) cat(2)) # 从左到右评估
#12
0 %>% (\(x) {cat(1); x}) %>% (\(x) cat(2)) # 仅评估cat(2),因为第一个结果从未使用
#2
1 %!>% assign("a", .) # 工作
a
#[1] 1
0 %>% assign("a", .) # 由于有额外的环境,不起作用
a
#[1] 1
0 %>% assign("a", ., envir=parent.env(environment())) # 明确指定要在哪里评估
a
#[1] 0
%T>%
泄漏管道
将一个值传递到一个函数或调用表达式中,并返回原始值而不是结果。当表达式用于其副作用时,比如绘图或打印时,这很有用。
matrix(1:4, 2) %T>% plot %>% sum # sum得到与plot相同的数据
#[1] 10
另请参阅:magrittr泄漏管%T>%等效。
英文:
%>%
Pipe
Pipe an object forward into a function or call expression.
library(magrittr)
1:10 %>% head() # Basic use
#[1] 1 2 3 4 5 6
1:10 %>% head # Works also
#[1] 1 2 3 4 5 6
#1:3 %>% approxfun(1:3, 4:6) #But in this case empty parentheses are needed
#Error in if (is.na(method)) stop("invalid interpolation method") :
1:3 %>% approxfun(1:3, 4:6)()
#[1] 4 5 6
1:10 %>% head(3) # Use with lhs as first argument
#[1] 1 2 3
"Ceci n'est pas une pipe" %>% gsub("une", "un", .) # Using the dot place-holder
#[1] "Ceci n'est pas un pipe"
1:3 %>% paste0(LETTERS[.], 0) # When dot is nested, lhs is still placed first
#[1] "1A0" "2B0" "3C0"
1:3 %>% {paste0(LETTERS[.], 0)} # This can be avoided with {}
#[1] "A0" "B0" "C0"
%<>%
Assignment pipe
Pipe an object forward into a function or call expression and update the lhs object with the resulting value.
x <- -2:2
x %<>% abs %>% sort
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.
iris %$% cor(Sepal.Length, Sepal.Width)
#[1] -0.1175698
See also: Should I use %$% instead of %>%?.
%!>%
Eager pipe
Evaluate from left to right. Whereas %>%
is lazy and only evaluates the piped expressions when needed, %!>%
is eager and evaluates the piped input at each step. Also it evaluates in the same environment.
0 %!>% (\(x) {cat(1); x}) %!>% (\(x) cat(2)) # Evaluates from left to right
#12
0 %>% (\(x) {cat(1); x}) %>% (\(x) cat(2)) # Evaluates only cat(2) as the first result is never used
#2
1 %!>% assign("a", .) # Work
a
#[1] 1
0 %>% assign("a", .) # Does not work as there is an additional environment
a
#[1] 1
0 %>% assign("a", ., envir=parent.env(environment())) # Give explicitly where to evaluate
a
#[1] 0
%T>%
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.
matrix(1:4, 2) %T>% plot %>% sum # sum gets the same data like plot
#[1] 10
See also: magrittr tee pipe %T>% equivalent.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论