有没有办法执行以下操作
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)
Run Code Online (Sandbox Code Playgroud)
使用基管而不是 Magrittr 管?
substitute(x |> y, list(x = ex1, y = ex2))
#> Error: The pipe operator requires a function call as RHS
Run Code Online (Sandbox Code Playgroud)
错误消息实际上在这里很有帮助。对于基管,右侧始终需要括号。这样做
substitute(x |> y(), list(x = ex1, y = ex2))
# (dplyr::filter(Species == "setosa" & Sepal.Width > 4))(iris)
Run Code Online (Sandbox Code Playgroud)
确实产生了一个呼叫。然而,您可能需要更改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
Run Code Online (Sandbox Code Playgroud)