[R] loops in Sweave

Jason Turner jasont at indigoindustrial.co.nz
Tue Sep 23 23:12:14 CEST 2003


On Wed, 2003-09-24 at 03:06, Millo Giovanni wrote:
> Dear all,
> 
> I was wondering whether there is a way to make loops in Sweave, i.e. for example to:
> 1) calculate a parameter, say, a=length(b)
> 2) according to that, add #a# chapters to the document, each including some repetitive analysis, each time done on a particular subset of the data indexed by the elements of 1:a.
> This would be of great help for repeating exploratory data analyses on, say, questionaries when the number of questions changes without having to change the Sweave .snw file.

Two possible ways, off the top of my head:

1) Within LaTeX, use \Sexpr{a} to get the length, then loop within
LaTeX.  I believe Lamport includes an example of looping within LaTeX,
but I haven't got the book handy.

2) Within the R chunk, generate the table using xtable() (package
"xtable") or Latex (package "Hmisc") and print directly within R.  I
haven't tried it, but something like
<<echo=FALSE,results=tex>>=
## build your table in R
tt <- xtable(foo)
print(tt)

@

might do the trick.  I'd been meaning to look at this anyway; you
question prompted me ;)

Check the Sweave manual at Herr Dr Leisch's site.
http://www.ci.tuwein.ac.at/~leisch/Sweave
This has the R chunk options required to produce the above, if my
untested example is not correct.

Cheers

Jason

-- 
Indigo Industrial Controls Ltd.
http://www.indigoindustrial.co.nz
+64-(0)21-343-545




More information about the R-help mailing list