ComplexHeatmap - Heatmap function within a for loop
1
1
Entering edit mode
An De Bondt ▴ 40
@an-de-bondt-3329
Last seen 8.2 years ago

Dear useRs,

Below a small chunk of code to illustrate an issue with generating heatmaps using the Heatmap function within the ComplexHeatmap package.

# this works fine, a pdf file gets generated and can be opened:

       dat <- matrix(rnorm(200), 10)
       pdf("heatmap.pdf", width = 15, height = 5)
       
Heatmap(dat)
       dev.off()

# a for loop around also generates files (from the code below just 1)

for (i in 1){
       dat <- matrix(rnorm(200), 10)
       pdf(paste0("heatmap", i, ".pdf"), width = 15, height = 5)
       Heatmap(dat)
       dev.off()
}

# However, trying to open the file results in this error message in Acrobar reader (Version 10.1.8): “The file cannot be opened because it has no pages”

# a for loop around the heatmap.2 function from the gplots library works fine (but adjusting the plot is less flexible)

library(gplots)
for (i in 1)
 
       dat <- matrix(rnorm(200), 10)
       pdf(paste0("heatmap", i, ".pdf"), width = 15, height = 5)
       heatmap.2(dat)
       dev.off()
}

It would be very nice if someone knows a work around.
Thanks a lot in advance!

Best regards,
An

 

 

 

>sessionInfo()

R version 3.2.2 (2015-08-14)

Platform: x86_64-pc-linux-gnu (64-bit)

 

locale:

[1] C

 

attached base packages:

[1] grid      stats     graphics  grDevices utils     datasets  methods 

[8] base    

 

other attached packages:

[1] gplots_2.17.0        ComplexHeatmap_1.6.0 rj_2.0.4-2         

 

loaded via a namespace (and not attached):

[1] rj.gd_2.0.0-1       circlize_0.3.4      dendextend_1.1.8  

 [4] gtools_3.5.0        bitops_1.0-6        magrittr_1.5      

 [7] KernSmooth_2.23-15  GlobalOptions_0.0.8 gdata_2.17.0      

[10] whisker_0.3-2       GetoptLong_0.1.1    RColorBrewer_1.1-2

[13] rjson_0.2.15        tools_3.2.2         colorspace_1.2-6  

[16] caTools_1.17.1      shape_1.4.2       

 

 

 

ComplexHeatmap for loop Heatmap function • 4.8k views
ADD COMMENT
2
Entering edit mode
@james-w-macdonald-5106
Last seen 29 minutes ago
United States

The ComplexHeatmap package uses grid graphics, and it is often the case that you need to print your Heatmap output for it to show up in the pdf.

for (i in 1){
       dat <- matrix(rnorm(200), 10)
       pdf(paste0("heatmap", i, ".pdf"), width = 15, height = 5)
       print(Heatmap(dat))
       dev.off()
}
ADD COMMENT
0
Entering edit mode

Great, thanks a lot, that works indeed!

Best regards,
An

ADD REPLY
0
Entering edit mode

I'm trying to save list of heatmaps after doing this

hm_lst<-cmplx_ht(list_of_files,metadata) 

The to save I tried something like this

lapply(1: length(hm_lst), function(i){
  ggsave(plot = hm_lst[[i]], 
         filename = paste(names(hm_lst)[i], ".pdf"), 
         width = 15)
})

The error is this

 Error: $ operator not defined for this S4 class 

Although the hm_list class is list bt still im not able to save it. Any suggestion what Im doing grossly wrong

ADD REPLY
2
Entering edit mode

What is hm_list? I would not use ggsave() to save the heatmaps. Can you directly use:

pdf(...)
draw(ht). # or print(ht)
dev.off()
ADD REPLY
0
Entering edit mode

yes it worked

ADD REPLY

Login before adding your answer.

Traffic: 976 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6