Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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.

share|improve this question

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer
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'. –  Clayton Stanley Sep 25 '12 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 '12 at 0:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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