Creating expression tree in R

前端 未结 2 858
天涯浪人
天涯浪人 2021-02-02 18:27

The substitute function in R creates a language object in the form of a tree that one can parse. How can I create the tree from scratch using list or else to then g

2条回答
  •  一整个雨季
    2021-02-02 18:53

    There are a few ways you could construct R expressions programmatically. The most convenient, if it works for your case, is bquote:

    > a = 1
    > bquote(.(a) + .(a))
    1 + 1
    

    where .() is an inverse-quote. This should work for practically anything, but if it does not, there are ways to manually construct the basic building blocks of expressions:

    > as.symbol('f')
    f
    > as.call(list(quote(f), 1, 2))
    f(1, 2)
    > as.call(list(as.symbol('{'), 1, 2))
    {
        1
        2
    }
    > 
    

提交回复
热议问题