Sweave and knitr Templates

NOTE: SUPERCEDED by knitr and LaTeX Styles knitrl and spaper; See GitHub rlatex Project

Using LaTeX Style Sweavel | Output

The most advanced way to use Sweave is through the R package SweaveListingUtils (see the third approach on this page) which does things such as listing all the functions in all the attached packages and highlighting these function names in all R code listings. SweaveListingUtils uses LaTeX's listings package to pretty-print R code. The Sweavel.sty package for LaTeX uses listings similarly but provides an easier way to customize the output and does not need you to run an R function to set this up as SweaveListingUtils does. Sweavel substitutes LaTeX listings package commands for the usual Sweave Sinput and Soutput environments. Having \usepackage{Sweavel} in your .Rnw file will cause Sweave to not use its default style package Sweave.sty. To install Sweavel.sty on your Linux/Unix system, download the .sty file (say to /tmp) and issue the following shell commands:
sudo cp /tmp/Sweavel.sty /usr/share/R/share/texmf/.
sudo mktexlsr
Alternatively, copy the .sty file to your project directory, especially if you want to customize it and have the local copy override any system copy.
NEW Sweavel.sty was updated June 2015 to require only black and white, using a different method to distinguish inputs and outputs.

With this approach, you can easily have LaTeX commands inside your R code by enclosing the command inside (* *). This is particularly useful for symbolic references to sections, pages, figures, and tables without the document. Here is a template using this approach. pdf output is found at the link above. There is a symbolic link to a section in the document in an R comment.

A pdf file with an advanced example along these lines is at SweaveLatex.
%Usage: R CMD Sweave template3.Rnw = Sweave template3
%       rubber -d template3 or pdflatex template3
% To get .R file: R CMD Stangle template3.Rnw = Stangle template3
\documentclass{article}
\usepackage{relsize,setspace}  % used by latex(describe( ))
\usepackage{needspace}
\usepackage{url}               % used in bibliography
\usepackage[superscript,nomove]{cite} % use if \cite is used and superscripts wanted
% Remove nomove if you want superscripts after punctuation in citations
\usepackage{lscape}            % for landscape mode tables
\textwidth 6.75in              % set dimensions before fancyhdr 
\textheight 9.25in
\topmargin -.875in
\oddsidemargin -.125in
\evensidemargin -.125in
\usepackage{fancyhdr}          % this and next line are for fancy headers/footers
\pagestyle{fancy}
\newcommand{\bc}{\begin{center}}  % abbreviate
\newcommand{\ec}{\end{center}}
\newcommand{\code}[1]{{\smaller\texttt{#1}}}
\newcommand{\R}{{\normalfont\textsf{R}}{}}

\usepackage{Sweavel}
% Uncomment some of the following to use some alternatives:
% \def\Sweavesize{\normalsize}  (changes size of typeset R code and output)
% \def\Rcolor{\color{black}}
% \def\Routcolor{\color{green}}
% \def\Rcommentcolor{\color{red}}
% To change background color or R code and/or output, use e.g.:
% \def\Rbackground{\color{white}}
% \def\Routbackground{\color{white}}
% To use rgb specifications use \color[rgb]{ , , }
% To use gray scale use e.g. \color[gray]{0.5}
% If you change any of these after the first chunk is produced, the
% changes will have effect only for the next chunk.

\SweaveOpts{keep.source=TRUE}
% To produce both postscript and pdf graphics, remove the eps and pdf
% parameters in the next line.  Set default plot size to 6x4 in.
\SweaveOpts{prefix.string=graphics/plot, eps = FALSE, pdf = TRUE}
\SweaveOpts{width=5, height=3.5}

\title{An Analysis}
\author{Jane Doe\\\smaller Department of Biostatistics\\\smaller Vanderbilt University School of Medicine}
\begin{document}
\maketitle
% Use the following 3 lines for long reports needing navigation
\tableofcontents
%\listoftables
%\listoffigures     % not used unless figure environments used
<<echo=f>>=
spar <- function(mar=c(3.25+bot-.45*multi,3.5+left,.5+top+.25*multi,.5+rt),
                 lwd = if(multi)1 else 1.75,
                 mgp = if(multi) c(1.5, .365, 0) else c(2.4-.4, 0.475, 0),
                 tcl = if(multi)-0.25 else -0.4,
                 bot=0, left=0, top=0, rt=0, ps=14,
                 mfrow=NULL, ...)
  {
    multi <- length(mfrow) > 0
    par(mar=mar, lwd=lwd, mgp=mgp, tcl=tcl, ...)
    if(multi) par(mfrow=mfrow)
  }
options(SweaveHooks=list(fig=spar)) # run spar() before every plot
options(prompt='  ',continue='  ')  # remove prompt characters at start of lines
require(rms)
@

\section{Descriptive Statistics}\label{descStats}
<<results=tex>>=
getHdata(support)   # Use Hmisc/getHdata to get dataset from VU DataSets wiki
d <- subset(support, select=c(age,sex,race,edu,income,hospdead,slos,dzgroup,
                       meanbp,hrt))
latex(describe(d), file='')
@
Race is reduced to three levels (white, black, OTHER) because of low
frequencies in other levels (minimum relative frequency set to 0.05).
<<>>=
d <- upData(d,
            race = combine.levels(race, minlev = 0.05))
@

\section{Redundancy Analysis and Variable Interrelationships}
\bc
% Note: giving a chunk name to each code chunk that produces a figure
% makes it easy to know which plots to send to a collaborator, and
% will not allow numbered orphan plots to be left when code chunks are
% inserted into the file.  The default in Sweave is for plots to be
% numbered by the chunks producing them.

<<vc,fig=t>>=
v <- varclus(~., data=d)
plot(v)
redun(~age+sex+race+edu+income+dzgroup+meanbp+hrt, data=d)
# Alternative: redun(~., data=subset(d, select=-c(hospdead,slos)))
@
\ec
Note that the clustering of black with white is not interesting; this just means that these are mutually exclusive higher frequency categories, causing them to be negatively correlated.
\section{Logistic Regression Model}
Here we fit a tentative binary logistic regression model.  The
coefficients are not very useful so they are not
printed.  Note: the symbolic section reference below was created by the
following \R\ comment:
\begin{verbatim}
# see Section (*\ref{descStats}*) for descriptive statistics
\end{verbatim}
The label was defined in an earlier section using
\begin{verbatim}
\section{Descriptive Statistics}\label{descStats}
\end{verbatim}

<<results=tex,fig=t,width=7,height=5>>=
dd <- datadist(d); options(datadist='dd')
f <- lrm(hospdead ~ rcs(age,4) + sex + race + dzgroup + rcs(meanbp,5),
         data=d) # see Section (*\ref{descStats}*) for descriptive statistics
print(f, latex=TRUE, coefs=FALSE)
latex(anova(f), where='h', file='')   # can also try where='htbp'
print(plot(Predict(f)))
@

\section{Test Calculations}
\bc
<<testp,fig=t>>=
x <- 3; y <- 2
if(x <= y) 'this' else 'that'
if(y >= x) 'that' else 'this'
x^y
plot(runif(20),runif(20))
@
\ec

\section{Computing Environment}
These analyses were done using the following versions of R\cite{Rsystem}, the
operating system, and add-on packages \code{Hmisc}\cite{Hmisc},
\code{rms}\cite{rrms}, and others:
<<echo=f,results=tex>>=
toLatex(sessionInfo(), locale=FALSE)
@

% Note: Rsystem reference is defined inside feh.bib.  It is a slightly
% edited version of the output of citation().
\bibliography{/home/harrelfe/bib/feh.bib}
\bibliographystyle{unsrt}
% Use \bibliographystyle{abbrv} if want references alphabetized
\end{document}

Using SweaveListingUtils | Output

This template assumes you have installed the R package SweaveListingUtils which allows you to greatly beautify how the R code, comments, and output are typeset. The LaTeX package listings is assumed to be installed on your system (it is on most). Note that there is a Linux package rubber you can install that automatically runs latex and bibtex as many times as needed. Run it using for example rubber model to compile model.tex. Use rubber -d model to run pdflatex in place of latex.
%Usage: R CMD Sweave template.Rnw
% Runs model.Rnw to produce template.tex and graphics files in graphics/
% latex or pdflatex template; bibtex template; latex or pdflatex template
% (leave out last two steps if there are no bibliographic citations,
% or automate with rubber -d template)
% To get .R file: Stangle template
\documentclass{article}
\usepackage{relsize,setspace}  % used by latex(describe( ))
\usepackage{url}               % used in bibliography
\usepackage[superscript,nomove]{cite} % use if \cite is used and superscripts wanted
% Remove nomove if you want superscripts after punctuation in citations
\usepackage{lscape}            % for landscape mode tables
\textwidth 6.75in              % set dimensions before fancyhdr 
\textheight 9.25in
\topmargin -.875in
\oddsidemargin -.125in
\evensidemargin -.125in
\usepackage{fancyhdr}          % this and next line are for fancy headers/footers
\pagestyle{fancy}
\newcommand{\bc}{\begin{center}}  % abbreviate
\newcommand{\ec}{\end{center}}

% The next 10 lines are used if you are beautifying R code and output with SweaveListingUtils
\usepackage{listings}
\usepackage[nogin]{Sweave}
% Use nogin to respect height and width as specified below, otherwise all widths will be changed to 80% of the text width
\SweaveOpts{keep.source=TRUE}
<<<SweaveListingspreparations, results=tex, echo=FALSE>>=
require(SweaveListingUtils)
SweaveListingPreparations()
spar <- function(mar=c(3.25+bot-.45*multi,3.5+left,.5+top+.25*multi,.5+rt),
                 lwd = if(multi)1 else 1.75,
                 mgp = if(multi) c(1.5, .365, 0) else c(2, 0.475, 0),
                 tcl = if(multi)-0.25 else -0.4,
                 bot=0, left=0, top=0, rt=0, ps=14,
                 mfrow=NULL, ...)
  {
    multi <- length(mfrow) > 0
    par(mar=mar, lwd=lwd, mgp=mgp, tcl=tcl, ps=ps, ...)
    if(multi) par(mfrow=mfrow)
  }
options(SweaveHooks=list(fig=spar)) # run spar() before every plot
options(prompt='  ',continue='  ')  # remove prompt characters at start of lines
@

% To control colors, use something like the following:
% r <- function(x) col2rgb(x)/255
% SweaveListingPreparations(Rcolor=r('green'), Rcomment=r('red'), Rout=r('blue'))
% For b+w documents use Rcolor=r('black'),Rcomment=r(gray(.3)),Rout=r('black')

\title{An Analysis}
\author{Jane Doe\\\smaller Department of Biostatistics\\\smaller Vanderbilt University School of Medicine}

\begin{document}
% To produce both postscript and pdf graphics, remove the eps and pdf
% parameters in the next line.  Set default plot size to 6x4 in.
\SweaveOpts{prefix.string=graphics/plot, eps = FALSE, pdf = TRUE}
\SweaveOpts{width=6, height=4}

%\setkeys{Gin}{width=0.85\textwidth}  will undo width=6, height=4
\maketitle
% Use the following 3 lines for long reports needing navigation
\tableofcontents
%\listoftables
%\listoffigures     % not used unless figure environments used
% results=tex below makes SweaveListingUtils highlight function names in
% Hmisc and Design
<<echo=F,results=tex>>=
options(Hverbose=FALSE) # Silently load Hmisc and Design
require(Design)
# The following line is only needed if the prselect function is needed.
# prselect will be added to the next version of Hmisc.  It allows for
# selective deletion of R output in the report.
source('http://biostat.mc.vanderbilt.edu/cgi-bin/viewvc.cgi/*checkout*/Hmisc/trunk/R/Misc.s')
@

\section{Descriptive Statistics}
<<results=tex>>=
getHdata(support)   # Use Hmisc/getHdata to get dataset from VU DataSets wiki
d <- subset(support, select=c(age,sex,race,edu,income,hospdead,slos,dzgroup,
                       meanbp,hrt))
latex(describe(d), file='')
@
Race is reduced to three levels (white, black, OTHER) because of low
frequencies in other levels (minimum relative frequency set to 0.05).
<<>>=
d <- upData(d,
            race = combine.levels(race, minlev = 0.05))
@

\section{Redundancy Analysis and Variable Interrelationships}
\bc
% Note: giving a chunk name to each code chunk that produces a figure
% makes it easy to know which plots to send to a collaborator, and
% will not allow numbered orphan plots to be left when code chunks are
% inserted into the file.  The default in Sweave is for plots to be
% numbered by the chunks producing them.
<<vc,fig=T>>=
v <- varclus(~., data=d)
plot(v)
redun(~age+sex+race+edu+income+dzgroup+meanbp+hrt, data=d)
# Alternative: redun(~., data=subset(d, select=-c(hospdead,slos)))
@
\ec
Note that the clustering of black with white is not interesting; this just means that these are mutually exclusive higher frequency categories, causing them to be negatively correlated.
\section{Logistic Regression Model}
Here we fit a tentative binary logistic regression model.  The
coefficients are not very useful so they are not printed.
<<z,eval=FALSE,echo=T>>=
dd <- datadist(d); options(datadist='dd')
f <- lrm(hospdead ~ rcs(age,4) + sex + race + dzgroup + rcs(meanbp,5),
         data=d)
f
<<echo=F>>=
z <- capture.output( {
<<z>>
   } )
prselect(z, 'S.E.') # keep only summary stats; or:
# prselect(z, stop='S.E.', j=-1)  # keep only coefficients
@
<<results=tex>>=
latex(anova(f), where='h', file='')   # can also try where='htbp'
@

\section{Test Calculations}
\bc
<<testp,fig=T>>=
x <- 3; y <- 2
if(x <= y) 'this' else 'that'
if(y >= x) 'that' else 'this'
x^y
plot(runif(20),runif(20))
@
\ec

\section{Computing Environment}
These analyses were done using the following versions of R\cite{Rsystem}, the
operating system, and add-on packages \code{Hmisc}\cite{Hmisc},
\code{Design}\cite{Design}, and others:
<<echo=F,results=tex>>=
toLatex(sessionInfo(), locale=FALSE)
@

% Note: Rsystem reference is defined inside feh.bib.  It is a slightly
% edited version of the output of citation().
\bibliography{/home/harrelfe/bib/feh.bib}
\bibliographystyle{unsrt}
% Use \bibliographystyle{abbrv} if want references alphabetized
\end{document}

If you have a master document that does not produce any R graphics, and a series of sections in separate .Rnw files, the master document can contain the setup up to but not including the definition of spar, and each separate .Rnw file to be \input from the master document should start with
<<echo=FALSE>>=
spar <- function(mar=c(3.25+bot-.45*multi,3.5+left,.5+top+.25*multi,.5+rt),
                 lwd = if(multi)1 else 1.75,
                 mgp = if(multi) c(1.5, .365, 0) else c(2.4-.4, 0.475, 0),
                 tcl = if(multi)-0.25 else -0.4,
                 bot=0, left=0, top=0, rt=0, ps=14,
                 mfrow=NULL, ...)
  {
    multi <- length(mfrow) > 0
    par(mar=mar, lwd=lwd, mgp=mgp, tcl=tcl, ...)
    if(multi) par(mfrow=mfrow)
  }
options(SweaveHooks=list(fig=spar)) # run spar() before every plot
options(prompt='  ',continue='  ')  # remove prompt characters at start of lines
@

\SweaveOpts{prefix.string=plot, eps = FALSE, pdf = TRUE, keep.source=TRUE}
\SweaveOpts{width=6, height=4}

You may have to have require or library commands in each section, as you run each section separately using e.g. R CMD Sweave section.Rnw.

Using LaTeX figure Environments for Plots

If you want plots to float, have captions, or you want to reference figures symbolically in your report (e.g., See Figure \ref{myfig}) you need to enclose the figures in a figure environment. You can do this in standard Sweave by having \begin{figure} and \end{figure} enclose the code chunk, but this will put the R code at the top of the figure. To have full control of the issue, use the following pattern. If you are using Sweave to make a presentation, you will want figure setup and closure (dev.off) commands to be suppressed from the final pdf file. This can be done by adding #rm# to any line you want to disappear, and running the .tex file created by Sweave through the rmlines shell script, e.g., rmlines my.tex.
% Put the following in your master =.tex= or =.Rnw= file:
%\fg{basefilename}{label}{caption}
\newcommand{\fg}[3]{\begin{figure}[htbp]%
        \leavevmode%
        \centerline{\includegraphics{#1}}%
        \caption{#3}%
        \label{#2}%
        \end{figure}}
%\fgh{basefilename}{label}{caption}{short caption for TOC}
\newcommand{\fgh}[4]{\begin{figure}[htbp]%
        \leavevmode%
        \centerline{\includegraphics{#1}}%
        \caption[#4]{#3}%
        \label{#2}%
        \end{figure}}

% Put at the top of each =.Rnw= file:
<<echo=f>>=
ppdf <- function(file, w=4.5, h=3, ...)   # set your own default height and width
  {
    pdf(paste(substitute(file),'pdf',sep='.'), width=w, height=h)
    # use e.g. paste('pdf/',substitute(file),'.pdf',sep='') to put in subdirectory
    spar(...)
  }
doff <- function() invisible(dev.off())   # invisible to prevent R output
spar <- function(mar=c(3.25+bot-.45*multi,3.5+left,.5+top+.25*multi,.5+rt),
                 lwd = if(multi)1 else 1.75,
                 mgp = if(multi) c(1.5, .365, 0) else c(2.4-.4, 0.475, 0),
                 tcl = if(multi)-0.25 else -0.4,
                 bot=0, left=0, top=0, rt=0, ps=14,
                 mfrow=NULL, ...)
  {
    multi <- length(mfrow) > 0
    par(mar=mar, lwd=lwd, mgp=mgp, tcl=tcl, ps=ps, ...)
    if(multi) par(mfrow=mfrow)
  }
# Default: par(mar=c(3.25,3.5,.5,.5))
options(SweaveHooks=list(fig=spar)) # run spar() before every plot
options(prompt='  ',continue='  ')  # remove prompt characters at start of lines
@

# Make a plot.  File name will be summary.pdf in current working directory
# Modify ppdf if you want to automatically put graphics in a subdirectory
<<>>=
ppdf(summary)   #rm#
# Use e.g. ppdf(summary, h=4, w=5) to change size or ppdf(myplot, mfrow=c(1,2)) for multiple plots
plot(f) # Figure \ref{zz}  [note zz can be a single word or more than one word separated by .]
doff() #rm#
@ 
 # s2latx.pl called by Sweavev will automatically remove lines ending in doff() from the listing without needing #rm#
\fg{summary}{zz}{Univariable summaries}


See also http://addictedtor.free.fr/misc/sweave for results of Romain Francois' highlight approach.
Topic attachments
I Attachment Action Size Date Who Comment
Sweavel.stysty Sweavel.sty manage 3.6 K 16 Oct 2015 - 16:50 FrankHarrell Enhanced Sweave style for LaTeX, using LaTeX listings package
Sweavev.stysty Sweavev.sty manage 2.5 K 18 Jul 2009 - 09:20 FrankHarrell Enhanced Sweave style for LaTeX using LaTeX fancyvrb package
rmlinesEXT rmlines manage 0.1 K 18 Jul 2009 - 09:03 FrankHarrell Shell script for removing lines in a file that contain #rm#
template.pdfpdf template.pdf manage 143.6 K 14 Jun 2009 - 23:10 FrankHarrell Result of R CMD Sweave template.Rnw then rubber -d template
template3.pdfpdf template3.pdf manage 318.3 K 19 Mar 2011 - 10:04 FrankHarrell Example report using the Sweavel LaTeX style template
templatep.pdfpdf templatep.pdf manage 163.5 K 28 Jun 2009 - 10:19 FrankHarrell Result of pdflatex --shell-escape templatep; using Sweavel style
Topic revision: r30 - 01 Nov 2015, FrankHarrell
 

This site is powered by FoswikiCopyright © 2013-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback