[R] Sweave and multiple graphs

tobias.verbeke@bivv.be tobias.verbeke at bivv.be
Fri Jun 11 14:22:09 CEST 2004





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 most
grateful.
>
> 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 most
grateful.
>
> 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




More information about the R-help mailing list