问题
I'm using R
's dendrapply
this way:
dendrapply(dendro, function(n) utils::str(attributes(n)))
where dendro
is a 'dendrogram' with 2 branches and 5902 members total, at height 2
.
After a while that it's running it crashes with this error:
Error: C stack usage 7971524 is too close to the limit
Any idea?
回答1:
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.
来源:https://stackoverflow.com/questions/43771295/dendrapply-error-c-stack-usage-is-too-close-to-the-limit