1

我想将一个 R 表达式解析为一个列表,并在最终将其转换为 json 对象之前选择性地修改它的各个方面。作为一个例子,我试图得到类似的东西:

{"op": "=",
      "content": {
          "lhs": "gender",
          "rhs": ["male"]
      }
}

我将从一个 R 表达式开始,例如:

gender == "male"

我可以pryr::ast用来获取树的文本版本,但我想将其作为列表获取,例如:

op: "=="
  [[1]]: "gender"
  [[2]]: "male"

列表的“格式”的细节并不那么重要,只是为了清楚。我只是想获得一个可计算和可修改的 R 表达式解析树。

4

2 回答 2

4

你在找这样的东西吗?

expr <- quote(gender == "male")

expr[[1]]
# `==`
expr[[2]]
# gender
expr[[3]]
# "male"
expr[[3]] <- "female"
expr
# gender == "female"
于 2016-07-10T12:45:28.230 回答
2

这是您请求的输出部分的一种方法,使用了我评论中引用的方法的修改。这是基于 Hadley 的 pkg:pryr。请参阅?Ops中缀运算符列表。我已经在 Hadley 的高级编程文本中看到了函数lhsrhs定义了... IIRC。显然,唯一标记为“ops”的函数将是中缀数学和逻辑,但是可以使用页面中的其他列表来完成对 Math()、Complex() 和 Summary() 函数的更完整标记?groupGeneric

call_tree2(quote(gender == "male")) # relabeling of items in pryr-functions
#--------
 - call:
   - `op: ==
   - `gender
   -  "male" 

函数定义如下:

library(pryr) # also loads the stringr namespace
# although the `tree` function is not exported, you can see it with:
pryr:::tree   # now for some hacking and adding of logic
tree2<-
function (x, level = 1, width = getOption("width"), branch = " - ") 
{
    indent <- str_c(str_dup("  ", level - 1), branch)
    if (is.atomic(x) && length(x) == 1) {
        label <- paste0(" ", deparse(x)[1])
        children <- NULL
    }
    else if (is.name(x)) {
        x <- as.character(x)
        if (x == "") {
            label <- "`MISSING"
        }
        if (x %in% c("+", "-", "*", "/", "^", "%%", "%/%",
"&", "|", "!","==", "!=", "<", "<=", ">=", ">") ) {
             label <- paste0("`op: ", as.character(x))}
        else {
            label <- paste0("`", as.character(x))
        }
        children <- NULL
    }
    else if (is.call(x)) {
        label <- "call:"
        children <- vapply(as.list(x), tree2, character(1), level = level + 
            1, width = width - 3)
    }
    else if (is.pairlist(x)) {
        label <- "[]"
        branches <- paste("", format(names(x)), "=")
        children <- character(length(x))
        for (i in seq_along(x)) {
            children[i] <- tree2(x[[i]], level = level + 1, width = width - 
                3, branch = branches[i])
        }
    }
    else {
        if (inherits(x, "srcref")) {
            label <- "<srcref>"
        }
        else {
            label <- paste0("", typeof(x), "")
        }
        children <- NULL
    }
    label <- str_trunc(label, width - 3)
    if (is.null(children)) {
        paste0(indent, label)
    }
    else {
        paste0(indent, label, "\n", paste0(children, collapse = "\n"))
    }
}
environment(tree2)<-environment(pryr:::tree)

现在用 call_tree2 调用它:

pryr::call_tree
call_tree2 <- 
function (x, width = getOption("width")) 
{
    if (is.expression(x) || is.list(x)) {
        trees <- vapply(x, tree2, character(1), width = width)
        out <- str_c(trees, collapse = "\n\n")
    }
    else {
        out <- tree2(x, width = width)
    }
    cat(out, "\n")
}
environment(call_tree2)<-environment(pryr::call_tree)
于 2016-07-10T18:19:41.830 回答