Skip to content

intro to sweave

Paul Magwene edited this page Sep 3, 2011 · 1 revision

Introduction to Literate Programming

Sweave for R

Sweave documents weave together documentation/discussion and code into a single document. The pieces of code and documentation are referred to as ‘chunks’. R comes with a set of tools that allow you to extract just the code, or to turn the entire document into a nicely formatted report.

Here’s a simple Sweave document to get you started:

\documentclass{article}
\begin{document}

This is a very simple Sweave file. It includes only a single
code chunk.

<<>>=
z <- rnorm(30, mean=0, sd=1)
summary(z)
@

That code chunk generated a random sample of 30 observations 
drawn from a normal distribution with mean zero and standard 
deviation one.

\end{document}  

Type those lines into a text editor and save the document with the name sweave1.Rnw.

Let’s break down the various pieces of the document. The first two lines and the last line represent LaTeX commands.

\documentclass{article}
\begin{document}
    ....
\end{document}

For simple Sweave documents that's all the LaTeX you need to learn. However, learning a little bit more about the  document preparation system gives you the option of producing very nicely formatted output as we’ll see in a little bit.

The R code is preceeded by the text <<>>=. This tells Sweave that you’re starting a code chunk. The @ symbol after the code chunk tells Sweave that you’re going back to writing documentation chunks.

If you already have a working installation of LaTeX on your computer you can now compile this into a nicely formatted document from the R interpretter. Change the R working directory so that it’s in the same directory where you saved the sweave1.Rnw file. The execute the following commands:

> library(tools)  # makes the texi2dvi function available
> Sweave('sweave1.Rnw') # compiles our Sweave document into 'sweave1.tex'
> texi2dvi("sweave1.tex", pdf = TRUE)

These commands will produce two new documents – sweave1.tex and a PDF document, sweave1.pdf. You can open the sweave1.tex file in any text editor and you’ll see that it’s just a slightly modified version of the sweave1.Rnw file you created. The PDF file contains the nicely formatted report.

If you got an error message the most likely reason is that R can’t find the path to your LaTeX executable. To check this try:

> texi2dvi('sweave1.tex', pdf=TRUE, quiet=FALSE)

If that's the case, you can fix that by typing the following into the R command line (on OS X):

> Sys.setenv("PATH" = paste(Sys.getenv("PATH"),"/usr/texbin",sep=":"))  

Then try executing the texi2dvi command again. If that solved your problem you can make this permanent by adding that line to your .Rprofile (located in /Users/yourname on OS X; if the file doesn’t already exist go ahead and create it). If that doesn’t work please see me for troubleshooting help.

RStudio makes Sweaving easy!

RStudio hides some of the complexity of Sweaving documents. Simply create or open your Sweave document (use the .Rnw extension) in RStudio and then hit the Compile PDF button. If your LaTeX setup is working, and the document and code are valid, Rstudio will compile everything behind the scenes and pop up a nice PDF.

A fancier Sweave document

Let’s get a little bit fancier and show how we can create graphics and use some LaTeX formatting features to produce a nicer document.

\documentclass[letterpaper]{article}
\usepackage[margin=0.75in]{geometry}

\title{My Second Sweave Report}
\author{John Q. Public}

\begin{document}
\maketitle
This is a still a simple Sweave file. However, 
now it includes several code chunks and several 
\LaTeX\ specific commands.

\section{Sampling from the random normal distribution}

<<>>=
z <- rnorm(30, mean=0, sd=1)
summary(z)
@

That code chunk generated a random sample of 30 
observations drawn from a normal distribution with mean 
zero ($\mu = 0$) and standard deviation one ($\sigma = 1$).


\section{Generating figures}

We can also automatically imbed graphics in our 
report. For example, the following will generate 
a histogram.

% this tells Sweave to set the graphics 
% to be half the width of the text
\setkeys{Gin}{width=0.5\textwidth} 
<<fig=TRUE>>=
hist(z)
@

\end{document}    

Notice how we put an argument, fig=TRUE within the second code chunk delimiter. This will tell Sweave to automatically imbed a figure with the histogram graphic we created into our report. Save this as sweave2.Rnw and repeat the above steps to compile it into a PDF report.

For a full overview of Sweave’s capabilities see the documentation for Sweave availabe at http://www.stat.uni-muenchen.de/~leisch/Sweave/.

Pweave for literate programming in Python

Pweave uses almost exactly the same syntax as Sweave to delimit code and document chunks. Here's a simple Pweave document.

\documentclass[letterpaper]{article}
\usepackage[margin=0.75in]{geometry}
\usepackage{graphicx} % unlike Sweave, Pweave doesn't
                      % pull this in automatically

\title{My First Pweave Report}
\author{John Q. Public}

\begin{document}
\maketitle
This is a still a simple Pweave file. As in our Sweave example,
there are several code chunks and we've included a figure.

\section{Sampling from the random normal distribution}

<<>>=
from numpy import random
z = random.normal(loc=0, scale=1, size=30)
@

That code chunk generated a random sample of 30 
observations drawn from a normal distribution with mean 
zero ($\mu = 0$) and standard deviation one ($\sigma = 1$).


\section{Generating figures}

We can also automatically imbed graphics in our 
report. For example, the following will generate 
a histogram.

% this tells Sweave to set the graphics 
% to be half the width of the text
\setkeys{Gin}{width=0.5\textwidth} 
<<fig=True>>=
import pylab
pylab.hist(z)
@

\end{document}

Note that the second code chunk, we wrote fig=True in the Pweave document, whereas we wrote fig=TRUE for the Sweave document. This minor difference reflects the different syntax for boolean values in R and Python.

Save that code in a text file called pweave1.Pnw and from the bash shell (not in the Python interpretter) type the following command:

Pweave -f "tex" pweave1.Pnw

The option -f "tex" tells Pweave to output a file (Note: from the Windows command prompt you must use double quotes around "tex", on Unix-based systems either single our double quotes work fine). Assuming you got no error messages, you can then compile this to PDF using the following command:

pdflatex pweave1.tex

Clone this wiki locally