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 --
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.
Stephan Kolassa <Stephan.Kolassa <at> gmx.de> writes:> I would like to loop over a medium amount of Sweave code, including both R andLaTeX chunks. Is there any way to> do so? As an illustration, can I create a .tex file like this using a loopwithin 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} > ################################################I normally do this with a \newcommand: all latex stuff in the newcommand{}, passing parameters created by R. Dieter
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 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.-- http://biostat.mc.vanderbilt.edu/JeffreyHorner
Stephan Kolassa <Stephan.Kolassa <at> gmx.de> writes:> > I would like to loop over a medium amount of Sweave code, including both Rand LaTeX chunks. Is there any way to Stephan noted that \newcommand was not expanded, so after re-reading I realized that my comment was a bit short. After all, when I had encountered the problem the first time, I remember some headaches when thinking about the hen and the egg. Below is a full example. The trick is to generate the newcommand and parameter with R. This method is especially helpful when you have a dozen or so of laboratory parameter that all have to be plotted, tabled, tested between groups, and summarized. I write the chapter only once, when I works I wrap everything with a newcommand. Default Sweave figure creation is not possible, but doing it explicitely with a pdf/dev.off is not much more complicated. Dieter % ---------------------------------- \documentclass{article} % \usepackage{Sweave} \SweaveOpts{echo=FALSE} \newcommand\bloodp[3]{ \subsection{Patient #1} For patient #1, the mean value of systolic pressure was #2~mmHg, the diastolic pressure was #3~mmHg. \begin{figure}[!htb]% \begin{center}% \includegraphics{histo#1}% \caption{Histogram of systolic blood pressure for patient #1.}% \label{fig:histo#1}% \end{center}% \end{figure}% \clearpage % Better use FloatBarrier here } \begin{document} \section{Blood Pressure} <<results=tex>>n=100 dt = data.frame(subj=sample(1:3,n,TRUE), syst=round(rnorm(n,120,10)),dia=round(rnorm(n,80,10))) # could also use tapply here for (i in 1:3) { dt1 = dt[dt$subj==i,] cat("\\bloodp{",i,"}{", round(mean(dt1$syst)),"}{", round(mean(dt1$dia)),"}\n",sep="") pdf(paste("histo",i,".pdf",sep="")) hist(dt1$syst,main="",xlab="Blood pressure") dev.off() } @ \end{document}