[R] problem using cat within Sweave code chunks when fig=TRUE

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 26 09:32:31 CEST 2008


On Mon, 25 Aug 2008, Wolfgang Huber wrote:

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

Sweave runs the code once (with the default graphics device), then once 
more for each 'fig' output.

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

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.


> best wishes
> 	Wolfgang
>
>
>
>
> Paul.Rustomji at csiro.au wrote:
>> 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
>> 
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list