cache: Cache a function call

Description Usage Arguments Details Examples

Description

Provides a convinient method to cache the results of slowing running functions. cache will detect if the inputs to a function or the function itself changes and will automatically rerun the function and re-cache in this instance.

Usage

1
cache(input, cache_dir = getOption("cache_dir"))

Arguments

input

expression of the form g <- f()

cache_dir

directory which the cached files should be saved in

Details

The function itself it's arguments are parsed from the function call and run using rlang. The results are cached and compared to the parmeters each time the function is run. The caching using savind in external rds files. In the cache directory each cache will have a subdirectory named 'g' (the name) of the object being assigned to. In this subdirectory will be two files: 'call.rds' which contains the function and parameters and 'object.rds' which contains the object created by the slow running computation.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
## Not run: 
# define a 'slow running' function with external parameters
N <- 100
K <- 42
simulate <- function(n, k){
  Sys.sleep(10)
  rnorm(n) + k
}

# the results of this function will be cached
# the function will only be run:
# 1. the first time the function is run
# 2. if the parameters (N, K) are changed and the function is rerun

cache(x <- simulate(N, K))

## End(Not run)

Voltemand/cacheR documentation built on May 4, 2019, 6:30 a.m.