Reputation: 16697
Looking for a way to use substitute on quoted language objects as expression.
substitute
expects to get lazy expression in expr
.
To goal is to substitute .expr
in expr.template
which is a language objects generated dynamically based on metadata.
## data
expr = quote(x <- f(10))
expr.template = quote(g(.expr, flag = TRUE))
## expected output
quote(g(x <- f(10), flag = TRUE))
#g(x <- f(10), flag = TRUE)
## current workaround
h = function(expr, expr.template){
eval(substitute(
substitute(
.expr.template,
list(.expr = expr)
),
list(.expr.template = expr.template)
))
}
h(expr = expr, expr.template = expr.template)
#g(x <- f(10), flag = TRUE)
So I would be surprised if there wouldn't be any more canonical way to deal with it. Base R solution preferred.
Upvotes: 4
Views: 89
Reputation: 269371
Use do.call
:
do.call("substitute", list(expr.template, list(.expr = expr)))
## g(x <- f(10), flag = TRUE)
Upvotes: 6