Modify symbols inside an expression using the tidy eval framework

Viewed 31

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.

1 Answers

Named interpolation is out of scope for tidyeval.

Related