Error in heatmap.2 (gplots)

岁酱吖の 提交于 2019-11-29 07:41:16

I'm the author of the gplots package. The 'node stack overflow' error occurs when a byte-compiled function has too many recursive calls.

In this case, it occurs because the function that plots dendrogram objects (stats:::plotNode) is implemented using a recursive algorithm and the dendrogram object is deeply nested.

Ultimately, the correct solution is to modify plotNode to use an iterative algorithm, which will prevent the recursion depth error from occuring.

In the short term, it is possible to force stats:::plotNode to be run as interpreted code rather then byte-compiled code via a nasty hack.

Here's the recipe:

## Convert a byte-compiled function to an interpreted-code function 
unByteCode <- function(fun)
    {
        FUN <- eval(parse(text=deparse(fun)))
        environment(FUN) <- environment(fun)
        FUN
    }

## Replace function definition inside of a locked environment **HACK** 
assignEdgewise <- function(name, env, value)
    {
        unlockBinding(name, env=env)
        assign( name, envir=env, value=value)
        lockBinding(name, env=env)
        invisible(value)
    }

## Replace byte-compiled function in a locked environment with an interpreted-code
## function
unByteCodeAssign <- function(fun)
    {
        name <- gsub('^.*::+','', deparse(substitute(fun)))
        FUN <- unByteCode(fun)
        retval <- assignEdgewise(name=name,
                                 env=environment(FUN),
                                 value=FUN
                                 )
        invisible(retval)
    }

## Use the above functions to convert stats:::plotNode to interpreted-code:
unByteCodeAssign(stats:::plotNode)

## Now raise the interpreted code recursion limit (you may need to adjust this,
##  decreasing if it uses to much memory, increasing if you get a recursion depth error ).
options(expressions=5e4)

## heatmap.2 should now work properly 
heatmap.2( ... )
Martin Morgan

In another post this is from stats:::midcache.dendrogram's function setmid. setmid calls itself recursively, and this recursion might be too deep -- probably the dendrogram is too dense to make any sense visually? You see where the error occurs by looking at the last few lines of traceback() after the error occurs.

To make further progress with this, you need to be able to provide a minimal reproducible example (using heatmap rather than heatmap.2, or even more refined based on your interpretation of traceback()) , perhaps by making the data file available, or by providing a recipe to simulate the data (m <- matrix(runif(1000), 40) ?) in a way that reliably reproduces the error.

This problem ("node stack overflow" error while using heatmap.2 function) occurs due to too many identical values in a specific column in your matrix, which causes recursion issue on R producing the error.

What I can suggest (which at least how I solved my very exact problem for my data) is to produce random numbers around the identical numbers and replace them with the original numbers in the matrix:

for (i in 1:nrow(my_matrix)) {
   if (my_matrix[i,my_column]=="100") { # assume i have too many 100 in my_column
      my_matrix[i,my_column]=runif(1,99,101) # replace 100 with nearby values randomly
    }
}

In this way, the heatmap is created without any issue since there are not too many identical numbers anymore, and also it virtually doesn't affect your matrix since you can choose a very small interval for random number generation around your identical value which will still reflect the original values with invisible color changes on the heatmap.

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