# Sweave documents: saving output for later use

I am fairly new to the idea of sweave documents. My document is a .Rnw file containing LaTeX code and R code. Part of my R code is computationally intensive and delays the compilation of my .Rnw file. Once I compile it, it looks all nice and pretty, but if I want to add more information or make some changes, then it takes a while to produce those results.

Is there an option, command or some way to tell the compiler to save the output and use it next time the .Rnw file is compiled? If it helps, I am using ESS through emacs on a ubuntu machine.

-

I think you want knitr or perhaps one of the Sweave variants that it builds on.

-
the solution with knitr is pretty simple: just use the chunk option cache=TRUE; everything will be saved and loaded automatically –  Yihui Jan 15 '13 at 5:11
So this is compiled the same way I would do a sweave document? Can I do this through emacs? –  nick Jan 15 '13 at 17:09
@nick I believe that the compilation process is similar to Sweave and that you can use emacs, but I don't use knitr myself. –  G. Poore Jan 15 '13 at 19:34

Apply these options: prefix=T then eval=F

Make the figures as separate files, then disable the chunk evaluation and include PDF of the figure.

• prefix=T: filenames of figures are generated with common prefix (by default the name of the source file).

• eval=F: The code chunk is not evaluated, and hence no text or graphical output created.

Play with the options in this MWE:

\documentclass{article}

\begin{document}

The A chunk

<<a,echo=T,eval=T,results=hide>>=
a <-2+3
a
@

The B chunk

<<b,echo=T,eval=T,fig=T,prefix=T,results=verbatim>>=
b <- a+3
b
cars <- c(1, 3, 6, 4, 9)
plot(cars)
@
\end{document}


Other options of some interest could be results=hide (output is suppressed, but code is executed).

However, even if you can disable the evaluation or the ouput of some chunks, may be this cannot help significantly with some large documents, as the result of a chunk often depend of earlier chunks, that is a pain switch on and off to avoid the rendering of some figures.

To test some chunk at the beginning or middle of the document and skipping all subsequent chunks, the faster is simply add a \end{document} after it.

`

-
If I understood your response, it sounds like I need to run the .Rnw file. Save the figures and such to separate files. Then when re-running the file, change all chunks to eval=FALSE and manually load the figures? Am I completely voiding the initial reason to use sweave in the first place? –  nick Jan 15 '13 at 15:58
Well, it's a pain, but it is a way to do it :-). What I would do is just put eval = F every finished figure, and only at the very end put eval=T in every chunk and only then worry about the overall look. Knitr option is also a good alternative although I prefer Sweave as it was faster in some of my documents. –  Fran Jan 15 '13 at 21:32