Sweave
‘Sweave’ provides a flexible framework for mixing LATEX and R code
for automatic report generation. The basic idea is to replace the
R code with its output, such that the final document only contains
the text and the output of the statistical analysis.
Execute hello.Rnw in R environment
>Sweave("hello.Rnw")
Following is the content of hello.Rnw
hello.Rnw
\documentclass[a4paper]{article}
\title{Sweave Example 1}
\begin{document}
\maketitle
In this example we embed parts of the examples from the
\texttt{kruskal.test} help page into a \LaTeX{} document:
Here comes the big one
<<>>=
@
which shows that the ACF for data set ldeaths
include a plot of the data:
\begin{center}
<<fig=TRUE,echo=FALSE>>=
acf(ldeaths)
@
\end{center}
\end{document}
>Sweave("hello.Rnw")
will create file hello.tex
hello.tex
\documentclass[a4paper]{article}
\title{Sweave Example}
\usepackage{Sweave}
\begin{document}
\maketitle
In this example we embed parts of the examples from the
\texttt{kruskal.test} help page into a \LaTeX{} document:
Here comes the big one
which shows that the ACF for data set ldeaths
include a plot of the data:
\begin{center}
\includegraphics{hello-002}
\end{center}
\end{document}
Another Snippet
\documentclass[a4paper]{article}
\title{Sweave Example 1}
\SweaveOpts{echo=true}
\begin{document}
\maketitle
\section{ACF}
In this example we embed parts of the examples from the
\texttt{ACF} help page into a \LaTeX{} document:
Here comes the big one
<<>>=
@
which shows that the ACF for data set ldeaths
include a plot of the data:
\begin{center}
<<fig=TRUE,echo=FALSE>>=
acf(ldeaths)
@
\end{center}
<<echo=TRUE>>=
x <- rnorm(100)
xm <- mean(x)
xm
@
However
<<echo=TRUE,results=hide>>=
x <- rnorm(100)
xm <- mean(x)
xm
@
Also
<<echo=FALSE>>=
x <- rnorm(100)
xm <- mean(x)
xm
@
\section{Histogram}
\setkeys{Gin}{width=1.0\textwidth}
<<echo=TRUE,fig=TRUE,width=7,height=4>>=
hist(ldeaths)
@
\section{A Figure}
\begin{figure}[htbp]
\begin{center}
\setkeys{Gin}{width=\textwidth}
<<echo=FALSE,fig=TRUE,width=4,height=4>>=
x = runif(100, 1, 10)
y = 2 + 3 * x + rnorm(100)
lm.xy = lm(y ~ x)
plot(x,y)
abline(lm.xy)
@
\caption{Regression of $y$ on $x$}
\label{fig:regression}
\end{center}
\end{figure
\section{Something More}
First we define a figure hook:
<<results=hide>>=
options(SweaveHooks = list(fig = function() par(mfrow=c(2,2))))
@
Then we setup variable definitions without actually evaluating them
<<xydef,eval=false>>=
x <- 1:10
y <- rnorm(x)
@
Then we put the pieces together:
\begin{center}
<<fig=T>>=
<<xydef>>
lm1 <- lm(y~x)
summary(lm1)
plot(lm1)
@
\end{center}
<<echo=false,results=hide>>=
library(lattice)
library(xtable)
data(cats, package="MASS")
@
\section{The Cats Data}
Consider the \texttt{cats} regression example from Venables \& Ripley
(1997). The data frame contains measurements of heart and body weight
of \Sexpr{nrow(cats)} cats (\Sexpr{sum(cats$Sex=="F")} female,
\Sexpr{sum(cats$Sex=="M")} male).
A linear regression model of heart weight by sex and gender can be
fitted in R using the command
<<>>=
lm1 = lm(Hwt~Bwt*Sex, data=cats)
lm1
@
Tests for significance of the coefficients are shown in
Table~\ref{tab:coef}, a scatter plot including the regression lines is
shown in Figure~\ref{fig:cats}.
\SweaveOpts{echo=false}
<<results=tex>>=
xtable(lm1, caption="Linear regression model for cats data.",
label="tab:coef")
@
\begin{figure}
\centering
<<fig=TRUE,width=12,height=6>>=
trellis.par.set(col.whitebg())
print(xyplot(Hwt~Bwt|Sex, data=cats, type=c("p", "r")))
@
\caption{The cats data from package MASS.}
\label{fig:cats}
\end{figure}
\begin{center}
\end{center}
\end{document}