0

I am trying to create heatmaps using RStudio in a for loop. I would like to save it in the folder directly rather than displaying the graphs in the plots window. I want to know how to do this inside a loop.

This is the code I tried so far> a[[i]] is dataframe containing the values to be plotted

setwd("/home/coolgal2k/output/heatmaps/")
hm = NULL
for (i in 1:100){
hm.900[[i]] <- heatmap.2(a[[i]], 
                     dendrogram="none", Rowv=F,Colv=F, 
                     col=bluered(256), scale="none", key=T, 
                     density.info="none", main=paste0("Heatmap of Z-scores","[[i]])", 
                     cexRow=0.7, cexCol=0.7, trace="none", symm=F, symbreaks=T, symkey=T) 
dev.off()
}

When I try to do this:

I get error saying that unexpected "}" in: I checked for all formatting in the code, it is fine!

Please help!

coolgal2k
  • 3
  • 3
  • 1
    You're missing a closing quote or a closing parentheses somewhere in all your ellipses. If you want us to help debug, please include all the code as well as the package name you're using. – Justin Jun 25 '14 at 11:03
  • @Justin: Just edited with the entire code! Thanks – coolgal2k Jun 25 '14 at 11:07

1 Answers1

1

Your paste command has the closing ) inside the quote. Its even colored wrong by the SO highlighter. If you're not already, I strongly suggest getting a good editor that does this similar syntax highlighting for you.

To save separate images, you can open a separate device and close it in each loop

for ... {
    png(paste(filename, I))
    heatmap.2(...)
    dev.off()
}

Or better, open a single device, and r do the work

png( "filename%02d")
for ...{
    heatmap.2(...)
}
dev.off()
Justin
  • 42,475
  • 9
  • 93
  • 111
  • Thanks @Justin. I got to know where exactly you are pointing out. I would like to know how to save each of them in a folder inside this loop. – coolgal2k Jun 25 '14 at 11:21
  • 1
    @coolgal2k that is a separate and unrelated question. Please accept this as answered and ask another using all the code you supplied above. Also, please make it [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). However, to save them, you need to open a device for each image inside your loop... – Justin Jun 25 '14 at 11:27
  • My question in the first instance was to know how to save each plot in a folder inside a loop – coolgal2k Jun 25 '14 at 11:49
  • Touche. Answer edited – Justin Jun 25 '14 at 11:58