qcache: qcache

Description Usage Arguments Details Examples

View source: R/qcache.R

Description

Helper function for caching objects for long running tasks

Usage

1
2
3
qcache(expr, name, envir=parent.frame(), cache_dir=".cache", 
              clear=FALSE, prompt=TRUE, qsave_params=list(), 
              qread_params=list())

Arguments

expr

The expression to evaluate

name

The cached expression name (see details)

envir

The environment to evaluate 'expr'

cache_dir

The directory to store cached files in

clear

Set to 'TRUE' to clear the cache (see details)

prompt

Whether to prompt before clearing

qsave_params

Parameters passed to 'qsave'

qread_params

Parameters passed to 'qread'

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 '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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
cache_dir <- tempdir()

a <- 1
b <- 5

# not cached
result <- qcache({a + b}, 
                 name="aplusb", 
                 cache_dir = cache_dir,
                 qsave_params = list(preset="fast"))

# cached
result <- qcache({a + b}, 
                 name="aplusb", 
                 cache_dir = cache_dir,
                 qsave_params = list(preset="fast"))

# clear cached result
qcache(name="aplusb", clear=TRUE, prompt=FALSE, cache_dir = cache_dir)

qs documentation built on Nov. 6, 2021, 1:07 a.m.

Related to qcache in qs...