替换引用的参数

jan*_*cki 4 r

寻找一种在引用语言对象上使用替换作为表达式的方法.
substitute期望得到懒惰的表达expr.
要目标是代替.exprexpr.template其中是基于元数据动态地生成一个语言对象.

## 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)
Run Code Online (Sandbox Code Playgroud)

如果没有更多的规范方法来处理它,我会感到惊讶.基础R溶液是优选的.

G. *_*eck 6

用途do.call:

do.call("substitute", list(expr.template, list(.expr = expr)))
## g(x <- f(10), flag = TRUE)
Run Code Online (Sandbox Code Playgroud)