\documentclass[nogin,12pt]{article} \usepackage[margin=1in]{geometry} % By default Sweave will produce both pdf and eps files % when asked to do graphics. When using the pdflatex % to typeset, the eps file is unnecessary; this suppresses it. \SweaveOpts{eps=FALSE} \begin{document} \section{Basics} This is a basic Sweave document. We can do arithmetic. <<>>= 1+1 @ We can save a value in a variable and output it in text. <<>>= a<-2*pi b<-4 @ The numeric value of $2\pi$, rounded to \Sexpr{b} digits, is \Sexpr{round(a, b)}. By default, both the input and output of R commands are shown. Either or both can be suppressed, as will be demonstrated in future sections. \section{Tables} We can fit a linear model on the Chatterjee-Price Attitude Data. The \texttt{R} help file describes the data as follows. \begin{quote} From a survey of the clerical employees of a large financial organization, the data are aggregated from the questionnaires of the approximately 35 employees for each of 30 (randomly selected) departments. The numbers give the percent proportion of favourable responses to seven questions in each department. \end{quote} This data file has \Sexpr{nrow(attitude)} rows and \Sexpr{ncol(attitude)} columns. We can use the \texttt{xtable} library to output the first few rows of the data. In the source file for this document, notice that for the chunk loading this library, both input and ouput are hidden (using \texttt{echo=FALSE} and \texttt{results=hide}, respectively. And in the chunk that creates the table, we tell Sweave that the results are already LaTeX code using \texttt{results=tex}. <>= library(xtable) @ <>= xtable(head(attitude)) @ We can fit a basic linear model and output a summary. <<>>= summary(fm1 <- lm(rating ~ ., data = attitude)) @ Or we can output the table of coefficients automatically in LaTeX format using \texttt{xtable}. <>= xtable(fm1) @ \section{Plots} This is one of the places I think Sweave really shines. No more trying to keep separately keep track of the both the code that created a figure and the figure itself! When you have many figures, that gets hairy fast. You can make a simple picture just by putting plotting code in an Sweave chunk. <>= with(attitude, plot(rating, complaints)) @ \subsection{Scaling graphics} I prefer to scale pictures by having R output them in a different size, this way the text on all the pictures is always the appropriate height. To do this, use the \texttt{nogin} document option (at the beginning of your Sweave file); this tells Sweave not to scale all figures to be 80\% of the width of the page, which is otherwise the default. Then use \texttt{height=} and \texttt{width=} in calling the Sweave chunk to change the height and width. Here's the same plot but with height 2 and width 3. I've also used the \texttt{cex} parameteroption in R to change the text and point size. <>= par(cex=0.6) with(attitude, plot(rating, complaints)) par(cex=1) @ An alternative way to have Sweave create the figure but not include it; you then use \texttt{includegraphics} as usual to include the figure. To do this easily, you'll need to name the chunk; this chunk I named \texttt{myfig}; look in the source to see how. The name of the created file to include is the name of the Sweave document file (here \texttt{myfile}), followed by a hyphen and then the name of the chunk, so here it's \texttt{myfile-myfig}. In this case, I've created the first figure again and scaled it by 50\%. <>= with(attitude, plot(rating, complaints)) @ \includegraphics[scale=0.5]{myfile-myfig} \subsection{Lattice} A note about using the \texttt{lattice} library; because of the way that library creates figures, you must wrap the plot command (or its output) in an additional plot command to have success, as follows. <>= library(lattice) myplot<-xyplot(complaints~rating, data=attitude) plot(myplot) @ \subsection{Randomness in plots} And, a note about using random numbers in plots; Sweave \emph{re-executes} code chunks to create figures, so if you're not careful, you can get behaviour like this: <>= a<-round(rnorm(3),2) a plot(a, type="n") text(1:3, a, a) @ The values in the plot are different from the values in the output! Instead, put the random number generation and the plotting in two different chuncks, like this: First chunk: <<>>= a<-round(rnorm(3),2) a @ Second chunck: <>= plot(a, type="n") text(1:3, a, a) @ \end{document}