How do you iterate over a global variable in sweave

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

How can I iterate over different meanings for a "specialty"?

Two possibilities are possible:

  • Make the file one big loop (and convert the Latex parts to R).
  • Write a script that copies the Sweave file, replaces the "specialty" value, and runs Sweave for each copy.

Can you comment on these ideas or suggest the best ones?

+4
source share
4 answers

Could you please indicate how you want your document to look at the end? It is clear that there are repeating structures in it. Sweave may not be the best tool in this case. Instead, you might want to use something like brew . See this Learning R blog post for an example of how this works.

+5
source

Here is information that may be useful for 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 . It has downloads and a quick reference guide (seven pages).

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

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

If you want to try brew, follow these steps in R:

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

Save the following brew code in a file called book.brew . The code prints several digits pi with one digit for each chapter. Note that there is one loop, and its parts are in latex, and its parts 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 empty, or brew will give you a warning about the incomplete line.

In R, enter

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

Compile the Latex book.tex file.

+5
source

I have done this before using the second option. I had a separate R file that will iterate over the names of groups, assign them to each group (your specialty ), create a renamed copy of the main Sweave file (with the group name inserted in the file name), and then Sweave is a new file. Your use of the word "replace" makes me hesitate - I would not try any regular expression (and perhaps this is not what you intend). Just assigning it to the master script ( specialty <- specialties[i] ) is fine.

This code gets trapped on my current home PC, but maybe I have it on flashdrive. If you have problems with the correct operation, let me know and I will delve into it.

brew is probably worth a look, although I have no personal experience with it, and therefore I cannot compare it with Sweave.

+2
source

There is a solution that allows you to stay in Sweave without using Brew. The key is to turn the code that was used in the loop into a Latex macro with \newcommand , and then have an R fragment that \newcommand over your variable and issues a call to your Latex macro for each value

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

 \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} 
0
source

Source: https://habr.com/ru/post/1307594/


All Articles