[R] Sweave

Frank E Harrell Jr fharrell at virginia.edu
Wed Jul 30 17:24:19 CEST 2003


Many of you are using Sweave for making statistical reports.  I thought it might be helpful to some to see an example of the setup I use (in Linux).  For those of you who have not yet discovered the power and productivity gains from using Fritz Leisch's wonderful package, I encourage you to give Sweave a try.

%File: model.nw
%Usage:
% Put library(Hmisc;Design;tools} in .First
% Sweave model   - runs model.nw to produce model.tex and
%                  graphics files in graphics/
% Sweave shell script defined as echo "Sweave(\"$1.nw\")" | R --no-save
% latex or pdflatex model; bibtex model; latex or pdflatex model
% To get .R file: Stangle model
\documentclass{article}
\usepackage{relsize,setspace}  % used by latex(describe( ))
\newcommand{\co}[1]{\texttt{\smaller #1}}

\title{Analysis}
\author{Frank E Harrell Jr}
\begin{document}
\SweaveOpts{prefix.string=graphics/plot}
\setkeys{Gin}{width=1.0\textwidth}
\maketitle
<<echo=F>>=
Hmisc.version  <- package.description('Hmisc')['Version']
Design.version <- package.description('Design')['Version']
@
All calculations were done using \Sexpr{R.version.string} \cite{Roriginal} on
RedHat Linux 9.0, using version \Sexpr{Hmisc.version} of the \texttt{Hmisc}
package and version \Sexpr{Design.version} of the \texttt{Design} package.
\section{Descriptive Statistics}
<<results=tex>>=
load('mydata.sav')
latex(describe(mydata), file='')  # generate LaTeX code in place
@ 
The variable clustering diagram below shows which variables in the
dataset are interrelated.  The $y$-axis is the Spearman $\rho^2$
(squared rank correlation coefficient).  \co{addon} and
\co{mica} cannot be considered together because of an undefined
correlation between them.  \co{addon} correlates weakly with
\co{sodm} (only).  \co{usedlabd} is always unity when it is not
missing, so it is also omitted from variable clustering.
\begin{center}
<<fig=T>>=
plot(varclus(~.-addon-usedlabd, data=mydata))
@
\end{center}
\section{Analysis of Dose Effects}
. . . .
\bibliography{/home/feh/bib/feh.bib}
\bibliographystyle{abbrv}
\end{document}

For more information about reproducible statistical reporting, literate programming, and Sweave see http://hesweb1.med.virginia.edu/biostat/s/LiveDoc.html

---
Frank E Harrell Jr              Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat




More information about the R-help mailing list