[R] several figures from one Sweave chunk? [solved]
Liviu Andronic
landronimirc at gmail.com
Sat Aug 7 13:30:46 CEST 2010
(on-list)
Hello Cameron
On Fri, 6 Aug 2010 19:02:22 +0100
Liviu Andronic <landronimirc at gmail.com> wrote:
> On Fri, 6 Aug 2010 11:30:59 -0600
> Cameron Bracken <cameron.bracken at gmail.com> wrote:
> > There is no real good way to deal with this in Sweave. Sweave does
> > not actually know anything about the graphics it is creating, all
> > it knows it that a graphic is or is not being created. The problem
> > lies with the graphic devices, each has its own way of creating a
> > new "page." That is when you create a new plot without closing the
> > graphics device. For example, pdf() createS a new page in the pdf
> > document, png() just overwrites the old image and tikz() creates a
> > whole new tikzpicture. These are all done without Sweave knowing a
> > bit about it.
> >
> > So the answer is no, pgfSweave nor cacheSweave have any way of
> > dealing with this. In my experience, you are much better off
> > either creating separate chunks for each image, or using something
> > like layout:
> >
> > <<fig=TRUE>>=
> > layout(matrix(c(1,3,2,4),nrow=2))
> > for (i in 1:4) plot(rnorm(100)+i)
> > @
> >
Unfortunately neither creating separate chunks for each image nor using
layout are options in my case: I need to automate the repetitive task
of describing the variables in my data set (15 for one df, about 45
for the second), using graphs and summary stats. This would require to
loop over mixed R/LaTeX code, something not allowed by Sweave.
Fortunately, I found a way to work around, based on the Sweave FAQ [1].
If you need not only graphs, but some LaTeX markup as well, Dieter
Menne proposed a very elegant solution that involved defining a
\newcommand containing most LaTeX code and calling it in the R loop
via a cat() statement [2]. Another alternative is to put all LaTeX code
in cat() statements, but this tends to get messy.
The final hurdle was to output some 'verbatim' text from within a
<<results=tex>>=
@
code chunk, which contained the entire loop. This can
be done by printing the object within
cat("\\begin{verbatim}\n")
[..]
cat("\\end{verbatim}\n")
I would have wanted to include this in the \newcommand call, but since
my object was a list, it couldn't be passed via cat(). I would love to
hear a workaround to this.
Here are the links to the LyX [3] and Sweave [4] versions and the PDF
[5] result of my file, extending Dieter's example.
Another potential solution, that I haven't yet fully investigated, is
brew. Contrary to my first impressions, its syntax is simpler than that
of Sweave. Moreover, it "natively" handles looping over mixed R/LaTeX
code and is thus perfect for generating repetitive reports. Combined
with a new 'weave' method [6], it seems that brew could easily be used
instead of Sweave.
To echo Dieter's comments, I had some headaches when thinking
about the hen and the egg. Hope this helps
Liviu
[1] http://www.stat.uni-muenchen.de/~leisch/Sweave/FAQ.html#x1-11000A.9
[2] https://stat.ethz.ch/pipermail/r-help/2008-June/164783.html
[3] http://s000.tinyupload.com/index.php?file_id=22646520644600938098
[4] http://s000.tinyupload.com/index.php?file_id=73594116524789598605
[5] http://s000.tinyupload.com/index.php?file_id=00010585455841885948
[6] http://biostatmatt.com/archives/573/comment-page-1#comment-770
> > Hope that helps. If you have a specific application where you need
> > this type of functionality, I may be able so suggest a workaround.
> >
> > -Cameron
> >
More information about the R-help
mailing list