问题描述
我以这种方式使用 R
的 dendrapply
:
I'm using R
's dendrapply
this way:
dendrapply(dendro, function(n) utils::str(attributes(n)))
其中 dendro
是一个 'dendrogram',在高度 2 处有 2 个分支和 5902 个成员
.
运行一段时间后,它崩溃并显示此错误:
After a while that it's running it crashes with this error:
Error: C stack usage 7971524 is too close to the limit
有什么想法吗?
推荐答案
看起来您遇到了无限递归情况,这是因为您的函数中没有返回节点.如果您只是想将每个节点的属性结构打印到控制台,请在函数中返回 n
,如下所示:
It looks like you have an infinite recursion situation which has arisen because the nodes are not returned in your function. If you are just looking to print the structure of the attributes of each node to the console, return n
in the function like so:
print_attrs <- function(n){
utils::str(attributes(n))
return(n)
}
dendrapply(dendro, print_attrs)
考虑到您的树状图的大小,这似乎最终会淹没控制台.创建每个节点的属性的平面(非嵌套)列表有点棘手,但一种方法是使用超级赋值运算符 <<-
来修改父框架中的变量一个函数中的一个函数:
Given the size of your dendrogram it seems like this might end up flooding the console. To create a flat (non-nested) list of the attributes of each node is a little bit trickier, but one approach is to use the superassignment operator <<-
to modify variables in the parent frame of a function within a function:
list_attrs <- function(x){
out <- vector(mode = "list", length = attr(x, "members"))
counter <- 1
get_node_attrs <- function(n){
out[[counter]] <<- attributes(n)
counter <<- counter + 1
return(n)
}
tmp <- dendrapply(x, get_node_attrs)
return(out)
}
myattributes <- list_attrs(dendro)
注意使用<<-
时要注意不要修改全局环境中的变量.请参阅此帖子更多信息.
Note that care should be taken when using <<-
not to modify variables in the global environment. See this post for more info.
这篇关于dendrapply 错误:C 堆栈使用量太接近限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!