我们如何有条件地评估本机 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)