Replace with quoted arguments

Looking for a way to use the replacement for quoted language objects as an expression.
substituteexpects to get a lazy expression in expr.
The goal is to replace .exprwith expr.template, which is a language object created 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)

Therefore, I would be surprised if there was no more canonical way to handle this. A solution of base R is preferred.

+4
source share
1 answer

Use do.call:

do.call("substitute", list(expr.template, list(.expr = expr)))
## g(x <- f(10), flag = TRUE)
+6
source

Source: https://habr.com/ru/post/1627314/


All Articles