tags:

views:

40

answers:

1

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.
+3  A: 

Quick comments:

  1. use the onefile=TRUE argument which gets passed through to pdf(), see the help pages for dev.copypdf and pdf

  2. as a general rule, you may find it easier to open the devices directly; again see help(pdf)

So in sum, add onefile=TRUE to you call and you should be fine but consider using pdf() directly.

Dirk Eddelbuettel
+1 to push you over 1000 and to get the gold r badge. :)
Shane
Thanks for the answer! I was totally on the wrong track there. However, I have problems with my pdf() export, and I will make a new question which I hope you can answer too :)
Timtico