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.
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.
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.