Helper function for caching objects for long running tasks
Usage
qs_cache(
expr,
name,
envir = parent.frame(),
cache_dir = ".cache",
clear = FALSE,
prompt = TRUE,
qs_save_params = list(),
qs_read_params = list(),
verbose = TRUE
)Arguments
- expr
The expression to evaluate.
- name
The cached expression name (see details).
- envir
The environment to evaluate
exprin.- cache_dir
The directory to store cached files in.
- clear
Set to
TRUEto clear the cache (see details).- prompt
Whether to prompt before clearing.
- qs_save_params
List of parameters passed on to
qs_save.- qs_read_params
List of parameters passed on to
qs_read.- verbose
Boolean. If
TRUE, the function prints an informative message regarding the status of cached objects.
Details
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 qs_save() in <cache_dir>/<name>.qs2.
If the file already exists instead, the expression is not evaluated and the cached result is read using qs_read() and returned.
To clear a cached result, you can manually delete the associated .qs2 file, or you can call qs_cache() 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.
Examples
cache_dir <- tempdir()
a <- 1
b <- 5
# not cached
result <- qs_cache({a + b},
name="aplusb",
cache_dir = cache_dir,
qs_save_params = list(compress_level = 5))
#> not cached
# cached
result <- qs_cache({a + b},
name="aplusb",
cache_dir = cache_dir,
qs_save_params = list(compress_level = 5))
#> cached
# clear cached result
qs_cache(name="aplusb", clear=TRUE, prompt=FALSE, cache_dir = cache_dir)
#> [1] TRUE