cacheOperation: Cache an operation to disk or load if cache file present

View source: R/misc.R

cacheOperationR Documentation

Cache an operation to disk or load if cache file present

Description

Cache an operation to disk to save computation time in the future. A hash of the function and arguments is generated to avoid erroneous caching but any global variables or arguments excluded by EXCLUDE are not checked and are vulnerable erroneous output if they are changed.

Usage

cacheOperation(
  cacheFile,
  operation,
  ...,
  OVERWRITE = FALSE,
  VOCAL = TRUE,
  EXCLUDE = NULL
)

Arguments

cacheFile

File location to save data to

operation

A function taking ... arguments and returning object to be stored

...

Arguments for operation function

OVERWRITE

If FALSE throw an error if hash of ... changes from cached values. If TRUE redo operation and overwrite cache without asking

VOCAL

If TRUE report on status of caching

EXCLUDE

Vector of names of arguments to exclude from md5 digest comparison (for very large arguments)

Value

Output from operation function with ... arguments

Examples

cache<-tempfile()
cacheOperation(cache,mean,1:10)
cacheOperation(cache,mean,1:10)
cacheOperation(cache,mean,x=1:20,OVERWRITE=TRUE)
cacheOperation(cache,mean,x=1:20,OVERWRITE=TRUE,EXCLUDE='x')
#Note that EXCLUDEd arguments are not checked and can generate false output
y<-2
cacheOperation(cache,function(x)sum(x^y),1:10,OVERWRITE=TRUE)
#Note that global variables can generate false output
y<-3
cacheOperation(cache,function(x)sum(x^y),1:10,OVERWRITE=TRUE)
cacheOperation(cache,mean,x=1:100,OVERWRITE=TRUE,EXCLUDE='x')

sherrillmix/dnar documentation built on July 18, 2022, 10:07 p.m.