7

I'm trying to get the heights of internal nodes of a dendrogram in a BFS order.

The utils::str function prints the dendrogram in a BFS order., so I thought I'd use that (redirect the output to a file and do some parsing on that to get the info I need).

My 'dendrogram' has 2 branches and 5902 members total download RDS file link: dendro.RDS.

When I try:

utils::str(dendro)

I get this error:

Error in getOption("OutDec") : node stack overflow
Error during wrapup: node stack overflow

I tried using a simple recursion function:

nodeHeights <- function(dendro){
  if(is.leaf(dendro))
    0
  else{
    cat(attr(dendro,"height"),"\n")
    max(nodeHeights(dendro[[1]]),nodeHeights(dendro[[2]]))+1
  }
}

But: nodeHeights(dendro)

Throws this error:

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

Any idea? Or any suggestion how to get the node heights of a dendrogram in a BFS order?

dan
  • 6,048
  • 10
  • 57
  • 125
  • In my R the probkem isn't with the capture.output but rather with utils::str(dendro) – dan May 14 '17 at 17:59

2 Answers2

2
> options(expressions=10000)
> nodeHeights(dendro)
[1] 1084

From ?options:

expressions sets a limit on the number of nested expressions that will be evaluated

vijucat
  • 2,059
  • 1
  • 15
  • 17
  • 1
    I'm aware of that fix but it's not a stable solution - there will be a dendrogram for which this will eventually crash as well. A non-recursive O(n) is what I'm looking for. – dan May 16 '17 at 21:35
1

Adding ulimit -s <high_value> in my .bashrc did the trick.

dan
  • 6,048
  • 10
  • 57
  • 125