_PLACEHOLDER最多只能在|>;管道的右侧使用一次,如果使用,则必须用作右侧调用表达式的命名参数.它不能用作未命名参数,也不能在嵌套在右侧主调用中的内部调用中使用.请参见help("|>")
.
以下是使用内置BOD数据框的一些替代方案.#3已经在另一个答案中显示了,但为了完整起见,我将其包括在内.我个人发现管道中所需的带有括号的匿名函数语法很难读懂,通常会使用其他语法之一.
library(dplyr) # filter
BOD[1, 2] <- NA
# 1 - list/with
BOD |>
list(x = _) |>
with(filter(x, complete.cases(x)))
# 2 - named function
noNA <- function(x) filter(x, complete.cases(x))
BOD |> noNA()
# 3 - anonymous function
BOD |> (\(x) filter(x, complete.cases(x)))()
# 4 - with dplyr use cur_group; pick(everything()) also works
BOD |>
filter(complete.cases(cur_group()))
# 5 - use a different function
library(tidyr)
BOD |>
drop_na()
我们也可以避免使用|>;
# 6 - uses %>% instead
BOD %>%
filter(complete.cases(.))
# 7 - avoid piping
filter(BOD, complete.cases(BOD))
# 8 - same as 7 but BOD only referenced once
x <- BOD
filter(x, complete.cases(x))
# 9 - Bizarro pipe (not really a pipe)
BOD ->.
filter(., complete.cases(.))