Last active
August 29, 2015 14:06
-
-
Save dholstius/1f2c85ea647435e5719e to your computer and use it in GitHub Desktop.
Evaluate an expression and save the result to a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cached <- function ( | |
file, | |
expr, | |
cache_dir = "cache", | |
compress = "xz", | |
verbose = TRUE, | |
force = FALSE | |
) { | |
file <- normalizePath(file.path(cache_dir, file), mustWork = FALSE) | |
if (!file.exists(file) || force) { | |
if (verbose) { | |
if (force) { | |
message("[cached] force:", file) | |
} else { | |
message("[cached] miss:", file) | |
} | |
} | |
if (!file.exists(dn <- dirname(file))) dir.create(dn, recursive = TRUE) | |
saveRDS(obj <- base::force(expr), file = file, compress = compress) | |
} else { | |
if (verbose) message("[cached] hit:", file) | |
obj <- readRDS(file) | |
} | |
return(obj) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment