gpt4 book ai didi

Creating an expression/call using the base R pipe(使用基本R管道创建表达式/调用)

转载 作者:bug小助手 更新时间:2023-10-25 21:41:40 27 4
gpt4 key购买 nike



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

更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com