How do I substitute symbols in a language object?

后端 未结 3 980
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 07:05

Suppose I have the following language object:

lang <- quote( f(x=a) )

and I want to substitute in 1 for a. How

相关标签:
3条回答
  • 2020-12-11 07:45

    Use do.call:

    do.call(substitute, list(lang, list(a=1)))
    

    By using do.call, we force evaluation of the name `lang` to its actual underlying value, f(x=a). Then substitution is performed on f(x=a), rather than the name `lang`.

    0 讨论(0)
  • 2020-12-11 07:58

    If you have previously defined a in some environment (.GlobalEnv) as:

    a <- 1
    

    You can generally run:

    construct(deconstruct_and_eval(lang))
    f(x = 1)
    

    For the definitions of these custom functions, see Generalized function to substitute all variables in the quote()d expression, if they exist in an environment

    0 讨论(0)
  • 2020-12-11 08:00

    You can use substituteDirect rather than substitute:

    substituteDirect(lang, list(a=1))
    

    Eventually you may be able to use substitute. According to ?substituteDirect:

    The goal is to replace this with an eval= argument to substitute.

    0 讨论(0)
提交回复
热议问题