dendrapply Error: C stack usage is too close to the limit

家住魔仙堡 提交于 2019-12-12 06:22:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!