[R] Sweave: looping over mixed R/LaTeX code
Delphine Fontaine
delphine.fontaine at genexion.com
Fri Jun 13 10:40:12 CEST 2008
Dear Stephan,
I have the same problem than you. My solution is a bit different but not very elegant....
I have a master document (let say master.Snw) and a file containing the code to repeat (which would be in the loop).
In the master document I start a counter at 0, and I copy " \SweaveInput{loop.Snw}" as many times as the n of the loop.
And in my loop.Snw, I don't forget to increment the counter of 1.
Not marvelous, but it works...
Delphine
Delphine Fontaine
Statistician
Data & Statistics Department
Genexion SA
Please consider the environment before printing this e-mail
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Stephan Kolassa
> Sent: vendredi 13 juin 2008 10:22
> To: r-help at r-project.org
> Subject: [R] Sweave: looping over mixed R/LaTeX code
>
>
> 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}
> ################################################
>
> 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 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.
More information about the R-help
mailing list