8

Cache

One challenge of dynamic documents is that some code chunks may take a long time to run, and these chunks may not be modified or updated frequently. In this case, caching can be very helpful. The basic idea is, a chunk will not be re-executed as long as it has not been modified since the last run, and old results will be directly loaded instead.

8.1    Implementation

Cache is not a new idea — both the packages cacheSweave and weaver have implemented it based on Sweave, with the former using filehash and the latter using *.RData images; cacheSweave also supports lazy-loading of objects based on filehash. The knitr package directly uses internal base R functions to save (tools:::makeLazyLoadDB()) and lazy-load objects (lazyLoad()

Get Dynamic Documents with R and knitr, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.