如何纯粹在基础R("基础管道")中管道?



有没有办法在基本R中管道化,而不必定义自己的函数(即"开箱即用"的东西),也不必加载任何外部包?

也就是说,一些(基础R)替代magrittr管道%>%。可能在即将推出的 R 版本中 (?

此功能在 R 4.0.3 中是否可用。如果不是,它在哪个 R 版本中找到,如果是,如何实现?

在 R 中,|>用作管道操作员。(从 4.1.0 开始)

左侧表达式lhs作为调用右侧表达式rhs的第一个自由参数插入。

mtcars |> head()                      # same as head(mtcars)
#                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
mtcars |> head(2)                     # same as head(mtcars, 2)
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

还可以将命名参数rhs调用中的占位符_一起使用,以指定要插入lhs的位置。占位符只能在rhs上出现一次。(从 4.2.0 开始)

mtcars |> lm(mpg ~ disp, data = _)
#mtcars |> lm(mpg ~ disp, _)  #Error: pipe placeholder can only be used as a named argument
#Call:
#lm(formula = mpg ~ disp, data = mtcars)
#
#Coefficients:
#(Intercept)         disp  
#   29.59985     -0.04122  

或者,在"一"之前显式命名参数:

mtcars |> lm(formula = mpg ~ disp)

如果占位符多次使用,或用作任何位置上的命名或未命名参数或禁用函数:使用(匿名)函数

mtcars |> ((.) .[.$cyl == 6,])()
#mtcars ->.; .[.$cyl == 6,]           # Alternative using bizarro pipe
#local(mtcars ->.; .[.$cyl == 6,])    # Without overwriting and keeping .
#                mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
mtcars |> ((.) lm(mpg ~ disp, .))()
#Call:
#lm(formula = mpg ~ disp, data = .)
#
#Coefficients:
#(Intercept)         disp  
#   29.59985     -0.04122
1:3 |> setNames(object = _, nm = _)
#Error in setNames(object = "_", nm = "_") : 
#  pipe placeholder may only appear once
1:3 |> ((.) setNames(., .))()
#1 2 3 
#1 2 3
1:3 |> list() |> setNames(".") |> with(setNames(., .))
#1 2 3 
#1 2 3 
#The same but over a function
._ <- (data, expr, ...) {
eval(substitute(expr), list(. = data), enclos = parent.frame())
}
1:3 |> ._(setNames(., .))
#1 2 3 
#1 2 3 

某些功能被禁用。

1:3 |> `+`(4)
#Error: function '+' not supported in RHS call of a pipe

但是有些仍然可以通过将它们放在圆括号中来调用,通过函数::调用它们,在函数中调用它或定义指向函数的链接。

1:3 |> (`+`)(4)
#[1] 5 6 7
1:3 |> base::`+`(4)
#[1] 5 6 7
1:3 |> ((.) . + 4)()
#[1] 5 6 7
fun <- `+`
1:3 |> fun(4)
#[1] 5 6 7

编写为x |> f(y)的表达式解析为f(x, y)。虽然管道中的代码是按顺序编写的,但用于评估的常规 R 语义适用。因此,管道表达式只有在rhs表达式中首次使用时才会被计算

-1 |> sqrt() |> ((x) 0)()
#[1] 0
. <- -1
. <- sqrt(.)
#Warning message:
#In sqrt(.) : NaNs produced
((x) 0)(.)
#[1] 0

x <- data.frame(a=0)
f1 <- (x) {message("IN 1"); x$b <- 1; message("OUT 1"); x}
f2 <- (x) {message("IN 2"); x$c <- 2; message("OUT 2"); x}
x|> f1() |> f2()
#IN 2
#IN 1
#OUT 1
#OUT 2
#  a b c
#1 0 1 2
f2(f1(x))
#IN 2
#IN 1
#OUT 1
#OUT 2
#  a b c
#1 0 1 2
. <- x
. <- f1(.)
#IN 1
#OUT 1
f2(.)
#IN 2
#OUT 2
#  a b c
#1 0 1 2

您可以使用bizarro管道(也是这个),它只是巧妙地使用了现有语法,不需要任何函数或包。

例如
mtcars ->.;
transform(., mpg = 2 * mpg) ->.;   # change units
lm(mpg ~., .) ->.;
coef(.)

->.;看起来像管道的地方。

在撰写本答案时,R (4.0.3) 的发布版本不包括管道运算符。

但是,正如useR!2020主题演讲中所指出的,基础|>运算符正在开发中。

来自 2020-12-15 R-devel 每日源代码的pipeOp手册页:

管道

表达式传递或管道传递lhs的结果 表达式到rhs表达式。

如果rhs表达式是调用,则lhs为 作为调用中的第一个参数插入。所以x |> f(y)是 解释为f(x, y).为避免歧义,函数rhs调用在语法上可能并不特殊,例如+if.

如果rhs表达式是function表达式,则 函数以lhs值作为其参数进行调用。 这是 当lhs需要作为参数传递时很有用rhs呼叫中的第一个。

此运算符何时会进入发布版本,或者它是否会在发布之前更改,目前尚不清楚。

最新更新