Search code examples
rexpressioneval

Evaluate values within expression function


Using the R language, if I want to plug in some real (evaluated) values in the expression and keep other parameters unresolved, what should I do? For example:

> b = 1
> a = 2
> expr = expression(b+a)
> expr
expression(b+a)
> eval(expr)
[1] 3

But what if I want the expr equal to

expression(1+a)

How can I preprocess the variable b?

Thanks so much!


Solution

  • Then you do not need to evaluate, since what you need is an expression. you need to substitute:

    a <- 2
    b <- 1
    substitute(expression(b + a),list(b=1))
    expression(1 + a)