Stephan Kolassa wrote on 06/13/2008 03:22 AM:
Dear guRus,

I would like to loop over a medium amount of Sweave code, including both R and LaTeX 
chunks. Is there any way to do so? As an illustration, can I create a .tex file like this 
using a loop within a .Rnw file, where the "1,2,3" comes from some iteration 
variable in R?

################################################
\documentclass{article}
\usepackage{Sweave}
\begin{document}
Iteration 1
Iteration 2
Iteration 3
\end{document}
################################################

Another alternative would be to use the brew package from CRAN:

http://cran.r-project.org/web/packages/brew/index.html

While the disadvantage would be a change of syntax from Sweave to brew, you would gain the advantage of looping over code chunks.

brew also installs a collection of example files, one being a conversion of the Sweave test file to brew. Scope out the 'Examples' section from the brew help page.

Best,

Jeff


Right now, I do have a working but painful solution. I put the loop contents in 
a separate loop.Rnw file, then:
1. run everything before the loop through R for initialization
2. Sweave loop.Rnw; shell("move loop.tex loop_1.tex")
   Sweave loop.Rnw; shell("move loop.tex loop_2.tex")
   ...
   Sweave loop.Rnw; shell("move loop.tex loop_n.tex")
3. \input all loop_i.tex files into master.Rnw and Sweave master.Rnw

This does what I need, however, it is a major pain code-wise, e.g., there 
appears to be no way to control the loop during execution (n must be known in 
advance), and I need to control all graphics using \includegraphics with the 
iteration counter paste()d into the filename.

An alternative may be not using Sweave and working with one giant sink() and 
lots of print()s, letting R just write the entire .tex file. This also appears 
inelegant to me.

Is there a better way to do this?

I have tried to do my homework, see below. Do I get partial credit ;-) ?

Thank you all for your time!
Stephan


#########################################


I can't simply start a for loop within an R chunk and finish it in another one.

whiledo in the ifthen.sty package doesn't like Sweave at all. And of course, it 
would simply reuse the R chunks if it did work, without changing things between 
loops. For the same reason, I cannot define a \newcommand{\loopcontent}{...} 
with the entire loop contents and then simply write \loopcontent \loopcontent 
... or \input or \include the loop content from an external file.

Of course it would be possible to not use Sweave and just use the output from 
the R console, but there are a couple of figures I would really like to see 
close to the relevant portions of the calculations.

I also thought about putting the entire loop in *one* R chunk, but then I see no way to include LaTeX chunks *within* this R chunk. I can't just sink() to the .tex file in the middle of the R chunk (as the sink() gets appended to the .tex file only after Sweave is done with it).
I have read the Sweave manual and FAQs and the R/R Windows FAQ, I did both RSiteSearches and RSeek searches for all 
combinations of "Sweave" and "loop", "for", "while" I could think of.

For what it's worth, here's my sessionInfo():

R version 2.7.0 (2008-04-22) i386-pc-mingw32
locale:
LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252

attached base packages:
[1] stats graphics grDevices utils datasets tcltk methods base
other attached packages:
[1] svIDE_0.9-5

loaded via a namespace (and not attached):
[1] svMisc_0.9-5

--

______________________________________________
R-help@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.


--
http://biostat.mc.vanderbilt.edu/JeffreyHorner

______________________________________________
R-help@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.

Reply via email to