Creating expression tree in R

吃可爱长大的小学妹 提交于 2019-12-03 07:26:13

问题


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 give it to eval?

# substitute gives a tree representation of the expression
a=1; b=2;
e1 = substitute(a+2*b)
eval(e1)      #gives 5 as expected
e1            # is type language
e1[[1]]       # this is `+`
e1[[2]]       # this is 'a' type symbol
e1[[3]]       # this is type language
e1[[3]][[1]]  # this is `*`  etc....

I would like to know how I can reconstruct the e1 object programmatically. Ideally I create an object of intricated lists with the correct object in them and maybe I call some as.language on the list object. However that does not work. For instance:

# how to construct the tree?
eval(list(as.symbol('+'),1,1))                # does not return 2
eval(as.expression(list(as.symbol('+'),1,1))) # does not return 2

One way is to just generate the string '1+1' and then parse it, but it does not seem elegant to generate strings to parse them again when you have the tree in the first place!

eval(parse(text='1+1')) # does return 1, but not elegant if tree is 
                        # large and already in memory 

Thanks for your help!


回答1:


> plus <- .Primitive("+")
> plus
function (e1, e2)  .Primitive("+")
> times=.Primitive("*")
> eval(call("plus", b, call("times",2, b)))
[1] 6
> eval(call("plus", a, call("times",2, b)))
[1] 5



回答2:


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
}
> 


来源:https://stackoverflow.com/questions/8610303/creating-expression-tree-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!