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 .... par(oldpar) dev.off() 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: <<results=tex,echo=FALSE>>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 -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% 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. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
r-help-bounces at stat.math.ethz.ch wrote on 11/06/2004 14:07:18:> 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 > .... > par(oldpar) > dev.off() > > 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: > > <<results=tex,echo=FALSE>>> 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 mostgrateful.> > Thanks in advance,r-help-bounces at stat.math.ethz.ch wrote on 11/06/2004 14:07:18:> Dear list, >[ generation of lots of graphics files to include in Sweave document]> > 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 mostgrateful.> > Thanks in advance,Use list.files(). It has a path argument (to specify the directory) and a pattern argument to put the regular expression. mygraphs <- list.files(path="./mygraphs", pattern="^ANCbwplot.*\\.eps") See ?list.files and maybe ?regex HTH, Tobias
tobias.verbeke at bivv.be wrote:> > Use list.files(). It has a path argument (to specify the directory) > and a pattern argument to put the regular expression. > > mygraphs <- list.files(path="./mygraphs", pattern="^ANCbwplot.*\\.eps") > > See ?list.files and maybe ?regex > > HTH, > TobiasHi Tobias. Thanks for the reply. I just found list.files() but was struggling with specifying the pattern argument. I have this working very well now for my application like so in a Sweave file: <<echo=FALSE,results=tex>>postscript(file = "ANCbwplot%03d.eps", onefile = FALSE, paper = "special", width = 4, height = 6, horizontal = FALSE) oldpar <- par(mfrow = c(4,3)) for (i in seq(along = g1865.w.res99)) { multplot(g1865.w.res99[[i]], m.title = names(g1865.w.res99[i])) } par(oldpar) invisible(dev.off()) graphs <- list.files(pattern = "^ANCbwplot.*\\.eps") for (i in seq(along = graphs)) { cat("\\includegraphics{", graphs[i], "}\n\n", sep = "") } @ multplot() is just a helper function that extracts the relevant information from the list g1865.w.res99 and plots a boxplot Many thanks for your help, Gavin -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% 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. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%