在R中创建表达式树

tla*_*don 12 tree eval r substitution

替代函数以R树的形式创建一个可以解析的语言对象.如何使用列表从头开始创建树,否则将其提供给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....
Run Code Online (Sandbox Code Playgroud)

我想知道如何以e1编程方式重建对象.理想情况下,我创建了一个复杂列表的对象,其中包含正确的对象,也许我会as.languagelist对象上调用一些对象.然而,这不起作用.例如:

# 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
Run Code Online (Sandbox Code Playgroud)

一种方法是生成字符串'1 + 1'然后解析它,但是当你首先拥有树时,生成字符串以再次解析它们似乎并不优雅!

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

谢谢你的帮助!

Owe*_*wen 9

有几种方法可以编程方式构造R表达式.如果适用于您的情况,最方便的是bquote:

> a = 1
> bquote(.(a) + .(a))
1 + 1
Run Code Online (Sandbox Code Playgroud)

哪里.()是反引号.这应该适用于任何事情,但如果没有,有办法手动构建表达式的基本构建块:

> as.symbol('f')
f
> as.call(list(quote(f), 1, 2))
f(1, 2)
> as.call(list(as.symbol('{'), 1, 2))
{
    1
    2
}
> 
Run Code Online (Sandbox Code Playgroud)


42-*_*42- 7

> 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
Run Code Online (Sandbox Code Playgroud)

  • 甚至更好,所以我可以做`call('+',e1,e2)`将2表达式与加号结合起来,完美! (2认同)