Is there a way of doing the following
有没有办法做到以下几点
ex1 <- quote(iris)
ex2 <- quote(dplyr::filter(Species == "setosa" & Sepal.Width > 4))
substitute(x %>% y, list(x = ex1, y = ex2))
#> iris %>% filter(Species == "setosa" & Sepal.Width > 4)
using the base pipe instead of the magrittr pipe?
使用底管而不是磁管?
substitute(x |> y, list(x = ex1, y = ex2))
#> Error: The pipe operator requires a function call as RHS
更多回答
优秀答案推荐
The error message is actually quite helpful here. With the base pipe you always need parentheses on the right-hand-side. So doing
错误消息在这里实际上是非常有用的。对于基管,您总是需要在右侧添加括号。这样做
substitute(x |> y(), list(x = ex1, y = ex2))
# (dplyr::filter(Species == "setosa" & Sepal.Width > 4))(iris)
does produce a call. Then however, you will probably want to change ex2
for the call to be valid:
确实产生了一个呼叫。但是,您可能需要更改EX2才能使调用有效:
ex1 <- quote(iris)
ex2 <- quote(\(x) dplyr::filter(x, Species == "setosa" & Sepal.Width > 4))
substitute(x |> y(), list(x = ex1, y = ex2)) |> eval()
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 5.7 4.4 1.5 0.4 setosa
# 2 5.2 4.1 1.5 0.1 setosa
# 3 5.5 4.2 1.4 0.2 setosa
更多回答
我是一名优秀的程序员,十分优秀!