Skip to content

Workaround for ComplexHeatmap in Positron plots pane #1265

Description

@jmw86069

The current Positron app does not render ComplexHeatmap by default, however there appears to be a relatively simple workaround.

Basic example from "Single Heatmap":

set.seed(123)
nr1 = 4; nr2 = 8; nr3 = 6; nr = nr1 + nr2 + nr3
nc1 = 6; nc2 = 8; nc3 = 10; nc = nc1 + nc2 + nc3
mat = cbind(rbind(matrix(rnorm(nr1*nc1, mean = 1,   sd = 0.5), nr = nr1),
          matrix(rnorm(nr2*nc1, mean = 0,   sd = 0.5), nr = nr2),
          matrix(rnorm(nr3*nc1, mean = 0,   sd = 0.5), nr = nr3)),
    rbind(matrix(rnorm(nr1*nc2, mean = 0,   sd = 0.5), nr = nr1),
          matrix(rnorm(nr2*nc2, mean = 1,   sd = 0.5), nr = nr2),
          matrix(rnorm(nr3*nc2, mean = 0,   sd = 0.5), nr = nr3)),
    rbind(matrix(rnorm(nr1*nc3, mean = 0.5, sd = 0.5), nr = nr1),
          matrix(rnorm(nr2*nc3, mean = 0.5, sd = 0.5), nr = nr2),
          matrix(rnorm(nr3*nc3, mean = 1,   sd = 0.5), nr = nr3))
   )
mat = mat[sample(nr, nr), sample(nc, nc)] # random shuffle rows and columns
rownames(mat) = paste0("row", seq_len(nr))
colnames(mat) = paste0("column", seq_len(nc))
hm <- ComplexHeatmap::Heatmap(mat)

In Positron, calling ComplexHeatmap::draw(hm) produces a blank plot.
Something about how Positron handles grid graphics? I don't know, they seem to handle ggplot2 and other grid output just fine.

The workaround is to convert to grid grob, then draw.
I don't know a solution. Perhaps update draw() to detect "if running inside Positron" and render accordingly?
I know that rendering ComplexHeatmap in RStudio produces a warning, so this is similar but for Positron.

# convert to grid grob
hm_grob <- grid::grid.grabExpr(
  draw(Heatmap(mat)))
# draw the grob
grid::grid.draw(hm_grob)

In case useful to anyone else, I made a wrapper function to draw ComplexHeatmap inside Positron.
The '...' ellipses allow passing arguments internally to draw().

draw_chm <- function
(hm, ...)
{
   hm_grob <- grid::grid.grabExpr(
      ComplexHeatmap::draw(hm, ...))
   grid::grid.newpage();
   grid::grid.draw(hm_grob);
}

Then draw like this:
draw_chm(hm)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions