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.

2 Answers 2


Brief answer:

Apply these options: prefix=T then eval=F

Long answer:

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:



The A chunk

a <-2+3

The B chunk 

b <- a+3
cars <- c(1, 3, 6, 4, 9)

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
    Commented Jan 15, 2013 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
    Commented Jan 15, 2013 at 21:32

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 Xie
    Commented Jan 15, 2013 at 5:11
  • So this is compiled the same way I would do a sweave document? Can I do this through emacs?
    – nick
    Commented Jan 15, 2013 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
    Commented Jan 15, 2013 at 19:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .