knitr::opts_chunk$set(echo = TRUE)

rawk

Caching results:

library(rawk)
set.seed(123)

file_function = function(file)
{
  n = as.numeric(readLines(file))
  rnorm(n)
}


tmpFile = tempfile()
cat("5\n", file = tmpFile)

file_function(tmpFile)

## Not match
all(file_function(tmpFile) == file_function(tmpFile))

# Cache function usage:
cache_function = file_modification_time_cache(file_function)
cache_function(tmpFile)

# fnc result is read from cached file, so in every 
# cache_function call random data will be the same
all.equal(cache_function(tmpFile, .CACHE_VERBOSE = FALSE),
          cache_function(tmpFile, .CACHE_VERBOSE = FALSE))

# file changed:
x = cache_function(tmpFile, .CACHE_VERBOSE = FALSE)
cat("5.0\n", file = tmpFile)
all.equal(x,
          cache_function(tmpFile, .CACHE_VERBOSE = FALSE))


zzawadz/rawk documentation built on May 5, 2019, 3:48 a.m.