Helper function for caching objects for long running tasks
qcache(
expr,
name,
envir = parent.frame(),
cache_dir = ".cache",
clear = FALSE,
prompt = TRUE,
qsave_params = list(),
qread_params = list()
)The expression to evaluate.
The cached expression name (see details).
The environment to evaluate expr in.
The directory to store cached files in.
Set to TRUE to clear the cache (see details).
Whether to prompt before clearing.
Parameters passed on to qsave.
Parameters passed on to qread.
This is a (very) simple helper function to cache results of long running calculations. There are other packages specializing in caching data that are more feature complete.
The evaluated expression is saved with qsave() in <cache_dir>/<name>.qs.
If the file already exists instead, the expression is not evaluated and the cached result is read using qread() and returned.
To clear a cached result, you can manually delete the associated .qs file, or you can call qcache() with clear = TRUE.
If prompt is also TRUE a prompt will be given asking you to confirm deletion.
If name is not specified, all cached results in cache_dir will be removed.
cache_dir <- tempdir()
a <- 1
b <- 5
# not cached
result <- qcache({a + b},
name="aplusb",
cache_dir = cache_dir,
qsave_params = list(preset="fast"))
#> [1] "not cached"
# cached
result <- qcache({a + b},
name="aplusb",
cache_dir = cache_dir,
qsave_params = list(preset="fast"))
#> [1] "cached"
# clear cached result
qcache(name="aplusb", clear=TRUE, prompt=FALSE, cache_dir = cache_dir)
#> [1] TRUE