[R] Sweave and multiple graphs

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Jun 11 14:07:18 CEST 2004

Dear list,

I am using Sweave to build a small report. I want to produce a series of 
figures, each figure containing a number of plots and then have them 
included in the Sweave file.

An example would be to :

postscript(file = "ANCbwplot%03d.eps", onefile = FALSE, other options...)
oldpar <- par(mfrow = c(2,2))
do lots of plots to produce a number of eps files

The example in the Sweave FAQ shows how to do something similar for 
cases where you know how many figures there are, but I do not know how 
many figures will be produced so want to produce a more generic solution.

I thought of doing the above code in Sweave, and because I named the 
plots in a unique way, I now want to read all the files in the current 
directory that match "ANCbwplot001.eps" or "ANCbwplot002.eps" or 
"ANCbwplot003.eps" an so on. If I have this as a vector in R, then I can 
loop over this vector and do something like:

file.vec <- all files in directory that match name
for(i in seq(along=file.vec))
   cat("\\includegraphics{", file.vec[i], "}\n\n", sep"")

in Sweave.

I'm not sure about getting a list of file names from the current working 
directory that match a given string that I can then loop over and print 
out using cat as shown above. If anyone has any suggestions as to how to 
go about doing this that they are willing to share I would be most grateful.

Thanks in advance,


Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpson at ucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.

More information about the R-help mailing list