Sweave and TeXWorks

Sweave is a tool for incorporating R into LaTeX documents.  In essence, you write a LaTeX document that contains R code.  Sweave strips out this code and runs it, and places the results back in the LaTeX document, allowing you to produce high quality documents from your analyses.  Manuals and examples can be found at the Sweave home page.

TeXWorks is an excellent editor for LaTeX that is distributed with many common LaTeX distributions, but by default it doesn’t work with Sweave.

In this guide we show how to make the two co-operate.

Installing

To make TeXWorks understand Sweave, you must install the patchDVI package, and add an Sweave action to TeXWorks.

R

The patchDVI package can be installed from within R with the command

install.packages("patchDVI", repos="http://r-forge.r-project.org")

TeXWorks

To add an Sweave action to TeXWorks under windows, start TeXWorks and

  1. From the Edit menu choose Preferences and then Typesetting.
  2. In the bottom panel, use the ‘+’ button to add a new tool.
    1. Name the tool ‘Sweave’
    2. In the Program box, browse to find  ‘Rscript.exe’ in the bin folder of your R distribution
    3. In the arguments panel, use the ‘+’ button three times to add the following arguments, each on a separate line
      1. –no-save
      2. -e
      3. patchDVI::SweavePDFMiktex(‘$fullname’,stylepath=FALSE,keep.source=FALSE)
    4. Click ‘Ok’ to return to the preferences
  3. Click ‘Ok’ to exit preferences.

Under Unix, the basic steps are the same, but you must use ‘patchDVI::SweavePDF’ instead of ‘patchDVI::SweavePDFMiktex’.

Finally, for this to work you must include the line

\SweaveOpts{concordance=TRUE}

after the begin document statement in all your Sweave documents.

Testing

Test your efforts with the example below.

  1. Open TeXWorks, paste the example text below into the window, and save it to a folder as ‘Template.Rnw’ (you may have to choose ‘All files(*)’ as the file type to stop TeXWorks wanting to call it ‘Template.Rnw.tex’).
  2. Locate the ‘Sweave.sty’ file from your R distribution (usually in share/texmf/tex/latex) and copy this to file to the same folder.
  3. In TeXworks, choose the ‘Sweave’ option in the dialog box at the top left, and hit the green arrow button.

If all goes to plan, this should produce an PDF file containing the evaluated R code.

\documentclass[a4paper]{article}
\usepackage{Sweave}
\usepackage{amsmath}

\title{Sweave Template}
\author{S. Wotherspoon}
\date{2012}

\begin{document}
\SweaveOpts{concordance=TRUE}
\maketitle

\section{Basic Usage}
\label{sec:basic-usage}

This section is just standard latex, and will appear as text in the
final document.

Include R code as follows
<<>>=
## This comment will not appear in the final document, but will be
## in the script that tangle produces
a <- 1
a+3
@

Code blocks can be evaluated but hidden with the \texttt{echo=F} option
<<echo=F>>=
## This will appear in the tangled script
a <- 5
@
or, code blocks can be shown but not evaluated with the \texttt{eval=F} option
<<eval=F>>=
## Unevaluated blocks are commented out in the tangled script
a <- 1
@
So now a is 5
<<>>=
a
@

To produce a figure use both the \texttt{fig=T} and \texttt{echo=F}
options and place the code in a figure environment, and Sweave will do
the rest.
\begin{figure}[ht]
  \centering
<<fig=T,echo=F>>=
plot(1:7)
@
  \caption{A plot.}
  \label{fig:fig1}
\end{figure}

To demonstrate how the plot is produced, produce a labelled block that
is not evaluated to display the code
<<label=plot1,eval=F>>=
plot(1:7)
@
and then reuse the labelled block to produce the figure
\begin{figure}[ht]
  \centering
<<fig=T,echo=F>>=
<<plot1>>
@
  \caption{A plot produced by a displayed code block.}
  \label{fig:fig2}
\end{figure}

\end{document}

Customizing Sweave

The default styles used by Sweave tend to be a little chunky, and are not to everyone’s liking.  Ross Ihaka has an excellent tutorial on how to customize the Sweave output to suit your taste.

This entry was posted in Guides. Bookmark the permalink.