0

I'm using cachesweave, but I don't think I get how everything works. I've tried to separate the code into simulation chunks and the plotting chunks, but some of the code is very long and written before I started the sweave document, so I instead use something like

 <<foo,cache=TRUE>>
 source("mainScript.R")
 @
 <<plot,fig=TRUE>>
 a<- print(str(F1))
 plot(F1)
 @

The thing is mainScript.R is somewhat convoluted simulation code including plot functions and so on. I've read in cacheSweave vignette "cacheSweave doesn't cache side-effects" and plots are not cached, so I was wondering if the plotting functions in mainScript.R effect how the expressions are evaluated?

This might be an obvious question. Let's say I have another chunk after the two above. all of the results of the expressions in both "foo" and "plot" can be used in this new chunk, right? For example,

 <<post-chunk>>
 print(a)
 print(str(F1))
 @
  • Just as an aside - you might want to look into knitr. It's basically a much nicer version of Sweave that has caching built in automatically and does cache plots. – Dason May 22 '12 at 21:54
  • Take a look at this answer: http://stackoverflow.com/questions/9538367/sweave-cache-packages/9561953#9561953 – jthetzel May 22 '12 at 22:29

1 Answers1

3

See Wikipedia for a full explanation. Some common side-effects in R include: print() objects, draw plots, write files and load packages.

The cacheSweave package only enables you to skip computation, and you have to lose all side-effects. As Dason commented, the knitr package is much more natural in terms of caching -- what you see in an uncached chunk will be seen in the cached chunk. The caching of side-effects in knitr is explained in its manual and the cache page in the website.

BTW, knitr keeps compatibility with Sweave and cacheSweave, so hopefully you do not need to do anything for the transition; just call library(knitr); knit('file.Rnw').

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • thank you. I was trying out knitr, but I'm new to sweave and didn't want to dive into another tool before I fully understood how weaving and caching works. So what is happening is once a chunk is cached, all packages loaded will not be available and plots drawn and output printed, will not be output the next time the document is weaved. – statistician_in_training May 25 '12 at 23:32