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.
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.
Create a new folder for this (and every) project.
Create an Sweave document ending in .Rnw (example below)
Sweave the file though R to create a latex file
LaTeX the resulting file to get a pdf.
Rstudio does 3 and 4 in one click.
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.
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:
A name for this code chunk followed by a comma.
echo
= TRUE, if the code should show up here
(default). otherwise, add otherwise echo
= FALSE,.
results
= verbatim, (default) to show output
here,results = hide,
to hide it, results
= tex, for latex output, usually of a
table
Figure inclusion.
One code block can create only one plot.
Add the option fig
= TRUE,.
By default it
creates both pdf and eps figures, so you may want to add eps
= FALSE, to just use pdf.
If you want to place
the figure yourself, add include
= FALSE,.
Add size options (in
inches) as needed: height
= 4, width=5, which set the relative
size, or aspect of the plot.
If using lattice or
ggplot graphics, put a print()
around the plotting function. Otherwise, nothing will
show in the file.
The plot gets saved as "RnwFilename-CodeChunkName.pdf", and you can use the latex \includegraphics command to pull it into the document where desired.
Size of output graphic is by default 85% of line width. Change that setting as needed with:
\setkeys{Gin}{width=.5\linewidth}This interacts with (5). If the original plot is too small, fonts may get expanded to fill the paper area and look too large. If original is too big, labels may become too small to read.
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)}.
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}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")
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
King Jones convinced me to try Sweave. You can see some of his work here. He's using the Sweavel.sty file just above.