I spent the last week learning 3 new tools: R, Sweave, and LaTeX. One question that came to my mind when working through my first project: Where do I place the majority of the R code?

The tutorials that I read online placed the majority of the R code in the LaTeX .Rnw file. However, I find having a bunch of R calculations in the LaTeX file distracting. What I do find extremely helpful (of course) is to call out to R code in the LaTeX file and embed the result.

So the workflow I've been using is to place 99% of my R code in my .R file. I run that file first, save a bunch of calculations as objects, and output the .Rout file once finished (to save the work). Then when running Sweave, I load up that .Rout file, so that I have the majority of my calculations already completed and in the Sweave R session.

Then my LaTeX callouts to R are quite simple: Just give me the XTable stored in 'res.table', or give me the result of an already-computed calculation stored in the variable 'res'.

So I push towards the minimal amount of R code in the LaTex file possible, to achieve the desired result (embedding stats results in the LaTeX writeup).

Does anyone have any experience with this approach? I'm just worried I might run into trouble further down the line, when I start really trying to load up and leverage this workflow.

2 Answers 2


I have the same obsession of keeping the code out, but I use knitr, which builds on Sweave. It might solve your issue if the issue is to avoid the .Rout file.

  • How do you keep most of the R code out of the .Rnw file when using knitr? It seems that knitr builds on cacheSweave, and cacheSweave provides automatic memoization of R chunks in the .Rnw file, but as far as I can tell; with cacheSweave (and knitr?) those chunks 'still have to be in the .Rnw file'. Sep 25, 2012 at 15:45
  • You won't find yourself pushing the code out if you are working in an entirely cached environment: knitr gives you the option to work with a minimal amount of files. I have a few TeX documents using Sweave where I coded like you are doing, but the newer ones use knitr and the change has also affected that aspect of the code. I find single files much, much safer, especially for TeX.
    – Fr.
    Sep 30, 2012 at 0:07

In order to keep code out of my .Rmd I've started writing a package with all the functions in it.

This may be with a slightly different intent than your workflow, but my project is repetitive enough that I can generalise the code into functions, with the goal of the project being to use it as a sort of super mail-merge to generate a document per data set from a large repository.

This satisfies the criteria of removing chunky R code blocks that you then want to hide, though it doesn't do much for reducing the processing time.

An alternative I've seen around suggests using source(), though that would, to my limited understanding just mirror your current process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.