Skip to content

problem using cat within Sweave code chunks when fig=TRUE

3 messages · Paul.Rustomji at csiro.au, Wolfgang Huber, Brian Ripley

#
Hello R list

I was intending to use a cat statement within Sweave code chunks that
generate greaphs to generate a readme.txt file listing all the figures
generated with a brief caption along the lines of:

desired format of "readme.txt"
_________________________________
figure1.eps   caption for figure1
figure1.pdf   caption for figure1
figure2.eps   caption for figure2
figure2.pdf   caption for figure2
_________________________________


As an example, this block of sweave code replicates what I would like to
do in principle:


<<label=codechunk1,fig=TRUE,include=FALSE>>=
plot(1:10)
cat("figure1.eps caption goes here \n",file="readme.txt",append=T)
cat("figure1.pdf caption goes here \n",file="readme.txt",append=T)
@


<<label=codechunk2,fig=TRUE,include=FALSE>>=
plot(11:20)
cat("figure2.eps caption goes here \n",file="readme.txt",append=T)
cat("figure2.pdf caption goes here \n",file="readme.txt",append=T)
@

which I originally though would produce the desired result, however, the
cat statement appears to get executed three times producing: 

readme.txt---
figure1.eps caption goes here 
figure1.pdf caption goes here 
figure1.eps caption goes here 
figure1.pdf caption goes here 
figure1.eps caption goes here 
figure1.pdf caption goes here 
figure2.eps caption goes here 
figure2.pdf caption goes here 
figure2.eps caption goes here 
figure2.pdf caption goes here 
figure2.eps caption goes here 
figure2.pdf caption goes here 

I have figured out that any sweave code with fig=TRUE appears to be
executed multiple times (up to three), presumably to write to both eps
and pdf graphics devices (not sure what the first/last execution does
though...).  

Does anyone have any ideas about how to only execute the cat statements
the first time around so that the output looks like what I specified at
the top of this message?

Paul


Paul Rustomji
Rivers and Estuaries
CSIRO Land and Water
GPO Box 1666
Canberra ACT 2601
#
Hi Paul,

You could

<<label=codechunk1a,fig=TRUE,include=FALSE>>=
plot(1:10)
@
<<label=codechunk1b>>=
cat("figure1.eps caption goes here \n",file="readme.txt",append=T)
cat("figure1.pdf caption goes here \n",file="readme.txt",append=T)
@

You can suppress one of the three executions by setting eps=FALSE in the 
code chunk options or in the document-wide \SweaveOpts
settings.

I would also be interested what the third execution is good for.

Also, if you set both eps=FALSE and pdf=FALSE, the code chunk is not 
shown in the (.tex) output, not graphic output file is produced, but the 
chunk still seems to be executed once.

  best wishes
	Wolfgang
Paul.Rustomji a csiro.au wrote:
#
On Mon, 25 Aug 2008, Wolfgang Huber wrote:

            
Sweave runs the code once (with the default graphics device), then once 
more for each 'fig' output.
I think you will find graphics output *is* produced, e.g. on Rplots.pdf in 
non-interactive use.  Look for 'eval' in makeRweaveLatexCodeRunner() to 
see why.