Following Ben Bolker's comment:
Simply copy and paste the entire question
environment. Edit the chunks' names. Sweave will then run the code twice and output the same text but with different numbers put in by \Sexpr
.
\begin{shortanswer}[title={\Large Short Answer (10 pts each)}]
\begin{question}
<<ifst1, echo=FALSE>>==
N = round(runif(1, 5, 20))
@
John wants to know whether his students are smarter than the average student.
John has \Sexpr{N} students \dots
\examvspace*{1.5in}
\begin{answer}
\end{answer}
\end{question}
\begin{question}
<<ifst2, echo=FALSE>>==
N = round(runif(1, 5, 20))
@
John wants to know whether his students are smarter than the average student.
John has \Sexpr{N} students \dots
\examvspace*{1.5in}
\begin{answer}
\end{answer}
\end{question}
\end{shortanswer}
This works, but heavily violates the DRY principle. With knitr, a successor of Sweave, you could write R code that creates the entire question
environment and writes it verbatim into the generated LaTeX code. (Perhaps this is possible with Sweave, too?) Something along these lines:
\begin{shortanswer}[...]
<<generate, echo=FALSE, format=asis>>=
for (i in 1:2) {
N = round(runif(1, 5, 20))
paste('\\begin{question}', 'John has', N, 'students', ..., '\\end{question}')
}
@
\end{shortanswer}
Best Answer
If using another package is acceptable for you, you should consider
automultiplechoice
, which also provides free software (for linux or osx) to correct the completed answer sheet scans for you. See http://home.gna.org/auto-qcm/