Sweave Intro

Why Sweave?
Makes research and analysis reproducible.
All code, analysis, and interpretation are in one document.
Sweave is included in every R install. It's used to create R help pages if you are building a package.



If you're brand new to Sweave, I recommend that you instead use knitr instead. It requires R version 2.14 or better, but it has some advantages, and you need either it or Sweave, not both.

Setup

One easy way is to run Sweave in Rstudio.
You also need a latex installation (in unix install texlive-full). For Windows,MikTeX
Other cross platform editors:
TexWorks or
TexMaker
And of course, you could use emacs, ess, and auctex.
You may need to copy the file Sweave.sty from it's folder in the R install to a tex-site folder for latex to find it easily.

Quick outline of the process

  1. Create a new folder for this (and every) project.

  2. Create an Sweave document ending in .Rnw (example below)

  3. Sweave the file though R to create a latex file

  4. LaTeX the resulting file to get a pdf.

Rstudio does 3 and 4 in one click.

RNW file template

A very basic LaTeX file. Futher down I have one complete with analysis.

\documentclass[11pt]{article}
\usepackage{graphicx, verbatim}
\setlength{\textwidth}{6.5in} 
\setlength{\textheight}{9in}
\setlength{\oddsidemargin}{0in} 
\setlength{\evensidemargin}{0in}
\setlength{\topmargin}{-1.5cm}

\begin{document}

\begin{center}
{\bf \Large Stat 500 Assignment 1} \\
 \end{center}


\end{document}

For more info on LaTeX, search for a LaTeX tutorial.

Include code chunks in the LaTeX document

All R code goes into a special block demarked with:

<<>>=
  cat("This is my R code.\n")
@

which must begin in column 1.
Options go inside the double inequalities:

To print an R expression use a \Sexpr{} statement like this:
Given that area and perimeter are in the model, shape has p-value of
\Sexpr{round(AIC(rock.lmfit), 3)}.

Example Analysis

Copy this code to a .Rnw file and run it with Rstudio.

\documentclass[11pt]{article}
\usepackage{graphicx, verbatim}
\setlength{\textwidth}{6.5in} 
\setlength{\textheight}{9in}
\setlength{\oddsidemargin}{0in} 
\setlength{\evensidemargin}{0in}
\setlength{\topmargin}{-1.5cm}

\begin{document}
\begin{center}
{\bf \Large Stat 500 Assignment 1\\}
 \end{center}
 
 Using the rock data in the datasets package. First we do some plots:\\
 %%\setkeys{Gin}{width=.3\linewidth}
<<>>=
 data(rock)
  require(ggplot2)
  plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red")
   print(plot1 )
@
%%  lattice and ggplots must be inside a print statement to show up
<<>>=
   print(plot1 + aes(x = peri) )
@
<<>>=
   print(plot1 + aes(x = shape) )
@

 Now go back and remove the \%\% comments on the line 15 above here  
 to set each plot width to .3
 times linewidth, and the three plots should fit on one line.  If you leave a blank line between the three code chunks above, they will start on new lines.

Summary of the linear model:
<<>>=
  rock.lmfit <- lm(log(perm) ~ ., rock)
  summary(rock.lmfit)
@

<<>>=
 xtable::xtable( summary(rock.lmfit)$coef, digits=5)
@
<<>>=
 rock.rlmfit = MASS::rlm( log(perm) ~ ., rock)
 xtable::xtable( summary(rock.rlmfit)$coef, digits = 4)
  ## assumes that you have the xtable package available. It creates latex tables.
@

To print any R object use a \verb|\Sexpr{any_R_object}| statement like this:
AIC of the linear model is \Sexpr{AIC(rock.lmfit)}.  
 
 
\end{document}

Running the .Rnw file

First make sure that each chunk of R code works.

In Rstudio, click “Compile pdf” buttom.
In Emacs M-n s for Sweave (step 1), M-n P for pdflatex (step 2).
To find bugs:
You can extract just the R code into another file via Stangle("file.Rnw")

To learn more:

TeXWorks easy LaTeXing
Sweave manual
Using Sweave in TexWorks
Using Sweave in TexMaker
cacheSweave for caching parts of the R code. Then they only get run again if the code gets changed. Needed for large projects with complex code.
pgfSweave allows caching graphics and use of tikzDevice so that fonts in plots are the same as those in the other parts of the document.
R taskview on reproducible research.
odfWeave creates reproducible research documents in odf (open office) format. These can then be saved as Word documents.
Speaking of converting to different formats (html, doc, rtf) , see SWeave conversions from Frank Harrell
Frank Harrell's Sweavel.sty

Acknowledgements:

King Jones convinced me to try Sweave. You can see some of his work here. He's using the Sweavel.sty file just above.


Email address:

Last Updated: