대체 기능 R
에서 구문 분석 할 수있는 트리 형식으로 언어 개체를 만듭니다. 목록을 사용하여 처음부터 트리를 만들려면 어떻게해야합니까?R에서 표현식 트리 만들기
# 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....
나는 프로그래밍 방식으로 e1
개체를 어떻게 재구성 할 수 있는지 알고 싶습니다. 이상적으로는 정확한 객체가 포함 된 복잡한 목록의 객체를 만들고 list
객체에서 as.language
을 호출 할 수 있습니다. 그러나 그것은 작동하지 않습니다. 예를 들어 :
# 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
한 가지 방법은 문자열 다음 '1 + 1'과 구문 분석을 생성하는 것입니다, 그러나 당신이 첫번째 장소에있는 나무가있을 때 다시 구문 분석 문자열을 생성하는 우아하지 않는 것 !
eval(parse(text='1+1')) # does return 1, but not elegant if tree is
# large and already in memory
도움 주셔서 감사합니다.
을 더 잘 수행하므로'call ('+ ' , e1, e2)'2 식을 더하기, 완전하게 결합하십시오! – tlamadon