I work with lots of data. Oftentimes I need to tell my program, “Hey, this is a heavy function. Save the result, OK?” This is the role of caching libraries. There are many available on CRAN for R code, but none fit the philosophy I had when working on my thesis.
The philosphy of stash
is that the user of this library, like me, is
rushed and forgetful. This leads to several design
considerations:
The most important function do.call.stash
, which mimics the syntax of
base R’s do.call
.
library(stash)
# Default cache location is ~/.stash-r/cache/"
# For a demo, put it in a temporary directory.
cache_dir <- file.path(tempdir(), "readme-test")
dir.create(cache_dir)
options(stash.cache_path = cache_dir)
heavy_function <- function(x) {
Sys.sleep(3)
mean(x)
}
result <- NULL
system.time({result <- do.call.stash(heavy_function, list(c(1:20)))})
## user system elapsed
## 0.003 0.000 3.005
result
## [1] 10.5
system.time({result <- do.call.stash(heavy_function, list(c(1:20)))})
## user system elapsed
## 0.001 0.000 0.001
result
## [1] 10.5
The other is setting the directory to store cached results, through
options(stash.cache_path = "the/path/to/your/cache/")
. This call is
visible above.
For comments, questions, and requests related to this library, Github issues are best.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.