Evaluate call that contains another call (call within call)

后端 未结 5 1660
-上瘾入骨i
-上瘾入骨i 2021-02-19 06:41

I have encountered a snippet of code where call contains another call. For example:

a <- 1
b <- 2
# First call
foo <- quote(a + a)
# Second call (call c         


        
5条回答
  •  长情又很酷
    2021-02-19 07:22

    I found a CRAN package that can do this - oshka: Recursive Quoted Language Expansion.

    It recursively replaces quoted language calls by objects in environment.

    a <- 1
    b <- 2
    foo <- quote(a + a)
    bar <- quote(foo ^ b)
    

    So call oshka::expand(bar) gives (a + a)^b and eval(oshka::expand(bar)) returns 4. It also works with more complicated calls that @Oliver suggested:

    d <- 3
    zz <- quote(bar + d)
    oshka::expand(zz)
    # (a + a)^b + d
    

提交回复
热议问题