[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
% 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
\usepackage{relsize,setspace}  % used by latex(describe( ))
\newcommand{\co}[1]{\texttt{\smaller #1}}

\author{Frank E Harrell Jr}
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}
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.
plot(varclus(~.-addon-usedlabd, data=mydata))
\section{Analysis of Dose Effects}
. . . .

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