[R] Sweave running R chunks for figure generation several times

Greg Snow Greg.Snow at imail.org
Fri Mar 26 16:44:11 CET 2010

Is the order consistent?  Meaning that the pdf/eps producing run always comes second?

If so, then set a variable to FALSE and when you get to the plotting code have an if statement on that variable, if it is FALSE then set it to TRUE, if it is TRUE then run the plotting code.  This way it only runs the 2nd time.  The only problems I can see is if the first default plot is needed to get some parameters for setting up the pdf/eps devices, and if you want to include the code (not just the plot) in the output then it will have the extra test.

Using some form of cachesweave may be usefull here as well.

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Marc Schwartz
> Sent: Thursday, March 25, 2010 1:03 PM
> To: Florian Burkart
> Cc: Leisch Friedrich; R-Help help
> Subject: Re: [R] Sweave running R chunks for figure generation several
> times
> On Mar 25, 2010, at 10:14 AM, Florian Burkart wrote:
> > Hi,
> >
> > following issue:
> >
> > Sweave is running the R code to generate
> > figures three times under standard behaviour,
> > twice if run with eps=FALSE, once if run
> > with eps=FALSE,pdf=FALSE.
> >
> > However, ideally, I'd want it to run once to
> > generate pds with eps=FALSE being set.
> >
> > How come this isn't so? Am I missing something?
> >
> > Basically, my R figure code is expensive to
> > run. I can of course move the very expensive
> > bits outside of the figure environment, but
> > there are still some costs involved...
> >
> > Thanks!
> Hi,
> The issue is that the figure chunk code is run initially just as a code
> chunk, which of course results in plots going to the default display
> device. Then the code is run once for each of the PDF and EPS
> (postscript) devices, depending upon the settings for 'pdf' and 'eps'.
> So even with both set to FALSE, the code is still run at least once
> using the default display device.
> It is not clear to me that there is an easy way to avoid the initial
> execution of the code to the default display device and a search of the
> archives does not seem to result in a solution.
> It seems to me that the issue of disabling the screen display of plots
> during Sweave processing has come up before, but I cannot seem to
> locate anything at the moment. Perhaps someone else has better
> recollection than I do.
> As you note, the best approach is to move as much of the time consuming
> code out of the figure chunk as possible, leaving only the plotting
> code in the chunk.
> I am cc'ing Fritz on this reply, as I know from prior communications
> that he has plans to re-write aspects of Sweave and that perhaps
> enabling the addition of an option to not run the code initially to the
> default display device may be something that he can consider as an RFE.
> HTH,
> Marc Schwartz
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list