tags:

views:

179

answers:

3

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?

+4  A: 

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.

Shane
Thank you for the reference to brew. I would like the Sweave results to be a book format, with a chapter for each "specialty" value.
Winston C. Yang
+2  A: 

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.

Matt Parker
Thank you for the reference to brew (as with another poster shane), and for offering your code. I wrote a Python program to read the Sweave file, replace the line containing the "specialty" assignment, and create a file called "batch" with Sweave and pdflatex commands, which I could run on the command line. I'm using bash on Mac OS 10.6. That's a good idea about using an assignment in an R master script.
Winston C. Yang
Interesting - I also used Python to ram all those LaTeX files through pdflatex. It's kind of reassuring that someone independently came up with a nearly-identical solution.
Matt Parker
You should package that so that others can use it. :)
Shane
Package... the Python script? Generalize the Sweave master file? I've been looking for an excuse to contribute some code for awhile, but you might have to help me see the opportunity here. :/
Matt Parker
+3  A: 

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.

Winston C. Yang
+1 Nice summary. Thanks for pursuing this.
Shane