qcache: qcache

View source: R/qcache.R

qcacheR Documentation

qcache

Description

Helper function for caching objects for long running tasks

Usage

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

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 on to qsave.

qread_params

Parameters passed on 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

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 Oct. 2, 2024, 1:07 a.m.