如何将AST作为R中的列表获取 [英] How to get AST as a list in R
本文介绍了如何将AST作为R中的列表获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有描述数学公式的字符串,我想将其转换为有意义的部分列表.函数 ast _
确实知道如何解析它,将其显示为抽象语法树,但不返回AST.我正在寻找一个确实返回树的函数.
I have strings, which describe mathematical formulas and I would like to convert it into lists of meaningful parts. The function ast_
does know how to parse it, to displays it as an Abstract Syntax Tree but does not return the AST. I am looking for a function which does return the tree.
bb <- "(media.urin_A + media.urin_B)/2"
lazyeval::ast_(rlang::parse_expr(bb))
> lazyeval::ast_(rlang::parse_expr(bb))
┗ ()
┗ `/
┗ ()
┗ `(
┗ ()
┗ `+
┗ `media.urin_A
┗ `media.urin_B
┗ 2
推荐答案
您可以使用 purrr
:
getAST <- function(ee) purrr::map_if(as.list(ee), is.call, getAST)
# Example usage on expressions:
getAST( quote(log10(a+5)/b) )
# List of 3
# $ : symbol /
# $ :List of 2
# ..$ : symbol log10
# ..$ :List of 3
# .. ..$ : symbol +
# .. ..$ : symbol a
# .. ..$ : num 5
# $ : symbol b
# Example usage on strings:
getAST( str2lang("(media.urin_A + media.urin_B)/2") )
# List of 3
# $ : symbol /
# $ :List of 2
# ..$ : symbol (
# ..$ :List of 3
# .. ..$ : symbol +
# .. ..$ : symbol media.urin_A
# .. ..$ : symbol media.urin_B
# $ : num 2
这篇关于如何将AST作为R中的列表获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文