R: dev.copy2pdf, multiple graphic devices to a single file, how to append to file?

Posted by Timtico on Stack Overflow See other posts from Stack Overflow or by Timtico
Published on 2010-06-18T15:25:43Z Indexed on 2010/06/18 16:03 UTC
Read the original article Hit count: 493

Filed under:

Hi everybody,

I have a script that makes barplots, and opens a new window when 6 barplots have been written to the screen and keeps opening new graphic devices whenever necessary. Depending on the input, this leaves me with a potential large number of openened windows (graphic devices) which I would like to write to a single PDF file.

Considering my Perl background, I decided to iterate over the different graphics devices, printing them out one by one. I would like to keep appending to a single PDF file, but I do not know how to do this, or if this is even possible. I would like to avoid looping in R. :)

The code I use:

for (i in 1:length(dev.list())
{
dev.set(which = dev.list()[i]
dev.copy2pdf(device = quartz, file = "/Users/Tim/Desktop/R/Filename.pdf")
}

However, this is not working as it will overwrite the file each time. Now is there an append function in R, like there is in Perl. Which allows me to keep adding pages to the existing pdf file?

Or is there a way to contain the information in a graphic window to a object, and keep adding new graphic devices to this object and finally print the whole thing to a file?

Other possible solutions I thought about:

  1. writing different pdf files, combining them after creation (perhaps even possible in R, with the right libraries installed?)
  2. copying the information in all different windows to one big graphic device and then print this to a pdf file.

© Stack Overflow or respective owner

Related posts about r