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

Marc Schwartz marc_schwartz at me.com
Thu Mar 25 20:03:30 CET 2010

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!


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.


Marc Schwartz

More information about the R-help mailing list