Description Usage Arguments Value Examples
Create function which cache result based on file. If file will be changed, function will also be recalculated, otherwise cached value will be returned
1 |
fnc |
function that operates on file (first argument must be path to file). |
Return new function with caching functionality
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | fnc <- function(file) {
n <- as.numeric(readLines(file, warn = FALSE))
rnorm(n)
}
file <- tempfile()
cat(5, file = file)
all.equal(fnc(file), fnc(file))
fcached <- file_modification_time_cache(fnc)
all.equal(fcached(file), fcached(file))
x <- fcached(file)
cat(5, file = file)
y <- fcached(file)
all.equal(x, y)
fnc2 <- function(file, k = 2) {
n <- as.numeric(readLines(file, warn = FALSE))
rnorm(n * k)
}
fcached2 <- file_modification_time_cache(fnc2)
all.equal(fcached2(file,1), fcached2(file,1))
all.equal(fcached2(file,1), fcached2(file,2))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.