[R] Mixing LaTeX and R Code in Loops and Functions in Sweave

Gabor Grothendieck ggrothendieck at gmail.com
Sat Oct 17 19:37:19 CEST 2009


Try this:

<<results=latex,echo=false>>=
for(i in 1:10) {
  cat("...latex code...")
}
@

or check out the brew package.

On Fri, Oct 16, 2009 at 3:25 PM, Jacob R. Marcus
<jmarcus at u.washington.edu> wrote:
> I have a question about mixing LaTeX and R code in loops and functions in Sweave. Here's my problem:
>
> I want to do something like this:
>
> <<>>=
>
> # some R code for a loop
> for(i in 1:10) {
>
> @
>
> My LaTex code here would describe what I do in the loop. For instance, in this loop print out the numbers one through 10. Maybe I could even have
> Multiple sections in between the loop. For instance,
> \section{Part of 1 of the Loop}
>
> <<>>=
>                cat(paste("Number",i))
> }
> @
>
> The problem is that Sweave evaluates the first chunk of code and throws an error because I haven't closed the loop. A similar problem happens when I want to have LaTeX code intermixed with a function. So how do I mix LaTex code with R chunks that alone may throw errors, but together with another chunk does not. Thanks in advance for your help! I am new to R and this is my first post on this list.
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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