这是您请求的输出部分的一种方法,使用了我评论中引用的方法的修改。这是基于 Hadley 的 pkg:pryr。请参阅?Ops
中缀运算符列表。我已经在 Hadley 的高级编程文本中看到了函数lhs
并rhs
定义了... 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)