使用整洁的评估框架修改表达式内部的符号
我想使用整洁的评估框架替换函数lazyeval :: Interp()
。基本上,我有一个简单的表达式:
library(rlang)
expr((a + 5 + b)/3)
#> (a + 5 + b)/3
我想替换此表达式,以便a
和b
符号变为c
和d
/代码>分别。使用LazyeVal,这很容易:
lazyeval::interp(expr((a + 5 + b)/3), a = expr(c), b = expr(d))
#> (c + 5 + d)/3
此功能具有.values
参数允许指定列表或环境:
lazyeval::interp(expr((a + 5 + b)/3), .values=list(a = expr(c), b = expr(d)))
#> (c + 5 + d)/3
lazyeval::interp(expr((a + 5 + b)/3), .values=as.environment(list(a = expr(c), b = expr(d))))
#> (c + 5 + d)/3
我天真地认为我可以用不defced的函数rlang ::: expr_interp
- 或使用rlang :: inject()
。事实并非如此:
expr_interp(expr((a + 5 + b)/3), as.environment(list(a = expr(c), b = expr(d))))
#> (a + 5 + b)/3
我知道我可以使用基本函数替代(就像函数Interp一样),但是我想找出一种方法来修改表达式/呼叫,以便可以用符号代替。另一个具有Rlang 。
I want to replace the function lazyeval::interp()
using the tidy eval framework. Basically, I have this simple expression :
library(rlang)
expr((a + 5 + b)/3)
#> (a + 5 + b)/3
I want to replace this expression so that the a
and b
symbols become c
and d
, respectively. With lazyeval, it is quite easy :
lazyeval::interp(expr((a + 5 + b)/3), a = expr(c), b = expr(d))
#> (c + 5 + d)/3
This function has the .values
argument allowing to specify either a list or an environment:
lazyeval::interp(expr((a + 5 + b)/3), .values=list(a = expr(c), b = expr(d)))
#> (c + 5 + d)/3
lazyeval::interp(expr((a + 5 + b)/3), .values=as.environment(list(a = expr(c), b = expr(d))))
#> (c + 5 + d)/3
I naively thought that I could just replace with the deprecated function rlang::expr_interp
- or with rlang::inject()
. That's not the case:
expr_interp(expr((a + 5 + b)/3), as.environment(list(a = expr(c), b = expr(d))))
#> (a + 5 + b)/3
I know that I can use the base function substitute for that (like the function interp does), but I would like to figure a way to modify an expression/call so that a symbol could be replaced by another one with rlang.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
命名的插值超出了Tidyeval的范围。
Named interpolation is out of scope for tidyeval.