Paul.Rustomji at csiro.au
2008-Aug-24 23:49 UTC
[R] problem using cat within Sweave code chunks when fig=TRUE
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
Wolfgang Huber
2008-Aug-25 11:15 UTC
[R] problem using cat within Sweave code chunks when fig=TRUE
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:> 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 a 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.