# Re: [R] Sweave: looping over mixed R/LaTeX code

From: Dieter Menne <dieter.menne_at_menne-biomed.de>
Date: Sat, 14 Jun 2008 07:30:04 +0000 (UTC)

Stephan Kolassa <Stephan.Kolassa <at> gmx.de> writes:

>
> I would like to loop over a medium amount of Sweave code, including both R and LaTeX chunks. Is there any way to

Stephan noted that \newcommand was not expanded, so after re-reading I realized that my comment was a bit short. After all, when I had encountered the problem the first time, I remember some headaches when thinking about the hen and the egg.


Dieter

% ----------------------------------


\documentclass{article} %

\usepackage{Sweave}
\SweaveOpts{echo=FALSE}

\newcommand\bloodp[3]{

\subsection{Patient #1}
For patient #1, the mean value of systolic pressure was #2~mmHg,   the diastolic pressure was #3~mmHg.
\begin{figure}[!htb]%
\begin{center}%

      \includegraphics{histo#1}%
\caption{Histogram of systolic blood pressure for patient #1.}%
\label{fig:histo#1}%


\end{center}%
\end{figure}%
\clearpage % Better use FloatBarrier here }

\begin{document}
\section{Blood Pressure}

<<results=tex>>=
n=100
dt = data.frame(subj=sample(1:3,n,TRUE),

syst=round(rnorm(n,120,10)),dia=round(rnorm(n,80,10)))

# could also use tapply here
for (i in 1:3) {
dt1 = dt[dt$subj==i,] cat("\\bloodp{",i,"}{",  round(mean(dt1$syst)),"}{",
round(mean(dt1$dia)),"}\n",sep="")  pdf(paste("histo",i,".pdf",sep="")) hist(dt1$syst,main="",xlab="Blood pressure")   dev.off()
}
@

\end{document}

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. Received on Sat 14 Jun 2008 - 07:33:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 14 Jun 2008 - 10:30:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.