Question

I have a big Sweave file with a variable called "specialty" near the top. The rest of this file is Latex and R, and uses this variable.

How can I loop over various values for "specialty"?

Two possibilities are:

  1. Make the file be one big loop (and convert the Latex parts to R).
  2. Write a script that copies the Sweave file, replace the value of "specialty", and run Sweave on each copy.

Can you comment on these ideas, or suggest better ones?

Was it helpful?

Solution

Could you state what you want your document to look like in the end? Clearly it has repetitive structures in it. In which case, Sweave may not be the best tool. You might instead want to consider using something like brew. See this blog post on the Learning R blog for an example of how this works.

OTHER TIPS

Here is some information that may be helpful to people who are new to brew.

(I learned about brew today and used it to create a book document, with a chapter for each "specialty".)

Shane's link is helpful. Another link is Brew. This has downloads and a short reference manual (seven pages).

In at least one way, brew is nicer than Sweave:

  • In brew, the tags are simpler, being variations of <%...%>.
  • In Sweave, the tags are <<...>>=...@ and \Sexpr{...}.

If you want to try brew, do the following in R:

install.packages("brew")
library(brew)

Save the following brew code in a file called book.brew. The code prints some digits of pi, with one digit per chapter. Note that there is one loop, and that parts of it are in Latex, and parts of it are in brew tags.

   \documentclass{book}
    \title{A book}
    \begin{document}
    \maketitle
    <%# This comment will not appear in the Latex file. %>
    <%
    digits = c(3, 1, 4, 1, 5, 9)
    for (i in 1:length(digits))
    {
    %>
    \chapter{Digit $<%= i %>$}
    Digit $<%= i %>$ of $\pi$ is $<%= digits[i] %>$.
    <%
    }
    %>
    \end{document}

Note: when you save the file, make the last line be a blank line, or brew will give you a warning about an unfinished line.

In R, type

brew("/your/path/to/book.brew", "/where/you/want/brew/to/create/book.tex")

Compile the Latex file book.tex.

I've done exactly this before, using your second option. I had a separate R file that would iterate through the group names, assign each to the group variable (your specialty), create a renamed copy of the Sweave master file (with the group's name pasted into the filename), and then Sweave that new file. Your use of the word "replace" makes me hesitant - I wouldn't try any kind of regex solution (and maybe that's not what you intend). Just assigning it in the master script (specialty <- specialties[i]) is fine.

That code is trapped on my currently-dead home PC, but I might have it on a flashdrive somewhere. If you have trouble getting this working right, let me know and I'll dig around for it.

brew is probably also worth looking into, though I don't have any personal experience with it yet and therefore can't compare it to Sweave.

There a solution that lets you stay in Sweave without having to use Brew . The key is to turn the code that's applied in the loop into a Latex macro with \newcommand, and then have an R chunk that loops over your variable and emits a call to your Latex macro for each value

A complete example is available at https://stat.ethz.ch/pipermail/r-help/2008-June/164783.html, but here is the gist of it:

\documentclass{article}
\SweaveOpts{echo=FALSE}
\newcommand\digit[2]{%
  \section{Digit #1}
  Digit #1 of $\pi$ is $#2$.
}
\title{Digits of $\pi$}
\begin{document}
\maketitle
<<results=tex>>=
digits = c(3, 1, 4, 1, 5, 9)
for (i in seq(digits)) {
  cat(paste("\\digit{", i, "}{", digits[i], "}\n", sep=""))
}
@ 
\end{document}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top