You seem to be confusing two ways of producing a pdf.
Sweave is a part of base R, and lets you include R code in an .Rnw file that will be 'sweaved' into a tex file with the results of the R code. You can then use a tex implementation to render that tex file into a PDF file.
You can also use R to produce plots and other graphic objects straight into a pdf file using the pdf() function.
To include your plot into a sweave document, replace your plot chunk with
<<name="Figure", echo=F, figure=True>>=
plot(1:10,1:10)
@
Then save your file and sweave it. sweave should produce a pdf and eps version fo your graph along with a tex document. Then run texi2dvi to produce a pdf.
knitr
has a few pretty straightforward ways of handling this.
Option 1: Using knit_child()
with inline R code
Say your setup is like the following. In the same directory, you have:
graph.R
## ---- graph
library(ggplot2)
CarPlot <- ggplot() +
stat_summary(data= mtcars,
aes(x = factor(gear),
y = mpg
),
fun.y = "mean",
geom = "bar"
)
CarPlot
chapter1.Rnw
Hey, look, a graph (Figure~\ref{fig:graph})!
<<graph, echo=FALSE, message=FALSE, fig.lp='fig:', out.width='.5\\linewidth', fig.align='center', fig.cap="A graph", fig.pos='h!'>>=
@
main.Rnw
\documentclass{article}
\begin{document}
<<external-code, echo=FALSE, cache=FALSE>>=
read_chunk('./graph.R')
@
\Sexpr{knit_child('chapter1.Rnw')}
\end{document}
Then, you can knit
the main.Rnw
file and compile the resulting .tex
file with either pdflatex
or xelatex
.
The output is:
Note that you can also read the external .R
file from the child .Rnw
file.
So, the following would have worked just as well.
chapter1-mod.Rnw
<<external-code, echo=FALSE, cache=FALSE>>=
read_chunk('./graph.R')
@
Hey, look, a graph (Figure~\ref{fig:graph})!
<<graph, echo=FALSE, message=FALSE, fig.lp='fig:', out.width='.5\\linewidth', fig.align='center', fig.cap="A graph", fig.pos='h!'>>=
@
main-mod.Rnw
\documentclass{article}
\begin{document}
\Sexpr{knit_child('chapter1-mod.Rnw')}
\end{document}
Option 2: Using chunk option child
Assuming you have graph.R
and chapter1.Rnw
from above in the same directory, then your main.Rnw
should be:
\documentclass{article}
\begin{document}
<<external-code, echo=FALSE, cache=FALSE>>=
read_chunk('./graph.R')
@
<<child-demo, child='chapter1.Rnw'>>=
@
\end{document}
Note that you can also read the external .R
file from within the child document in this case, too.
So, assuming you had graph.R
and chapter1-mod.Rnw
from above in the same directory, then your main-mod.Rnw
file should be:
\documentclass{article}
\begin{document}
<<child-demo, child='chapter1-mod.Rnw'>>=
@
\end{document}
Best Answer
I also use sweave (a few months) and latex (20 years). With a reasonably fast machine, the optimal scheme is simply to rebuild often. I use "make" (on a unix-like machine) so that only the chapters whose Rnw file has changed get rebuilt into tex files. That saves a lot of sweave time.
Alternatively, you can edit the .tex file and work away at errors, and put your results back into the .Rnw file when you're ready. But that's error-prone.
If you run sweave-latex often, and fix errors as soon as you see them, then you won't make so many errors.
The other advice is to use a decent editor (e.g. emacs) that does a lot of the work for you, e.g. finishing off parenthetic blocks, begin/end environments and so forth. That prevents 2/3 of the errors you're likely to make.