Sweave.sh plays with cacheSweave
[This article was first published on Gregor Gorjanc, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I have added support for caching to Sweave.sh script as implemented in cacheSweave R package written by Roger D. Peng. Now, one can set caching on for chunks that are time consuming (data import, some calculations, …) and the Sweaving process will reuse the cached objects each time they are needed. Read the details about the cacheSweave package in the package vignette. Option –cache for Sweave.sh script should also be easy to understand. However, here is a minimalist example:
\documentclass{article} \usepackage{Sweave} \begin{document} <<setup>>= n <- 10 s <- 15 @ Let us first simulate \Sexpr{n} values from a normal distribution and add a \Sexpr{s} sec pause to show the effect of caching. <<simulate, cache=true>>= x <- rnorm(n) Sys.sleep(s) @ Now print the values: <<print, results=verbatim>>= print(x) @ \end{document}Now, one can run the following command:
Sweave.sh –cache test.Rnwand the output on the command line is:
Run Sweave and postprocess with LaTeX directly from the command line - cache mode via cacheSweave R package R version 2.8.0 (2008-10-20) Copyright (C) 2008 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(package='cacheSweave'); Sweave(file='test.Rnw', driver=cacheSweaveDriver); Loading required package: filehash filehash: Simple key-value database (2.0 2008-08-03) Loading required package: stashR A Set of Tools for Administering SHared Repositories (0.3-2 2008-04-30) Writing to file test.tex Processing code chunks ... 1 : echo term verbatim (label=setup) 2 : echo term verbatim (label=simulate) 3 : echo term verbatim (label=print) You can now run LaTeX on 'test.tex'
When you repeat the Sweaving process, which you more or less always do, there is no need to wait for 15 second since cacheSweave package takes the x object from the cache! Excellent job Roger!
To leave a comment for the author, please follow the link and comment on their blog: Gregor Gorjanc.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.