【发布时间】:2019-07-03 16:08:44
【问题描述】:
给定一个任意的固定表达式,我想用多个值的集合替换单个符号。例子:
Expression | Symbol | Replace with | Desired Output
-----------------------------------------------------------------------------------------
f(x, 5) | x | a = 1, b = sym, c = "char" | f(a = 1, b = sym, c = "char", 5)
g(f(h(y)), z) | y | 1, 2, 3 | g(f(h(1, 2, 3)), z)
g(f(h(y), z), z) | z | 4, x | g(f(h(y), 4, x), 4, x)
substitute() 函数很接近,但它并不是我想要的。在下面的例子中,我想把f(x)变成f(1, b = 4, c = d),但是我还没有找到合适的env参数。
substitute(
expr = f(x),
env = list(x = list(1, b = 4, c = rlang::sym("d")))
)
#> f(list(1, b = 4, c = d))
substitute(
expr = f(x),
env = list(x = as.call(c(quote(x), 1, b = 4, c = quote(d)))[-1])
)
#> f(1(b = 4, c = d))
由reprex package (v0.2.1) 于 2019 年 2 月 9 日创建
是否有可能找到一个env 使得substitute(f(x), env) 等于f(1, b = 4, c = d)?
备注:
- 以上,重要的是我们以
f(x)开头。我们不能简单地写as.call(c(quote(f), env))。 - https://github.com/ropensci/drake/issues/724 和 https://github.com/ropensci/drake/issues/726 描述了激励用例。
- 我们不能在 tidy 评估中使用
!!!,因为drake中的所有 tidy 评估都需要单独处理。 - 这篇文章替换了不太清楚的an earlier one。
【问题讨论】:
标签: r metaprogramming static-code-analysis