我们如何在本机R |>
管道中有条件地评估一步骤?
这篇文章展示了如何使用magrittr %>%
管道来实现。
library(dplyr)
do_thing = TRUE
# Works with magrittr syntax leading into the `{ }` expression.
x <- iris %>%
{ if (do_thing) mutate(., Sepal.Length = Sepal.Length + 100) else . } |>
select(Sepal.Length)
# Errors with native pipe leading into the `{ }`
# NB: the placeholder after `mutate(_` is updated
# Error in { :
# function '{' not supported in RHS call of a pipe (<input>:2:3)
x <- iris |>
{ if (do_thing) mutate(_, Sepal.Length = Sepal.Length + 100) else _ } |>
select(Sepal.Length)
_
。点击此处 查看完整的详细比较。 - shafeeError in { : function '{' not supported in RHS call of a pipe (<input>:2:3)
。 - Lief Esbenshade