Reproducible workflow

As part of a reproducible workflow, caching of function calls, code chunks, and other elements of a project is a critical component. The objective of a reproducible workflow is is likely that an entire work flow from raw data to publication, decision support, report writing, presentation building etc., could be built and be reproducible anywhere, on any computer, operating system, with any starting conditions, on demand. The reproducible::Cache function is built to work with any R function.

Differences with other approaches

Cache users DBI as a backend, with key functions, dbReadTable, dbRemoveTable, dbSendQuery, dbSendStatement, dbCreateTable and dbAppendTable. These can all be accessed via Cache, showCache, clearCache, and keepCache. It is optimized for speed of transactions, using digest::digest on objects and files. The main function is superficially similar to archivist::cache, which uses digest::digest in all cases to determine whether the arguments are identical in subsequent iterations. It also but does many things that make standard caching with digest::digest don’t work reliably between systems. For these, the function .robustDigest is introduced to make caching transferable between systems. This is relevant for file paths, environments, parallel clusters, functions (which are contained within an environment), and many others (e.g., see ?.robustDigest for methods). Cache also adds important elements like automated tagging and the option to retrieve disk-cached values via stashed objects in memory using memoise::memoise. This means that running Cache 1, 2, and 3 times on the same function will get progressively faster. This can be extremely useful for web apps built with, say shiny.

Function-level caching

Any function can be cached by wrapping Cache around the function call, or by using base pipe |>:

This will be a slight change to a function call, such as: terra::project(raster, crs = terra::crs(newRaster)) to Cache(terra::project(raster, crs = terra::crs(newRaster))) or with the pipe, which may be more convenient as it is easy to add and remove caching in the code base: terra::project(raster, crs = terra::crs(newRaster)) |> Cache()

This is particularly useful for expensive operations.

## 
## Attaching package: 'data.table'
## The following object is masked from 'package:terra':
## 
##     shift
tmpDir <- file.path(tempfile(), "reproducible_examples", "Cache")
dir.create(tmpDir, recursive = TRUE)

# Source raster with a complete LCC definition
ras <- terra::rast(terra::ext(0, 300, 0, 300), vals = 1:9e4, res = 1)
terra::crs(ras) <- "+proj=lcc +lat_1=60 +lat_2=70 +lat_0=50 +lon_0=-100 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"

# Target CRS in PROJ form (no EPSG lookup)
newCRS <- "+proj=longlat +datum=WGS84 +no_defs"

# Derive target extent from source extent (no registry lookup)
target_ext <- terra::project(terra::ext(ras), from = terra::crs(ras), to = newCRS)

# Build template with chosen resolution; assign CRS
tmplate <- terra::rast(target_ext, resolution = 0.00001)
terra::crs(tmplate) <- newCRS

# No Cache
system.time(map1 <- terra::project(ras, tmplate, method = "near"))
##    user  system elapsed 
##   0.028   0.002   0.030
# Try with memoise for this example -- for many simple cases, memoising will not be faster
opts <- options("reproducible.useMemoise" = TRUE)
# With Cache -- a little slower the first time because saving to disk
system.time({
  suppressWarnings({
    map1 <- terra::project(ras, tmplate, method = "near") |> 
      Cache(cachePath = tmpDir)
  })
})
## Saved! Cache file: b080b61ea3444bd7.rds; fn: project (and added a memoised copy)
##    user  system elapsed 
##   0.423   0.008   0.431
# faster the second time; improvement depends on size of object and time to run function
system.time({
  map2 <- terra::project(ras, tmplate, method = "near") |> 
    Cache(cachePath = tmpDir)
})
## Warning: [readValues] raster has no values
## Object to retrieve (fn: project, b080b61ea3444bd7.rds) ...
## Loaded! Memoised result from previous project call
##    user  system elapsed 
##   0.175   0.001   0.176
options(opts)

all.equal(map1, map2, check.attributes = FALSE) # TRUE
## [1] "Attributes: < Component \".Cache\": Component \"newCache\": 1 element mismatch >"

Caching examples

Basic use

try(clearCache(tmpDir, ask = FALSE), silent = TRUE) # just to make sure it is clear

ranNumsA <- rnorm(10, 16) |> Cache(cachePath = tmpDir)
## Saved! Cache file: aa549dd751b2f26d.rds; fn: rnorm
# All same
ranNumsB <- rnorm(10, 16) |> Cache(cachePath = tmpDir) # recovers cached copy
## Object to retrieve (fn: rnorm, aa549dd751b2f26d.rds) ...
## Loaded! Cached result from previous rnorm call
ranNumsD1 <- Cache(quote(rnorm(n = 10, 16))) |> Cache(cachePath = tmpDir) # recovers cached copy
## No cachePath supplied and getOption('reproducible.cachePath') is inside a temporary directory;
##   this will not persist across R sessions.
## Saved! Cache file: aa549dd751b2f26d.rds; fn: rnorm
## Saved! Cache file: af3928ff9ed68e4e.rds; fn: Cache
ranNumsD2 <- Cache(rnorm(n = 10, 16)) |> Cache(cachePath = tmpDir) # recovers cached copy
## No cachePath supplied and getOption('reproducible.cachePath') is inside a temporary directory;
##   this will not persist across R sessions.
## Object to retrieve (fn: rnorm, aa549dd751b2f26d.rds) ...
## Loaded! Cached result from previous rnorm call
## Saved! Cache file: 2f977d8a8473c320.rds; fn: Cache
# pipe
ranNumsD3 <- rnorm(n = 10, 16) |> Cache(cachePath = tmpDir) # recovers cached copy
## Object to retrieve (fn: rnorm, aa549dd751b2f26d.rds) ...
## Loaded! Cached result from previous rnorm call
# Any minor change makes it different
ranNumsE <- rnorm(10, 6) |> Cache(cachePath = tmpDir) # different
## Saved! Cache file: d78b46a2a76d6d80.rds; fn: rnorm
Example 1: Basic cache use with tags
ranNumsA <- rnorm(4) |> Cache(cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: adf21923cd1e50d0.rds; fn: rnorm
ranNumsB <- runif(4) |> Cache(cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: e23cab430872a0ea.rds; fn: runif
showCache(tmpDir, userTags = c("objectName"))
## Cache size:
## Total (including Rasters): 40 bytes
## Selected objects (not including Rasters): 40 bytes
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: adf21923cd1e50d0            function                     rnorm
##  2: adf21923cd1e50d0          objectName                         a
##  3: adf21923cd1e50d0            accessed 2026-01-08 05:48:59.40991
##  4: adf21923cd1e50d0             inCloud                     FALSE
##  5: adf21923cd1e50d0   elapsedTimeDigest          0.001774549 secs
##  6: adf21923cd1e50d0           preDigest     .FUN:4f604aa46882b368
##  7: adf21923cd1e50d0           preDigest     mean:c40c00762a0dac94
##  8: adf21923cd1e50d0           preDigest        n:7eef4eae85fd9229
##  9: adf21923cd1e50d0           preDigest       sd:853b1797f54b229c
## 10: adf21923cd1e50d0               class                   numeric
## 11: adf21923cd1e50d0         object.size                        80
## 12: adf21923cd1e50d0            fromDisk                     FALSE
## 13: adf21923cd1e50d0          resultHash                          
## 14: adf21923cd1e50d0 elapsedTimeFirstRun           0.00122261 secs
## 15: e23cab430872a0ea            function                     runif
## 16: e23cab430872a0ea          objectName                         b
## 17: e23cab430872a0ea            accessed 2026-01-08 05:48:59.41739
## 18: e23cab430872a0ea             inCloud                     FALSE
## 19: e23cab430872a0ea   elapsedTimeDigest          0.001629591 secs
## 20: e23cab430872a0ea           preDigest     .FUN:881ec847b7161f3c
## 21: e23cab430872a0ea           preDigest      max:853b1797f54b229c
## 22: e23cab430872a0ea           preDigest      min:c40c00762a0dac94
## 23: e23cab430872a0ea           preDigest        n:7eef4eae85fd9229
## 24: e23cab430872a0ea               class                   numeric
## 25: e23cab430872a0ea         object.size                        80
## 26: e23cab430872a0ea            fromDisk                     FALSE
## 27: e23cab430872a0ea          resultHash                          
## 28: e23cab430872a0ea elapsedTimeFirstRun          0.001210451 secs
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##                    createdDate
##                         <char>
##  1:  2026-01-08 05:48:59.41252
##  2:  2026-01-08 05:48:59.41252
##  3:  2026-01-08 05:48:59.41252
##  4:  2026-01-08 05:48:59.41252
##  5:  2026-01-08 05:48:59.41252
##  6:  2026-01-08 05:48:59.41252
##  7:  2026-01-08 05:48:59.41252
##  8:  2026-01-08 05:48:59.41252
##  9:  2026-01-08 05:48:59.41252
## 10:  2026-01-08 05:48:59.41252
## 11:  2026-01-08 05:48:59.41252
## 12:  2026-01-08 05:48:59.41252
## 13:  2026-01-08 05:48:59.41252
## 14:  2026-01-08 05:48:59.41252
## 15: 2026-01-08 05:48:59.419808
## 16: 2026-01-08 05:48:59.419808
## 17: 2026-01-08 05:48:59.419808
## 18: 2026-01-08 05:48:59.419808
## 19: 2026-01-08 05:48:59.419808
## 20: 2026-01-08 05:48:59.419808
## 21: 2026-01-08 05:48:59.419808
## 22: 2026-01-08 05:48:59.419808
## 23: 2026-01-08 05:48:59.419808
## 24: 2026-01-08 05:48:59.419808
## 25: 2026-01-08 05:48:59.419808
## 26: 2026-01-08 05:48:59.419808
## 27: 2026-01-08 05:48:59.419808
## 28: 2026-01-08 05:48:59.419808
##                    createdDate
##                         <char>
showCache(tmpDir, userTags = c("^a$")) # regular expression ... "a" exactly
## Cache size:
## Total (including Rasters): 20 bytes
## Selected objects (not including Rasters): 20 bytes
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: adf21923cd1e50d0            function                     rnorm
##  2: adf21923cd1e50d0          objectName                         a
##  3: adf21923cd1e50d0            accessed 2026-01-08 05:48:59.40991
##  4: adf21923cd1e50d0             inCloud                     FALSE
##  5: adf21923cd1e50d0   elapsedTimeDigest          0.001774549 secs
##  6: adf21923cd1e50d0           preDigest     .FUN:4f604aa46882b368
##  7: adf21923cd1e50d0           preDigest     mean:c40c00762a0dac94
##  8: adf21923cd1e50d0           preDigest        n:7eef4eae85fd9229
##  9: adf21923cd1e50d0           preDigest       sd:853b1797f54b229c
## 10: adf21923cd1e50d0               class                   numeric
## 11: adf21923cd1e50d0         object.size                        80
## 12: adf21923cd1e50d0            fromDisk                     FALSE
## 13: adf21923cd1e50d0          resultHash                          
## 14: adf21923cd1e50d0 elapsedTimeFirstRun           0.00122261 secs
##                   createdDate
##                        <char>
##  1: 2026-01-08 05:48:59.41252
##  2: 2026-01-08 05:48:59.41252
##  3: 2026-01-08 05:48:59.41252
##  4: 2026-01-08 05:48:59.41252
##  5: 2026-01-08 05:48:59.41252
##  6: 2026-01-08 05:48:59.41252
##  7: 2026-01-08 05:48:59.41252
##  8: 2026-01-08 05:48:59.41252
##  9: 2026-01-08 05:48:59.41252
## 10: 2026-01-08 05:48:59.41252
## 11: 2026-01-08 05:48:59.41252
## 12: 2026-01-08 05:48:59.41252
## 13: 2026-01-08 05:48:59.41252
## 14: 2026-01-08 05:48:59.41252
showCache(tmpDir, userTags = c("runif")) # show only cached objects made during runif call
## Cache size:
## Total (including Rasters): 20 bytes
## Selected objects (not including Rasters): 20 bytes
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: e23cab430872a0ea            function                     runif
##  2: e23cab430872a0ea          objectName                         b
##  3: e23cab430872a0ea            accessed 2026-01-08 05:48:59.41739
##  4: e23cab430872a0ea             inCloud                     FALSE
##  5: e23cab430872a0ea   elapsedTimeDigest          0.001629591 secs
##  6: e23cab430872a0ea           preDigest     .FUN:881ec847b7161f3c
##  7: e23cab430872a0ea           preDigest      max:853b1797f54b229c
##  8: e23cab430872a0ea           preDigest      min:c40c00762a0dac94
##  9: e23cab430872a0ea           preDigest        n:7eef4eae85fd9229
## 10: e23cab430872a0ea               class                   numeric
## 11: e23cab430872a0ea         object.size                        80
## 12: e23cab430872a0ea            fromDisk                     FALSE
## 13: e23cab430872a0ea          resultHash                          
## 14: e23cab430872a0ea elapsedTimeFirstRun          0.001210451 secs
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:48:59.419808
##  2: 2026-01-08 05:48:59.419808
##  3: 2026-01-08 05:48:59.419808
##  4: 2026-01-08 05:48:59.419808
##  5: 2026-01-08 05:48:59.419808
##  6: 2026-01-08 05:48:59.419808
##  7: 2026-01-08 05:48:59.419808
##  8: 2026-01-08 05:48:59.419808
##  9: 2026-01-08 05:48:59.419808
## 10: 2026-01-08 05:48:59.419808
## 11: 2026-01-08 05:48:59.419808
## 12: 2026-01-08 05:48:59.419808
## 13: 2026-01-08 05:48:59.419808
## 14: 2026-01-08 05:48:59.419808
clearCache(tmpDir, userTags = c("runif"), ask = FALSE) # remove only cached objects made during runif call
## Cache size:
## Total (including Rasters): 20 bytes
## Selected objects (not including Rasters): 20 bytes
showCache(tmpDir) # all
## Cache size:
## Total (including Rasters): 482 bytes
## Selected objects (not including Rasters): 482 bytes
##              cacheId              tagKey                         tagValue
##               <char>              <char>                           <char>
##  1: 2f977d8a8473c320            function                            Cache
##  2: 2f977d8a8473c320            accessed        2026-01-08 05:48:59.31954
##  3: 2f977d8a8473c320             inCloud                            FALSE
##  4: 2f977d8a8473c320   elapsedTimeDigest                 0.002318144 secs
##  5: 2f977d8a8473c320           preDigest  .cacheChaining:71681d621365dfd7
##  6: 2f977d8a8473c320           preDigest     .cacheExtra:c85d88fc56f4e042
##  7: 2f977d8a8473c320           preDigest            .FUN:ab4b977119e40b21
##  8: 2f977d8a8473c320           preDigest   .functionName:c85d88fc56f4e042
##  9: 2f977d8a8473c320           preDigest            conn:118387d5d48f757d
## 10: 2f977d8a8473c320           preDigest             drv:9ce9a83896bf68a1
## 11: 2f977d8a8473c320           preDigest cacheSaveFormat:cf2828ea967d53e7
## 12: 2f977d8a8473c320           preDigest          dryRun:e9aac936a0e8f6ae
## 13: 2f977d8a8473c320           preDigest             FUN:f2b5e0e1d6ee2618
## 14: 2f977d8a8473c320               class                          numeric
## 15: 2f977d8a8473c320         object.size                              176
## 16: 2f977d8a8473c320            fromDisk                            FALSE
## 17: 2f977d8a8473c320          resultHash                                 
## 18: 2f977d8a8473c320 elapsedTimeFirstRun                 0.006827593 secs
## 19: aa549dd751b2f26d            function                            rnorm
## 20: aa549dd751b2f26d            accessed        2026-01-08 05:48:59.29299
## 21: aa549dd751b2f26d             inCloud                            FALSE
## 22: aa549dd751b2f26d   elapsedTimeDigest                 0.001420975 secs
## 23: aa549dd751b2f26d           preDigest            .FUN:4f604aa46882b368
## 24: aa549dd751b2f26d           preDigest            mean:15620f138033a66c
## 25: aa549dd751b2f26d           preDigest               n:c5775c3b366fb719
## 26: aa549dd751b2f26d           preDigest              sd:853b1797f54b229c
## 27: aa549dd751b2f26d               class                          numeric
## 28: aa549dd751b2f26d         object.size                              176
## 29: aa549dd751b2f26d            fromDisk                            FALSE
## 30: aa549dd751b2f26d          resultHash                                 
## 31: aa549dd751b2f26d elapsedTimeFirstRun                 0.001206875 secs
## 32: aa549dd751b2f26d            accessed        2026-01-08 05:48:59.30134
## 33: aa549dd751b2f26d            accessed        2026-01-08 05:48:59.33309
## 34: adf21923cd1e50d0            function                            rnorm
## 35: adf21923cd1e50d0          objectName                                a
## 36: adf21923cd1e50d0            accessed        2026-01-08 05:48:59.40991
## 37: adf21923cd1e50d0             inCloud                            FALSE
## 38: adf21923cd1e50d0   elapsedTimeDigest                 0.001774549 secs
## 39: adf21923cd1e50d0           preDigest            .FUN:4f604aa46882b368
## 40: adf21923cd1e50d0           preDigest            mean:c40c00762a0dac94
## 41: adf21923cd1e50d0           preDigest               n:7eef4eae85fd9229
## 42: adf21923cd1e50d0           preDigest              sd:853b1797f54b229c
## 43: adf21923cd1e50d0               class                          numeric
## 44: adf21923cd1e50d0         object.size                               80
## 45: adf21923cd1e50d0            fromDisk                            FALSE
## 46: adf21923cd1e50d0          resultHash                                 
## 47: adf21923cd1e50d0 elapsedTimeFirstRun                  0.00122261 secs
## 48: af3928ff9ed68e4e            function                            Cache
## 49: af3928ff9ed68e4e            accessed        2026-01-08 05:48:59.30578
## 50: af3928ff9ed68e4e             inCloud                            FALSE
## 51: af3928ff9ed68e4e   elapsedTimeDigest                 0.002487421 secs
## 52: af3928ff9ed68e4e           preDigest  .cacheChaining:71681d621365dfd7
## 53: af3928ff9ed68e4e           preDigest     .cacheExtra:c85d88fc56f4e042
## 54: af3928ff9ed68e4e           preDigest            .FUN:ab4b977119e40b21
## 55: af3928ff9ed68e4e           preDigest   .functionName:c85d88fc56f4e042
## 56: af3928ff9ed68e4e           preDigest            conn:118387d5d48f757d
## 57: af3928ff9ed68e4e           preDigest             drv:9ce9a83896bf68a1
## 58: af3928ff9ed68e4e           preDigest cacheSaveFormat:cf2828ea967d53e7
## 59: af3928ff9ed68e4e           preDigest          dryRun:e9aac936a0e8f6ae
## 60: af3928ff9ed68e4e           preDigest             FUN:265d86c3bd130de5
## 61: af3928ff9ed68e4e               class                             call
## 62: af3928ff9ed68e4e         object.size                             1320
## 63: af3928ff9ed68e4e            fromDisk                            FALSE
## 64: af3928ff9ed68e4e          resultHash                                 
## 65: af3928ff9ed68e4e elapsedTimeFirstRun                  0.00810504 secs
## 66: d78b46a2a76d6d80            function                            rnorm
## 67: d78b46a2a76d6d80            accessed        2026-01-08 05:48:59.33644
## 68: d78b46a2a76d6d80             inCloud                            FALSE
## 69: d78b46a2a76d6d80   elapsedTimeDigest                 0.001314163 secs
## 70: d78b46a2a76d6d80           preDigest            .FUN:4f604aa46882b368
## 71: d78b46a2a76d6d80           preDigest            mean:152602b8ff81e5bb
## 72: d78b46a2a76d6d80           preDigest               n:c5775c3b366fb719
## 73: d78b46a2a76d6d80           preDigest              sd:853b1797f54b229c
## 74: d78b46a2a76d6d80               class                          numeric
## 75: d78b46a2a76d6d80         object.size                              176
## 76: d78b46a2a76d6d80            fromDisk                            FALSE
## 77: d78b46a2a76d6d80          resultHash                                 
## 78: d78b46a2a76d6d80 elapsedTimeFirstRun                0.0009243488 secs
##              cacheId              tagKey                         tagValue
##               <char>              <char>                           <char>
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:48:59.327462
##  2: 2026-01-08 05:48:59.327462
##  3: 2026-01-08 05:48:59.327462
##  4: 2026-01-08 05:48:59.327462
##  5: 2026-01-08 05:48:59.327462
##  6: 2026-01-08 05:48:59.327462
##  7: 2026-01-08 05:48:59.327462
##  8: 2026-01-08 05:48:59.327462
##  9: 2026-01-08 05:48:59.327462
## 10: 2026-01-08 05:48:59.327462
## 11: 2026-01-08 05:48:59.327462
## 12: 2026-01-08 05:48:59.327462
## 13: 2026-01-08 05:48:59.327462
## 14: 2026-01-08 05:48:59.327462
## 15: 2026-01-08 05:48:59.327462
## 16: 2026-01-08 05:48:59.327462
## 17: 2026-01-08 05:48:59.327462
## 18: 2026-01-08 05:48:59.327462
## 19:  2026-01-08 05:48:59.29548
## 20:  2026-01-08 05:48:59.29548
## 21:  2026-01-08 05:48:59.29548
## 22:  2026-01-08 05:48:59.29548
## 23:  2026-01-08 05:48:59.29548
## 24:  2026-01-08 05:48:59.29548
## 25:  2026-01-08 05:48:59.29548
## 26:  2026-01-08 05:48:59.29548
## 27:  2026-01-08 05:48:59.29548
## 28:  2026-01-08 05:48:59.29548
## 29:  2026-01-08 05:48:59.29548
## 30:  2026-01-08 05:48:59.29548
## 31:  2026-01-08 05:48:59.29548
## 32: 2026-01-08 05:48:59.301435
## 33: 2026-01-08 05:48:59.333179
## 34:  2026-01-08 05:48:59.41252
## 35:  2026-01-08 05:48:59.41252
## 36:  2026-01-08 05:48:59.41252
## 37:  2026-01-08 05:48:59.41252
## 38:  2026-01-08 05:48:59.41252
## 39:  2026-01-08 05:48:59.41252
## 40:  2026-01-08 05:48:59.41252
## 41:  2026-01-08 05:48:59.41252
## 42:  2026-01-08 05:48:59.41252
## 43:  2026-01-08 05:48:59.41252
## 44:  2026-01-08 05:48:59.41252
## 45:  2026-01-08 05:48:59.41252
## 46:  2026-01-08 05:48:59.41252
## 47:  2026-01-08 05:48:59.41252
## 48: 2026-01-08 05:48:59.314836
## 49: 2026-01-08 05:48:59.314836
## 50: 2026-01-08 05:48:59.314836
## 51: 2026-01-08 05:48:59.314836
## 52: 2026-01-08 05:48:59.314836
## 53: 2026-01-08 05:48:59.314836
## 54: 2026-01-08 05:48:59.314836
## 55: 2026-01-08 05:48:59.314836
## 56: 2026-01-08 05:48:59.314836
## 57: 2026-01-08 05:48:59.314836
## 58: 2026-01-08 05:48:59.314836
## 59: 2026-01-08 05:48:59.314836
## 60: 2026-01-08 05:48:59.314836
## 61: 2026-01-08 05:48:59.314836
## 62: 2026-01-08 05:48:59.314836
## 63: 2026-01-08 05:48:59.314836
## 64: 2026-01-08 05:48:59.314836
## 65: 2026-01-08 05:48:59.314836
## 66: 2026-01-08 05:48:59.338331
## 67: 2026-01-08 05:48:59.338331
## 68: 2026-01-08 05:48:59.338331
## 69: 2026-01-08 05:48:59.338331
## 70: 2026-01-08 05:48:59.338331
## 71: 2026-01-08 05:48:59.338331
## 72: 2026-01-08 05:48:59.338331
## 73: 2026-01-08 05:48:59.338331
## 74: 2026-01-08 05:48:59.338331
## 75: 2026-01-08 05:48:59.338331
## 76: 2026-01-08 05:48:59.338331
## 77: 2026-01-08 05:48:59.338331
## 78: 2026-01-08 05:48:59.338331
##                    createdDate
##                         <char>
clearCache(tmpDir, ask = FALSE)
Example 2: using the “accessed” tag
ranNumsA <- rnorm(4) |> Cache(cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: adf21923cd1e50d0.rds; fn: rnorm
ranNumsB <- runif(4) |> Cache(cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: e23cab430872a0ea.rds; fn: runif
# access it again, from Cache
Sys.sleep(1)
ranNumsA <- rnorm(4) |> Cache(cachePath = tmpDir, userTags = "objectName:a")
## Object to retrieve (fn: rnorm, adf21923cd1e50d0.rds) ...
## Loaded! Cached result from previous rnorm call
wholeCache <- showCache(tmpDir)
## Cache size:
## Total (including Rasters): 40 bytes
## Selected objects (not including Rasters): 40 bytes
# keep only items accessed "recently" (i.e., only objectName:a)
onlyRecentlyAccessed <- showCache(tmpDir, userTags = max(wholeCache[tagKey == "accessed"]$tagValue))
## Cache size:
## Total (including Rasters): 40 bytes
## Selected objects (not including Rasters): 40 bytes
# inverse join with 2 data.tables ... using: a[!b]
# i.e., return all of wholeCache that was not recently accessed
#   Note: the two different ways to access -- old way with "artifact" will be deprecated
toRemove <- unique(wholeCache[!onlyRecentlyAccessed, on = "cacheId"], by = "cacheId")$cacheId
clearCache(tmpDir, toRemove, ask = FALSE) # remove ones not recently accessed
## Cache size:
## Total (including Rasters): 40 bytes
## Selected objects (not including Rasters): 40 bytes
showCache(tmpDir) # still has more recently accessed
## Empty data.table (0 rows and 4 cols): cacheId,tagKey,tagValue,createdDate
Example 3: using keepCache

keepCache does the same as previous example, but more simply.

ranNumsA <- rnorm(4) |> Cache(cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: adf21923cd1e50d0.rds; fn: rnorm
ranNumsB <- Cache(runif(4)) |> Cache(cachePath = tmpDir, userTags = "objectName:b")
## No cachePath supplied and getOption('reproducible.cachePath') is inside a temporary directory;
##   this will not persist across R sessions.
## Saved! Cache file: e23cab430872a0ea.rds; fn: runif
## Saved! Cache file: f98bb25bbc75190b.rds; fn: Cache
# keep only those cached items from the last 24 hours
oneDay <- 60 * 60 * 24
keepCache(tmpDir, after = Sys.time() - oneDay, ask = FALSE)
## Nothing to remove; keeping all
##              cacheId              tagKey                         tagValue
##               <char>              <char>                           <char>
##  1: adf21923cd1e50d0            function                            rnorm
##  2: adf21923cd1e50d0          objectName                                a
##  3: adf21923cd1e50d0            accessed        2026-01-08 05:49:00.85237
##  4: adf21923cd1e50d0             inCloud                            FALSE
##  5: adf21923cd1e50d0   elapsedTimeDigest                 0.001477718 secs
##  6: adf21923cd1e50d0           preDigest            .FUN:4f604aa46882b368
##  7: adf21923cd1e50d0           preDigest            mean:c40c00762a0dac94
##  8: adf21923cd1e50d0           preDigest               n:7eef4eae85fd9229
##  9: adf21923cd1e50d0           preDigest              sd:853b1797f54b229c
## 10: adf21923cd1e50d0               class                          numeric
## 11: adf21923cd1e50d0         object.size                               80
## 12: adf21923cd1e50d0            fromDisk                            FALSE
## 13: adf21923cd1e50d0          resultHash                                 
## 14: adf21923cd1e50d0 elapsedTimeFirstRun                 0.001057863 secs
## 15: f98bb25bbc75190b            function                            Cache
## 16: f98bb25bbc75190b          objectName                                b
## 17: f98bb25bbc75190b            accessed        2026-01-08 05:49:00.85961
## 18: f98bb25bbc75190b             inCloud                            FALSE
## 19: f98bb25bbc75190b   elapsedTimeDigest                 0.002369881 secs
## 20: f98bb25bbc75190b           preDigest  .cacheChaining:71681d621365dfd7
## 21: f98bb25bbc75190b           preDigest     .cacheExtra:c85d88fc56f4e042
## 22: f98bb25bbc75190b           preDigest            .FUN:ab4b977119e40b21
## 23: f98bb25bbc75190b           preDigest   .functionName:c85d88fc56f4e042
## 24: f98bb25bbc75190b           preDigest            conn:118387d5d48f757d
## 25: f98bb25bbc75190b           preDigest             drv:9ce9a83896bf68a1
## 26: f98bb25bbc75190b           preDigest cacheSaveFormat:cf2828ea967d53e7
## 27: f98bb25bbc75190b           preDigest          dryRun:e9aac936a0e8f6ae
## 28: f98bb25bbc75190b           preDigest             FUN:0ea2b04926b969cd
## 29: f98bb25bbc75190b               class                          numeric
## 30: f98bb25bbc75190b         object.size                             1008
## 31: f98bb25bbc75190b            fromDisk                            FALSE
## 32: f98bb25bbc75190b          resultHash                                 
## 33: f98bb25bbc75190b elapsedTimeFirstRun                  0.01127672 secs
##              cacheId              tagKey                         tagValue
##               <char>              <char>                           <char>
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:00.854445
##  2: 2026-01-08 05:49:00.854445
##  3: 2026-01-08 05:49:00.854445
##  4: 2026-01-08 05:49:00.854445
##  5: 2026-01-08 05:49:00.854445
##  6: 2026-01-08 05:49:00.854445
##  7: 2026-01-08 05:49:00.854445
##  8: 2026-01-08 05:49:00.854445
##  9: 2026-01-08 05:49:00.854445
## 10: 2026-01-08 05:49:00.854445
## 11: 2026-01-08 05:49:00.854445
## 12: 2026-01-08 05:49:00.854445
## 13: 2026-01-08 05:49:00.854445
## 14: 2026-01-08 05:49:00.854445
## 15: 2026-01-08 05:49:00.871854
## 16: 2026-01-08 05:49:00.871854
## 17: 2026-01-08 05:49:00.871854
## 18: 2026-01-08 05:49:00.871854
## 19: 2026-01-08 05:49:00.871854
## 20: 2026-01-08 05:49:00.871854
## 21: 2026-01-08 05:49:00.871854
## 22: 2026-01-08 05:49:00.871854
## 23: 2026-01-08 05:49:00.871854
## 24: 2026-01-08 05:49:00.871854
## 25: 2026-01-08 05:49:00.871854
## 26: 2026-01-08 05:49:00.871854
## 27: 2026-01-08 05:49:00.871854
## 28: 2026-01-08 05:49:00.871854
## 29: 2026-01-08 05:49:00.871854
## 30: 2026-01-08 05:49:00.871854
## 31: 2026-01-08 05:49:00.871854
## 32: 2026-01-08 05:49:00.871854
## 33: 2026-01-08 05:49:00.871854
##                    createdDate
##                         <char>
# Keep all Cache items created with an rnorm() call
keepCache(tmpDir, userTags = "rnorm", ask = FALSE)
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: adf21923cd1e50d0            function                     rnorm
##  2: adf21923cd1e50d0          objectName                         a
##  3: adf21923cd1e50d0            accessed 2026-01-08 05:49:00.85237
##  4: adf21923cd1e50d0             inCloud                     FALSE
##  5: adf21923cd1e50d0   elapsedTimeDigest          0.001477718 secs
##  6: adf21923cd1e50d0           preDigest     .FUN:4f604aa46882b368
##  7: adf21923cd1e50d0           preDigest     mean:c40c00762a0dac94
##  8: adf21923cd1e50d0           preDigest        n:7eef4eae85fd9229
##  9: adf21923cd1e50d0           preDigest       sd:853b1797f54b229c
## 10: adf21923cd1e50d0               class                   numeric
## 11: adf21923cd1e50d0         object.size                        80
## 12: adf21923cd1e50d0            fromDisk                     FALSE
## 13: adf21923cd1e50d0          resultHash                          
## 14: adf21923cd1e50d0 elapsedTimeFirstRun          0.001057863 secs
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:00.854445
##  2: 2026-01-08 05:49:00.854445
##  3: 2026-01-08 05:49:00.854445
##  4: 2026-01-08 05:49:00.854445
##  5: 2026-01-08 05:49:00.854445
##  6: 2026-01-08 05:49:00.854445
##  7: 2026-01-08 05:49:00.854445
##  8: 2026-01-08 05:49:00.854445
##  9: 2026-01-08 05:49:00.854445
## 10: 2026-01-08 05:49:00.854445
## 11: 2026-01-08 05:49:00.854445
## 12: 2026-01-08 05:49:00.854445
## 13: 2026-01-08 05:49:00.854445
## 14: 2026-01-08 05:49:00.854445
showCache(tmpDir)
## Cache size:
## Total (including Rasters): 20 bytes
## Selected objects (not including Rasters): 20 bytes
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: adf21923cd1e50d0            function                     rnorm
##  2: adf21923cd1e50d0          objectName                         a
##  3: adf21923cd1e50d0            accessed 2026-01-08 05:49:00.85237
##  4: adf21923cd1e50d0             inCloud                     FALSE
##  5: adf21923cd1e50d0   elapsedTimeDigest          0.001477718 secs
##  6: adf21923cd1e50d0           preDigest     .FUN:4f604aa46882b368
##  7: adf21923cd1e50d0           preDigest     mean:c40c00762a0dac94
##  8: adf21923cd1e50d0           preDigest        n:7eef4eae85fd9229
##  9: adf21923cd1e50d0           preDigest       sd:853b1797f54b229c
## 10: adf21923cd1e50d0               class                   numeric
## 11: adf21923cd1e50d0         object.size                        80
## 12: adf21923cd1e50d0            fromDisk                     FALSE
## 13: adf21923cd1e50d0          resultHash                          
## 14: adf21923cd1e50d0 elapsedTimeFirstRun          0.001057863 secs
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:00.854445
##  2: 2026-01-08 05:49:00.854445
##  3: 2026-01-08 05:49:00.854445
##  4: 2026-01-08 05:49:00.854445
##  5: 2026-01-08 05:49:00.854445
##  6: 2026-01-08 05:49:00.854445
##  7: 2026-01-08 05:49:00.854445
##  8: 2026-01-08 05:49:00.854445
##  9: 2026-01-08 05:49:00.854445
## 10: 2026-01-08 05:49:00.854445
## 11: 2026-01-08 05:49:00.854445
## 12: 2026-01-08 05:49:00.854445
## 13: 2026-01-08 05:49:00.854445
## 14: 2026-01-08 05:49:00.854445
# Remove all Cache items that happened within a rnorm() call
clearCache(tmpDir, userTags = "rnorm", ask = FALSE)
## Cache size:
## Total (including Rasters): 20 bytes
## Selected objects (not including Rasters): 20 bytes
showCache(tmpDir) ## empty
## Empty data.table (0 rows and 4 cols): cacheId,tagKey,tagValue,createdDate
# Also, can set a time before caching happens and remove based on this
#  --> a useful, simple way to control Cache
ranNumsA <- rnorm(4) |> Cache(cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: adf21923cd1e50d0.rds; fn: rnorm
startTime <- Sys.time()
Sys.sleep(1)
ranNumsB <- rnorm(5) |> Cache(cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: 438a3028a4570cf9.rds; fn: rnorm
keepCache(tmpDir, after = startTime, ask = FALSE) # keep only those newer than startTime
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: 438a3028a4570cf9            function                     rnorm
##  2: 438a3028a4570cf9          objectName                         b
##  3: 438a3028a4570cf9            accessed 2026-01-08 05:49:01.94469
##  4: 438a3028a4570cf9             inCloud                     FALSE
##  5: 438a3028a4570cf9   elapsedTimeDigest          0.001589775 secs
##  6: 438a3028a4570cf9           preDigest     .FUN:4f604aa46882b368
##  7: 438a3028a4570cf9           preDigest     mean:c40c00762a0dac94
##  8: 438a3028a4570cf9           preDigest        n:a4f076b3db622faf
##  9: 438a3028a4570cf9           preDigest       sd:853b1797f54b229c
## 10: 438a3028a4570cf9               class                   numeric
## 11: 438a3028a4570cf9         object.size                        96
## 12: 438a3028a4570cf9            fromDisk                     FALSE
## 13: 438a3028a4570cf9          resultHash                          
## 14: 438a3028a4570cf9 elapsedTimeFirstRun          0.001112938 secs
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:01.946766
##  2: 2026-01-08 05:49:01.946766
##  3: 2026-01-08 05:49:01.946766
##  4: 2026-01-08 05:49:01.946766
##  5: 2026-01-08 05:49:01.946766
##  6: 2026-01-08 05:49:01.946766
##  7: 2026-01-08 05:49:01.946766
##  8: 2026-01-08 05:49:01.946766
##  9: 2026-01-08 05:49:01.946766
## 10: 2026-01-08 05:49:01.946766
## 11: 2026-01-08 05:49:01.946766
## 12: 2026-01-08 05:49:01.946766
## 13: 2026-01-08 05:49:01.946766
## 14: 2026-01-08 05:49:01.946766
clearCache(tmpDir, ask = FALSE)
Example 4: searching for multiple objects in the cache
# default userTags is "and" matching; for "or" matching use |
ranNumsA <- runif(4) |> Cache(cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: e23cab430872a0ea.rds; fn: runif
ranNumsB <- rnorm(4) |> Cache(cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: adf21923cd1e50d0.rds; fn: rnorm
# show all objects (runif and rnorm in this case)
showCache(tmpDir)
## Cache size:
## Total (including Rasters): 40 bytes
## Selected objects (not including Rasters): 40 bytes
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: adf21923cd1e50d0            function                     rnorm
##  2: adf21923cd1e50d0          objectName                         b
##  3: adf21923cd1e50d0            accessed 2026-01-08 05:49:02.05887
##  4: adf21923cd1e50d0             inCloud                     FALSE
##  5: adf21923cd1e50d0   elapsedTimeDigest          0.001390219 secs
##  6: adf21923cd1e50d0           preDigest     .FUN:4f604aa46882b368
##  7: adf21923cd1e50d0           preDigest     mean:c40c00762a0dac94
##  8: adf21923cd1e50d0           preDigest        n:7eef4eae85fd9229
##  9: adf21923cd1e50d0           preDigest       sd:853b1797f54b229c
## 10: adf21923cd1e50d0               class                   numeric
## 11: adf21923cd1e50d0         object.size                        80
## 12: adf21923cd1e50d0            fromDisk                     FALSE
## 13: adf21923cd1e50d0          resultHash                          
## 14: adf21923cd1e50d0 elapsedTimeFirstRun         0.0009651184 secs
## 15: e23cab430872a0ea            function                     runif
## 16: e23cab430872a0ea          objectName                         a
## 17: e23cab430872a0ea            accessed 2026-01-08 05:49:02.05263
## 18: e23cab430872a0ea             inCloud                     FALSE
## 19: e23cab430872a0ea   elapsedTimeDigest          0.001705408 secs
## 20: e23cab430872a0ea           preDigest     .FUN:881ec847b7161f3c
## 21: e23cab430872a0ea           preDigest      max:853b1797f54b229c
## 22: e23cab430872a0ea           preDigest      min:c40c00762a0dac94
## 23: e23cab430872a0ea           preDigest        n:7eef4eae85fd9229
## 24: e23cab430872a0ea               class                   numeric
## 25: e23cab430872a0ea         object.size                        80
## 26: e23cab430872a0ea            fromDisk                     FALSE
## 27: e23cab430872a0ea          resultHash                          
## 28: e23cab430872a0ea elapsedTimeFirstRun          0.001122236 secs
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:02.060803
##  2: 2026-01-08 05:49:02.060803
##  3: 2026-01-08 05:49:02.060803
##  4: 2026-01-08 05:49:02.060803
##  5: 2026-01-08 05:49:02.060803
##  6: 2026-01-08 05:49:02.060803
##  7: 2026-01-08 05:49:02.060803
##  8: 2026-01-08 05:49:02.060803
##  9: 2026-01-08 05:49:02.060803
## 10: 2026-01-08 05:49:02.060803
## 11: 2026-01-08 05:49:02.060803
## 12: 2026-01-08 05:49:02.060803
## 13: 2026-01-08 05:49:02.060803
## 14: 2026-01-08 05:49:02.060803
## 15:  2026-01-08 05:49:02.05479
## 16:  2026-01-08 05:49:02.05479
## 17:  2026-01-08 05:49:02.05479
## 18:  2026-01-08 05:49:02.05479
## 19:  2026-01-08 05:49:02.05479
## 20:  2026-01-08 05:49:02.05479
## 21:  2026-01-08 05:49:02.05479
## 22:  2026-01-08 05:49:02.05479
## 23:  2026-01-08 05:49:02.05479
## 24:  2026-01-08 05:49:02.05479
## 25:  2026-01-08 05:49:02.05479
## 26:  2026-01-08 05:49:02.05479
## 27:  2026-01-08 05:49:02.05479
## 28:  2026-01-08 05:49:02.05479
##                    createdDate
##                         <char>
# show objects that are both runif and rnorm
# (i.e., none in this case, because objecs are either or, not both)
showCache(tmpDir, userTags = c("runif", "rnorm")) ## empty
## Cache size:
## Total (including Rasters): 0 bytes
## Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows and 4 cols): cacheId,tagKey,tagValue,createdDate
# show objects that are either runif or rnorm ("or" search)
showCache(tmpDir, userTags = "runif|rnorm")
## Cache size:
## Total (including Rasters): 40 bytes
## Selected objects (not including Rasters): 40 bytes
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: adf21923cd1e50d0            function                     rnorm
##  2: adf21923cd1e50d0          objectName                         b
##  3: adf21923cd1e50d0            accessed 2026-01-08 05:49:02.05887
##  4: adf21923cd1e50d0             inCloud                     FALSE
##  5: adf21923cd1e50d0   elapsedTimeDigest          0.001390219 secs
##  6: adf21923cd1e50d0           preDigest     .FUN:4f604aa46882b368
##  7: adf21923cd1e50d0           preDigest     mean:c40c00762a0dac94
##  8: adf21923cd1e50d0           preDigest        n:7eef4eae85fd9229
##  9: adf21923cd1e50d0           preDigest       sd:853b1797f54b229c
## 10: adf21923cd1e50d0               class                   numeric
## 11: adf21923cd1e50d0         object.size                        80
## 12: adf21923cd1e50d0            fromDisk                     FALSE
## 13: adf21923cd1e50d0          resultHash                          
## 14: adf21923cd1e50d0 elapsedTimeFirstRun         0.0009651184 secs
## 15: e23cab430872a0ea            function                     runif
## 16: e23cab430872a0ea          objectName                         a
## 17: e23cab430872a0ea            accessed 2026-01-08 05:49:02.05263
## 18: e23cab430872a0ea             inCloud                     FALSE
## 19: e23cab430872a0ea   elapsedTimeDigest          0.001705408 secs
## 20: e23cab430872a0ea           preDigest     .FUN:881ec847b7161f3c
## 21: e23cab430872a0ea           preDigest      max:853b1797f54b229c
## 22: e23cab430872a0ea           preDigest      min:c40c00762a0dac94
## 23: e23cab430872a0ea           preDigest        n:7eef4eae85fd9229
## 24: e23cab430872a0ea               class                   numeric
## 25: e23cab430872a0ea         object.size                        80
## 26: e23cab430872a0ea            fromDisk                     FALSE
## 27: e23cab430872a0ea          resultHash                          
## 28: e23cab430872a0ea elapsedTimeFirstRun          0.001122236 secs
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:02.060803
##  2: 2026-01-08 05:49:02.060803
##  3: 2026-01-08 05:49:02.060803
##  4: 2026-01-08 05:49:02.060803
##  5: 2026-01-08 05:49:02.060803
##  6: 2026-01-08 05:49:02.060803
##  7: 2026-01-08 05:49:02.060803
##  8: 2026-01-08 05:49:02.060803
##  9: 2026-01-08 05:49:02.060803
## 10: 2026-01-08 05:49:02.060803
## 11: 2026-01-08 05:49:02.060803
## 12: 2026-01-08 05:49:02.060803
## 13: 2026-01-08 05:49:02.060803
## 14: 2026-01-08 05:49:02.060803
## 15:  2026-01-08 05:49:02.05479
## 16:  2026-01-08 05:49:02.05479
## 17:  2026-01-08 05:49:02.05479
## 18:  2026-01-08 05:49:02.05479
## 19:  2026-01-08 05:49:02.05479
## 20:  2026-01-08 05:49:02.05479
## 21:  2026-01-08 05:49:02.05479
## 22:  2026-01-08 05:49:02.05479
## 23:  2026-01-08 05:49:02.05479
## 24:  2026-01-08 05:49:02.05479
## 25:  2026-01-08 05:49:02.05479
## 26:  2026-01-08 05:49:02.05479
## 27:  2026-01-08 05:49:02.05479
## 28:  2026-01-08 05:49:02.05479
##                    createdDate
##                         <char>
# keep only objects that are either runif or rnorm ("or" search)
keepCache(tmpDir, userTags = "runif|rnorm", ask = FALSE)
## Nothing to remove; keeping all
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: adf21923cd1e50d0            function                     rnorm
##  2: adf21923cd1e50d0          objectName                         b
##  3: adf21923cd1e50d0            accessed 2026-01-08 05:49:02.05887
##  4: adf21923cd1e50d0             inCloud                     FALSE
##  5: adf21923cd1e50d0   elapsedTimeDigest          0.001390219 secs
##  6: adf21923cd1e50d0           preDigest     .FUN:4f604aa46882b368
##  7: adf21923cd1e50d0           preDigest     mean:c40c00762a0dac94
##  8: adf21923cd1e50d0           preDigest        n:7eef4eae85fd9229
##  9: adf21923cd1e50d0           preDigest       sd:853b1797f54b229c
## 10: adf21923cd1e50d0               class                   numeric
## 11: adf21923cd1e50d0         object.size                        80
## 12: adf21923cd1e50d0            fromDisk                     FALSE
## 13: adf21923cd1e50d0          resultHash                          
## 14: adf21923cd1e50d0 elapsedTimeFirstRun         0.0009651184 secs
## 15: e23cab430872a0ea            function                     runif
## 16: e23cab430872a0ea          objectName                         a
## 17: e23cab430872a0ea            accessed 2026-01-08 05:49:02.05263
## 18: e23cab430872a0ea             inCloud                     FALSE
## 19: e23cab430872a0ea   elapsedTimeDigest          0.001705408 secs
## 20: e23cab430872a0ea           preDigest     .FUN:881ec847b7161f3c
## 21: e23cab430872a0ea           preDigest      max:853b1797f54b229c
## 22: e23cab430872a0ea           preDigest      min:c40c00762a0dac94
## 23: e23cab430872a0ea           preDigest        n:7eef4eae85fd9229
## 24: e23cab430872a0ea               class                   numeric
## 25: e23cab430872a0ea         object.size                        80
## 26: e23cab430872a0ea            fromDisk                     FALSE
## 27: e23cab430872a0ea          resultHash                          
## 28: e23cab430872a0ea elapsedTimeFirstRun          0.001122236 secs
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:02.060803
##  2: 2026-01-08 05:49:02.060803
##  3: 2026-01-08 05:49:02.060803
##  4: 2026-01-08 05:49:02.060803
##  5: 2026-01-08 05:49:02.060803
##  6: 2026-01-08 05:49:02.060803
##  7: 2026-01-08 05:49:02.060803
##  8: 2026-01-08 05:49:02.060803
##  9: 2026-01-08 05:49:02.060803
## 10: 2026-01-08 05:49:02.060803
## 11: 2026-01-08 05:49:02.060803
## 12: 2026-01-08 05:49:02.060803
## 13: 2026-01-08 05:49:02.060803
## 14: 2026-01-08 05:49:02.060803
## 15:  2026-01-08 05:49:02.05479
## 16:  2026-01-08 05:49:02.05479
## 17:  2026-01-08 05:49:02.05479
## 18:  2026-01-08 05:49:02.05479
## 19:  2026-01-08 05:49:02.05479
## 20:  2026-01-08 05:49:02.05479
## 21:  2026-01-08 05:49:02.05479
## 22:  2026-01-08 05:49:02.05479
## 23:  2026-01-08 05:49:02.05479
## 24:  2026-01-08 05:49:02.05479
## 25:  2026-01-08 05:49:02.05479
## 26:  2026-01-08 05:49:02.05479
## 27:  2026-01-08 05:49:02.05479
## 28:  2026-01-08 05:49:02.05479
##                    createdDate
##                         <char>
clearCache(tmpDir, ask = FALSE)
Example 5: using caching to speed up rerunning expensive computations
ras <- terra::rast(terra::ext(0, 5, 0, 5),
  res = 1,
  vals = sample(1:5, replace = TRUE, size = 25),
  crs = "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"
)

rasCRS <- terra::crs(ras)
# A slow operation, like GIS operation
notCached <- suppressWarnings(
  # project raster generates warnings when run non-interactively
  terra::project(ras, rasCRS, res = 5)
)

cached <- suppressWarnings(
  # project raster generates warnings when run non-interactively
  # using quote works also
  terra::project(ras, rasCRS, res = 5) |> Cache(cachePath = tmpDir)
)
## Saved! Cache file: 90f1b7ce9e996dc1.rds; fn: project
# second time is much faster
reRun <- suppressWarnings(
  # project raster generates warnings when run non-interactively
  terra::project(ras, rasCRS, res = 5) |> Cache(cachePath = tmpDir)
)
## Object to retrieve (fn: project, 90f1b7ce9e996dc1.rds) ...
## Loaded! Cached result from previous project call
# recovered cached version is same as non-cached version
all.equal(notCached, reRun, check.attributes = FALSE) ## TRUE
## [1] "Attributes: < Names: 2 string mismatches >"                                                    
## [2] "Attributes: < Length mismatch: comparison on first 2 components >"                             
## [3] "Attributes: < Component 1: Modes: character, list >"                                           
## [4] "Attributes: < Component 1: names for current but not for target >"                             
## [5] "Attributes: < Component 1: Attributes: < names for target but not for current > >"             
## [6] "Attributes: < Component 1: Attributes: < Length mismatch: comparison on first 0 components > >"
## [7] "Attributes: < Component 1: target is character, current is list >"                             
## [8] "Attributes: < Component 2: 'current' is not an envRefClass >"

Nested Caching

Nested caching, which is when Caching of a function occurs inside an outer function, which is itself cached. This is a critical element to working within a reproducible work flow. It is not enough during development to cache flat code chunks, as there will be many levels of “slow” functions. Ideally, at all points in a development cycle, it should be possible to get to any line of code starting from the very initial steps, running through everything up to that point, in less than a few seconds. If the workflow can be kept very fast like this, then there is a guarantee that it will work at any point.

##########################
## Nested Caching
# Make 2 functions
inner <- function(mean) {
  d <- 1
  rnorm(n = 3, mean = mean)
}
outer <- function(n) {
  inner(0.1) |> Cache(cachePath = tmpdir2)
}

# make 2 different cache paths
tmpdir1 <- file.path(tempfile(), "first")
tmpdir2 <- file.path(tempfile(), "second")

# Run the Cache ... notOlderThan propagates to all 3 Cache calls,
#   but cachePath is tmpdir1 in top level Cache and all nested
#   Cache calls, unless individually overridden ... here inner
#   uses tmpdir2 repository
outer(n = 2) |> Cache(cachePath = tmpdir1)
## Saved! Cache file: a352d42cb0291199.rds; fn: inner
## Saved! Cache file: 61564dd5e84ab6d5.rds; fn: outer
## [1] -0.6854327 -0.9567369 -0.6955414
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
## 
## attr(,"tags")
## [1] "cacheId:61564dd5e84ab6d5"
## attr(,"callInCache")
## [1] ""
showCache(tmpdir1) # 2 function calls
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: 61564dd5e84ab6d5            function                     outer
##  2: 61564dd5e84ab6d5            accessed 2026-01-08 05:49:02.39302
##  3: 61564dd5e84ab6d5             inCloud                     FALSE
##  4: 61564dd5e84ab6d5   elapsedTimeDigest          0.001781225 secs
##  5: 61564dd5e84ab6d5           preDigest     .FUN:fd3ff16451bebbef
##  6: 61564dd5e84ab6d5           preDigest        n:82dc709f2b91918a
##  7: 61564dd5e84ab6d5               class                   numeric
##  8: 61564dd5e84ab6d5         object.size                      1008
##  9: 61564dd5e84ab6d5            fromDisk                     FALSE
## 10: 61564dd5e84ab6d5          resultHash                          
## 11: 61564dd5e84ab6d5 elapsedTimeFirstRun          0.006536007 secs
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:02.400518
##  2: 2026-01-08 05:49:02.400518
##  3: 2026-01-08 05:49:02.400518
##  4: 2026-01-08 05:49:02.400518
##  5: 2026-01-08 05:49:02.400518
##  6: 2026-01-08 05:49:02.400518
##  7: 2026-01-08 05:49:02.400518
##  8: 2026-01-08 05:49:02.400518
##  9: 2026-01-08 05:49:02.400518
## 10: 2026-01-08 05:49:02.400518
## 11: 2026-01-08 05:49:02.400518
showCache(tmpdir2) # 1 function call
## Cache size:
## Total (including Rasters): 20 bytes
## Selected objects (not including Rasters): 20 bytes
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: a352d42cb0291199            function                     inner
##  2: a352d42cb0291199       outerFunction                     outer
##  3: a352d42cb0291199            accessed 2026-01-08 05:49:02.39591
##  4: a352d42cb0291199             inCloud                     FALSE
##  5: a352d42cb0291199   elapsedTimeDigest          0.001423359 secs
##  6: a352d42cb0291199           preDigest     .FUN:c411a17f70613a02
##  7: a352d42cb0291199           preDigest     mean:22413394efd9f6a3
##  8: a352d42cb0291199               class                   numeric
##  9: a352d42cb0291199         object.size                        80
## 10: a352d42cb0291199            fromDisk                     FALSE
## 11: a352d42cb0291199          resultHash                          
## 12: a352d42cb0291199 elapsedTimeFirstRun         0.0009157658 secs
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:02.397857
##  2: 2026-01-08 05:49:02.397857
##  3: 2026-01-08 05:49:02.397857
##  4: 2026-01-08 05:49:02.397857
##  5: 2026-01-08 05:49:02.397857
##  6: 2026-01-08 05:49:02.397857
##  7: 2026-01-08 05:49:02.397857
##  8: 2026-01-08 05:49:02.397857
##  9: 2026-01-08 05:49:02.397857
## 10: 2026-01-08 05:49:02.397857
## 11: 2026-01-08 05:49:02.397857
## 12: 2026-01-08 05:49:02.397857
# userTags get appended
# all items have the outer tag propagate, plus inner ones only have inner ones
clearCache(tmpdir1, ask = FALSE)
outerTag <- "outerTag"
innerTag <- "innerTag"
inner <- function(mean) {
  d <- 1
  rnorm(n = 3, mean = mean) |> Cache(notOlderThan = Sys.time() - 1e5, userTags = innerTag)
}
outer <- function(n) {
  inner(0.1) |> Cache()
}
aa <- Cache(outer, n = 2) |> Cache(cachePath = tmpdir1, userTags = outerTag)
## No cachePath supplied and getOption('reproducible.cachePath') is inside a temporary directory;
##   this will not persist across R sessions.
## No cachePath supplied and getOption('reproducible.cachePath') is inside a temporary directory;
##   this will not persist across R sessions.
## No cachePath supplied and getOption('reproducible.cachePath') is inside a temporary directory;
##   this will not persist across R sessions.
## Saved! Cache file: a481e2b85f7337f2.rds; fn: rnorm
## Saved! Cache file: 9691c7bae9ad6582.rds; fn: inner
## Saved! Cache file: 2d26a68d5154433e.rds; fn: outer
## Saved! Cache file: 792e02df4389b115.rds; fn: Cache
showCache(tmpdir1) # rnorm function has outerTag and innerTag, inner and outer only have outerTag
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                         tagValue
##               <char>              <char>                           <char>
##  1: 792e02df4389b115            function                            Cache
##  2: 792e02df4389b115            userTags                         outerTag
##  3: 792e02df4389b115            accessed        2026-01-08 05:49:02.41895
##  4: 792e02df4389b115             inCloud                            FALSE
##  5: 792e02df4389b115   elapsedTimeDigest                 0.002523899 secs
##  6: 792e02df4389b115           preDigest  .cacheChaining:71681d621365dfd7
##  7: 792e02df4389b115           preDigest     .cacheExtra:c85d88fc56f4e042
##  8: 792e02df4389b115           preDigest            .FUN:ab4b977119e40b21
##  9: 792e02df4389b115           preDigest   .functionName:c85d88fc56f4e042
## 10: 792e02df4389b115           preDigest            conn:118387d5d48f757d
## 11: 792e02df4389b115           preDigest             drv:9ce9a83896bf68a1
## 12: 792e02df4389b115           preDigest               n:82dc709f2b91918a
## 13: 792e02df4389b115           preDigest cacheSaveFormat:cf2828ea967d53e7
## 14: 792e02df4389b115           preDigest          dryRun:e9aac936a0e8f6ae
## 15: 792e02df4389b115           preDigest             FUN:f4ab56347506d214
## 16: 792e02df4389b115               class                          numeric
## 17: 792e02df4389b115         object.size                             1008
## 18: 792e02df4389b115            fromDisk                            FALSE
## 19: 792e02df4389b115          resultHash                                 
## 20: 792e02df4389b115 elapsedTimeFirstRun                  0.01954913 secs
##              cacheId              tagKey                         tagValue
##               <char>              <char>                           <char>
##                    createdDate
##                         <char>
##  1: 2026-01-08 05:49:02.439398
##  2: 2026-01-08 05:49:02.439398
##  3: 2026-01-08 05:49:02.439398
##  4: 2026-01-08 05:49:02.439398
##  5: 2026-01-08 05:49:02.439398
##  6: 2026-01-08 05:49:02.439398
##  7: 2026-01-08 05:49:02.439398
##  8: 2026-01-08 05:49:02.439398
##  9: 2026-01-08 05:49:02.439398
## 10: 2026-01-08 05:49:02.439398
## 11: 2026-01-08 05:49:02.439398
## 12: 2026-01-08 05:49:02.439398
## 13: 2026-01-08 05:49:02.439398
## 14: 2026-01-08 05:49:02.439398
## 15: 2026-01-08 05:49:02.439398
## 16: 2026-01-08 05:49:02.439398
## 17: 2026-01-08 05:49:02.439398
## 18: 2026-01-08 05:49:02.439398
## 19: 2026-01-08 05:49:02.439398
## 20: 2026-01-08 05:49:02.439398
##                    createdDate
##                         <char>

cacheId

Sometimes, it is not absolutely desirable to maintain the work flow intact because changes that are irrelevant to the analysis, such as changing messages sent to a user, may be changed, without a desire to rerun functions. The cacheId argument is for this. Once a piece of code is run, then the cacheId can be manually extracted (it is reported at the end of a Cache call) and manually placed in the code, passed in as, say, cacheId = "ad184ce64541972b50afd8e7b75f821b".

### cacheId
set.seed(1)
rnorm(1) |> Cache(cachePath = tmpdir1)
## Saved! Cache file: ca275879d5116967.rds; fn: rnorm
## [1] -0.6264538
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
## 
## attr(,"tags")
## [1] "cacheId:ca275879d5116967"
## attr(,"callInCache")
## [1] ""
# manually look at output attribute which shows cacheId: 422bae4ed2f770cc
rnorm(1) |> Cache(cachePath = tmpdir1, cacheId = "422bae4ed2f770cc") # same value
## cacheId passed to override automatic digesting; using 422bae4ed2f770cc
## Saved! Cache file: 422bae4ed2f770cc.rds; fn: rnorm
## [1] 0.1836433
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
## 
## attr(,"tags")
## [1] "cacheId:422bae4ed2f770cc"
## attr(,"callInCache")
## [1] ""
# override even with different inputs:
rnorm(2) |> Cache(cachePath = tmpdir1, cacheId = "422bae4ed2f770cc")
## cacheId passed to override automatic digesting; using 422bae4ed2f770cc
## Object to retrieve (fn: rnorm, 422bae4ed2f770cc.rds) ...
## Loaded! Cached result from previous rnorm call
## [1] 0.1836433
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] FALSE
## 
## attr(,"tags")
## [1] "cacheId:422bae4ed2f770cc"
## attr(,"callInCache")
## [1] ""

Working with the Cache manually

Since the cache is simply a DBI data table (of an SQLite database by default). In addition, there are several helpers in the reproducible package, including showCache, keepCache and clearCache that may be useful. Also, one can access cached items manually (rather than simply rerunning the same Cache function again).

# As of reproducible version 1.0, there is a new backend directly using DBI
mapHash <- unique(showCache(tmpDir, userTags = "project")$cacheId)
## Cache size:
## Total (including Rasters): 676 bytes
## Selected objects (not including Rasters): 676 bytes
map <- loadFromCache(mapHash[1], cachePath = tmpDir)
## Loaded! Cached result from previous  call
terra::plot(map)

## cleanup
unlink(dirname(tmpDir), recursive = TRUE)

Alternative database backends

By default, caching relies on a sqlite database for it’s backend. While this works in many situations, there are some important limitations of using sqlite for caching, including 1) speed; 2) concurrent transactions; 3) sharing database across machines or projects. Fortunately, Cache makes use of DBI package and thus supports several database backends, including mysql and postgresql.

See https://github.com/PredictiveEcology/SpaDES/wiki/Using-alternate-database-backends-for-Cache for further information on configuring these additional backends.

Reproducible workflow

In general, we feel that a liberal use of Cache will make a re-usable and reproducible work flow. shiny apps can be made, taking advantage of Cache. Indeed, much of the difficulty in managing data sets and saving them for future use, can be accommodated by caching.