I want to start using Sweave (I'm quite familiar with LaTeX, especially form my days as a student).
Trying to get an idea of how to use it, I'm going through this intro.
I'm basically copying their example file (see below) into Rstudio and clicking "Compile PDF".
But what happens is that the text with the R output from tests etc. gets printet to a PDF with the same name as my .Rnw file, whereas the plots get printet to another file called Rplots.pdf.
How do I make it print the plots and text to the same file? I apologise in advance if there's something obvious I've missed, it's my first time using Sweave ever today.
testSweave.Rnw:
\documentclass{article}
\usepackage{graphicx, verbatim}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\topmargin}{-1.5cm}
\begin{document}
\SweaveOpts{concordance=TRUE}
\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}
I found the solution! I don't know why they omitted this in the intro, but anyway adding a "fig = TRUE" statement did the trick.
E.g. in the first chunk:
<<>>=
data(rock)
require(ggplot2)
plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red")
print(plot1)
@
All I had to do was add the stament:
<<fig = TRUE>>=
data(rock)
require(ggplot2)
plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red")
print(plot1)
@
And now it all gets printed to one file.