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.
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
.
Any function can be cached by wrapping Cache
around the
function call, or by using base pipe |>
or using:
Cache(FUN = functionName, ...)
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
Cache(terra::project, raster, crs = terra::crs(newRaster))
or with the pipe
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(tempdir(), "reproducible_examples", "Cache")
dir.create(tmpDir, recursive = TRUE)
ras <- terra::rast(terra::ext(0, 300, 0, 300), vals = 1:9e4, res = 1)
terra::crs(ras) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"
newCRS <- "+init=epsg:4326" # A longlat crs
# No Cache
system.time(suppressWarnings(map1 <- terra::project(ras, newCRS))) # Warnings due to new PROJ
## user system elapsed
## 0.035 0.004 0.040
# 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 <- Cache(terra::project, ras, newCRS, cachePath = tmpDir, notOlderThan = Sys.time())
})
})
## Saved! Cache file: 58442e5727a1aa3c.rds; fn: terra::project
## user system elapsed
## 0.325 0.028 0.359
# faster the second time; improvement depends on size of object and time to run function
system.time({
map2 <- Cache(terra::project, ras, newCRS, cachePath = tmpDir)
})
## Object to retrieve (fn: terra::project, 58442e5727a1aa3c.rds) ...
## Loaded! Memoised result from previous terra::project call
## user system elapsed
## 0.201 0.000 0.204
## [1] "Attributes: < Component \".Cache\": Component \"newCache\": 1 element mismatch >"
## [2] "Attributes: < Component \"ptr\": Component \"origin\": Mean relative difference: 0.0005613483 >"
try(clearCache(tmpDir, ask = FALSE), silent = TRUE) # just to make sure it is clear
ranNumsA <- Cache(rnorm, 10, 16, cachePath = tmpDir)
## Saved! Cache file: 4fca280cda001fc9.rds; fn: rnorm
## Object to retrieve (fn: rnorm, 4fca280cda001fc9.rds) ...
## Loaded! Cached result from previous rnorm call
## Object to retrieve (fn: quote, 4fca280cda001fc9.rds) ...
## Loaded! Cached result from previous quote call
## Object to retrieve (fn: rnorm, 4fca280cda001fc9.rds) ...
## Loaded! Cached result from previous rnorm call
## Object to retrieve (fn: rnorm, 4fca280cda001fc9.rds) ...
## Loaded! Cached result from previous rnorm call
# Any minor change makes it different
ranNumsE <- Cache(rnorm, 10, 6, cachePath = tmpDir) # different
## Saved! Cache file: 5efb386c43c29ce1.rds; fn: rnorm
## Saved! Cache file: ad0ea27476c50b66.rds; fn: rnorm
## Saved! Cache file: deaa37372f85861b.rds; fn: runif
# access it again, from Cache
Sys.sleep(1)
ranNumsA <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:a")
## Object to retrieve (fn: rnorm, ad0ea27476c50b66.rds) ...
## Loaded! Cached result from previous rnorm call
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 504 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): 252 bytes
## Selected objects (not including Rasters): 252 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): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: ad0ea27476c50b66 objectName a
## 2: ad0ea27476c50b66 function rnorm
## 3: ad0ea27476c50b66 class numeric
## 4: ad0ea27476c50b66 object.size 1008
## 5: ad0ea27476c50b66 accessed 2024-12-12 06:31:28.27216
## 6: ad0ea27476c50b66 inCloud FALSE
## 7: ad0ea27476c50b66 fromDisk FALSE
## 8: ad0ea27476c50b66 resultHash
## 9: ad0ea27476c50b66 elapsedTimeDigest 0.001947403 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun 8.130074e-05 secs
## 11: ad0ea27476c50b66 otherFunctions vweave_rmarkdown
## 12: ad0ea27476c50b66 otherFunctions process_file
## 13: ad0ea27476c50b66 otherFunctions process_group
## 14: ad0ea27476c50b66 otherFunctions call_block
## 15: ad0ea27476c50b66 otherFunctions block_exec
## 16: ad0ea27476c50b66 otherFunctions eng_r
## 17: ad0ea27476c50b66 otherFunctions in_input_dir
## 18: ad0ea27476c50b66 otherFunctions in_dir
## 19: ad0ea27476c50b66 otherFunctions with_handlers
## 20: ad0ea27476c50b66 preDigest n:7eef4eae85fd9229
## 21: ad0ea27476c50b66 preDigest mean:c40c00762a0dac94
## 22: ad0ea27476c50b66 preDigest sd:853b1797f54b229c
## 23: ad0ea27476c50b66 preDigest .FUN:4f604aa46882b368
## 24: ad0ea27476c50b66 accessed 2024-12-12 06:31:29.31291
## 25: ad0ea27476c50b66 elapsedTimeLoad 0.003530979 secs
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:28.272841
## 2: 2024-12-12 06:31:28.272841
## 3: 2024-12-12 06:31:28.272841
## 4: 2024-12-12 06:31:28.272841
## 5: 2024-12-12 06:31:28.272841
## 6: 2024-12-12 06:31:28.272841
## 7: 2024-12-12 06:31:28.272841
## 8: 2024-12-12 06:31:28.272841
## 9: 2024-12-12 06:31:28.272841
## 10: 2024-12-12 06:31:28.272841
## 11: 2024-12-12 06:31:28.272841
## 12: 2024-12-12 06:31:28.272841
## 13: 2024-12-12 06:31:28.272841
## 14: 2024-12-12 06:31:28.272841
## 15: 2024-12-12 06:31:28.272841
## 16: 2024-12-12 06:31:28.272841
## 17: 2024-12-12 06:31:28.272841
## 18: 2024-12-12 06:31:28.272841
## 19: 2024-12-12 06:31:28.272841
## 20: 2024-12-12 06:31:28.272841
## 21: 2024-12-12 06:31:28.272841
## 22: 2024-12-12 06:31:28.272841
## 23: 2024-12-12 06:31:28.272841
## 24: 2024-12-12 06:31:29.31291
## 25: 2024-12-12 06:31:29.3162
## createdDate
keepCache
does the same as previous example, but more
simply.
## Object to retrieve (fn: rnorm, ad0ea27476c50b66.rds) ...
## Loaded! Cached result from previous rnorm call
## Saved! Cache file: deaa37372f85861b.rds; fn: runif
# keep only those cached items from the last 24 hours
oneDay <- 60 * 60 * 24
keepCache(tmpDir, after = Sys.time() - oneDay, ask = FALSE)
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 504 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: ad0ea27476c50b66 objectName a
## 2: ad0ea27476c50b66 function rnorm
## 3: ad0ea27476c50b66 class numeric
## 4: ad0ea27476c50b66 object.size 1008
## 5: ad0ea27476c50b66 accessed 2024-12-12 06:31:28.27216
## 6: ad0ea27476c50b66 inCloud FALSE
## 7: ad0ea27476c50b66 fromDisk FALSE
## 8: ad0ea27476c50b66 resultHash
## 9: ad0ea27476c50b66 elapsedTimeDigest 0.001947403 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun 8.130074e-05 secs
## 11: ad0ea27476c50b66 otherFunctions vweave_rmarkdown
## 12: ad0ea27476c50b66 otherFunctions process_file
## 13: ad0ea27476c50b66 otherFunctions process_group
## 14: ad0ea27476c50b66 otherFunctions call_block
## 15: ad0ea27476c50b66 otherFunctions block_exec
## 16: ad0ea27476c50b66 otherFunctions eng_r
## 17: ad0ea27476c50b66 otherFunctions in_input_dir
## 18: ad0ea27476c50b66 otherFunctions in_dir
## 19: ad0ea27476c50b66 otherFunctions with_handlers
## 20: ad0ea27476c50b66 preDigest n:7eef4eae85fd9229
## 21: ad0ea27476c50b66 preDigest mean:c40c00762a0dac94
## 22: ad0ea27476c50b66 preDigest sd:853b1797f54b229c
## 23: ad0ea27476c50b66 preDigest .FUN:4f604aa46882b368
## 24: ad0ea27476c50b66 accessed 2024-12-12 06:31:29.31291
## 25: ad0ea27476c50b66 elapsedTimeLoad 0.004675388 secs
## 26: ad0ea27476c50b66 accessed 2024-12-12 06:31:29.401225
## 27: deaa37372f85861b objectName b
## 28: deaa37372f85861b function runif
## 29: deaa37372f85861b class numeric
## 30: deaa37372f85861b object.size 1008
## 31: deaa37372f85861b accessed 2024-12-12 06:31:29.417036
## 32: deaa37372f85861b inCloud FALSE
## 33: deaa37372f85861b fromDisk FALSE
## 34: deaa37372f85861b resultHash
## 35: deaa37372f85861b elapsedTimeDigest 0.001774311 secs
## 36: deaa37372f85861b elapsedTimeFirstRun 7.653236e-05 secs
## 37: deaa37372f85861b otherFunctions vweave_rmarkdown
## 38: deaa37372f85861b otherFunctions process_file
## 39: deaa37372f85861b otherFunctions process_group
## 40: deaa37372f85861b otherFunctions call_block
## 41: deaa37372f85861b otherFunctions block_exec
## 42: deaa37372f85861b otherFunctions eng_r
## 43: deaa37372f85861b otherFunctions in_input_dir
## 44: deaa37372f85861b otherFunctions in_dir
## 45: deaa37372f85861b otherFunctions with_handlers
## 46: deaa37372f85861b preDigest n:7eef4eae85fd9229
## 47: deaa37372f85861b preDigest min:c40c00762a0dac94
## 48: deaa37372f85861b preDigest max:853b1797f54b229c
## 49: deaa37372f85861b preDigest .FUN:881ec847b7161f3c
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:28.272841
## 2: 2024-12-12 06:31:28.272841
## 3: 2024-12-12 06:31:28.272841
## 4: 2024-12-12 06:31:28.272841
## 5: 2024-12-12 06:31:28.272841
## 6: 2024-12-12 06:31:28.272841
## 7: 2024-12-12 06:31:28.272841
## 8: 2024-12-12 06:31:28.272841
## 9: 2024-12-12 06:31:28.272841
## 10: 2024-12-12 06:31:28.272841
## 11: 2024-12-12 06:31:28.272841
## 12: 2024-12-12 06:31:28.272841
## 13: 2024-12-12 06:31:28.272841
## 14: 2024-12-12 06:31:28.272841
## 15: 2024-12-12 06:31:28.272841
## 16: 2024-12-12 06:31:28.272841
## 17: 2024-12-12 06:31:28.272841
## 18: 2024-12-12 06:31:28.272841
## 19: 2024-12-12 06:31:28.272841
## 20: 2024-12-12 06:31:28.272841
## 21: 2024-12-12 06:31:28.272841
## 22: 2024-12-12 06:31:28.272841
## 23: 2024-12-12 06:31:28.272841
## 24: 2024-12-12 06:31:29.31291
## 25: 2024-12-12 06:31:29.3162
## 26: 2024-12-12 06:31:29.401225
## 27: 2024-12-12 06:31:29.417685
## 28: 2024-12-12 06:31:29.417685
## 29: 2024-12-12 06:31:29.417685
## 30: 2024-12-12 06:31:29.417685
## 31: 2024-12-12 06:31:29.417685
## 32: 2024-12-12 06:31:29.417685
## 33: 2024-12-12 06:31:29.417685
## 34: 2024-12-12 06:31:29.417685
## 35: 2024-12-12 06:31:29.417685
## 36: 2024-12-12 06:31:29.417685
## 37: 2024-12-12 06:31:29.417685
## 38: 2024-12-12 06:31:29.417685
## 39: 2024-12-12 06:31:29.417685
## 40: 2024-12-12 06:31:29.417685
## 41: 2024-12-12 06:31:29.417685
## 42: 2024-12-12 06:31:29.417685
## 43: 2024-12-12 06:31:29.417685
## 44: 2024-12-12 06:31:29.417685
## 45: 2024-12-12 06:31:29.417685
## 46: 2024-12-12 06:31:29.417685
## 47: 2024-12-12 06:31:29.417685
## 48: 2024-12-12 06:31:29.417685
## 49: 2024-12-12 06:31:29.417685
## createdDate
# Keep all Cache items created with an rnorm() call
keepCache(tmpDir, userTags = "rnorm", ask = FALSE)
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: ad0ea27476c50b66 objectName a
## 2: ad0ea27476c50b66 function rnorm
## 3: ad0ea27476c50b66 class numeric
## 4: ad0ea27476c50b66 object.size 1008
## 5: ad0ea27476c50b66 accessed 2024-12-12 06:31:28.27216
## 6: ad0ea27476c50b66 inCloud FALSE
## 7: ad0ea27476c50b66 fromDisk FALSE
## 8: ad0ea27476c50b66 resultHash
## 9: ad0ea27476c50b66 elapsedTimeDigest 0.001947403 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun 8.130074e-05 secs
## 11: ad0ea27476c50b66 otherFunctions vweave_rmarkdown
## 12: ad0ea27476c50b66 otherFunctions process_file
## 13: ad0ea27476c50b66 otherFunctions process_group
## 14: ad0ea27476c50b66 otherFunctions call_block
## 15: ad0ea27476c50b66 otherFunctions block_exec
## 16: ad0ea27476c50b66 otherFunctions eng_r
## 17: ad0ea27476c50b66 otherFunctions in_input_dir
## 18: ad0ea27476c50b66 otherFunctions in_dir
## 19: ad0ea27476c50b66 otherFunctions with_handlers
## 20: ad0ea27476c50b66 preDigest n:7eef4eae85fd9229
## 21: ad0ea27476c50b66 preDigest mean:c40c00762a0dac94
## 22: ad0ea27476c50b66 preDigest sd:853b1797f54b229c
## 23: ad0ea27476c50b66 preDigest .FUN:4f604aa46882b368
## 24: ad0ea27476c50b66 accessed 2024-12-12 06:31:29.31291
## 25: ad0ea27476c50b66 elapsedTimeLoad 0.004675388 secs
## 26: ad0ea27476c50b66 accessed 2024-12-12 06:31:29.401225
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:28.272841
## 2: 2024-12-12 06:31:28.272841
## 3: 2024-12-12 06:31:28.272841
## 4: 2024-12-12 06:31:28.272841
## 5: 2024-12-12 06:31:28.272841
## 6: 2024-12-12 06:31:28.272841
## 7: 2024-12-12 06:31:28.272841
## 8: 2024-12-12 06:31:28.272841
## 9: 2024-12-12 06:31:28.272841
## 10: 2024-12-12 06:31:28.272841
## 11: 2024-12-12 06:31:28.272841
## 12: 2024-12-12 06:31:28.272841
## 13: 2024-12-12 06:31:28.272841
## 14: 2024-12-12 06:31:28.272841
## 15: 2024-12-12 06:31:28.272841
## 16: 2024-12-12 06:31:28.272841
## 17: 2024-12-12 06:31:28.272841
## 18: 2024-12-12 06:31:28.272841
## 19: 2024-12-12 06:31:28.272841
## 20: 2024-12-12 06:31:28.272841
## 21: 2024-12-12 06:31:28.272841
## 22: 2024-12-12 06:31:28.272841
## 23: 2024-12-12 06:31:28.272841
## 24: 2024-12-12 06:31:29.31291
## 25: 2024-12-12 06:31:29.3162
## 26: 2024-12-12 06:31:29.401225
## createdDate
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: ad0ea27476c50b66 objectName a
## 2: ad0ea27476c50b66 function rnorm
## 3: ad0ea27476c50b66 class numeric
## 4: ad0ea27476c50b66 object.size 1008
## 5: ad0ea27476c50b66 accessed 2024-12-12 06:31:28.27216
## 6: ad0ea27476c50b66 inCloud FALSE
## 7: ad0ea27476c50b66 fromDisk FALSE
## 8: ad0ea27476c50b66 resultHash
## 9: ad0ea27476c50b66 elapsedTimeDigest 0.001947403 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun 8.130074e-05 secs
## 11: ad0ea27476c50b66 otherFunctions vweave_rmarkdown
## 12: ad0ea27476c50b66 otherFunctions process_file
## 13: ad0ea27476c50b66 otherFunctions process_group
## 14: ad0ea27476c50b66 otherFunctions call_block
## 15: ad0ea27476c50b66 otherFunctions block_exec
## 16: ad0ea27476c50b66 otherFunctions eng_r
## 17: ad0ea27476c50b66 otherFunctions in_input_dir
## 18: ad0ea27476c50b66 otherFunctions in_dir
## 19: ad0ea27476c50b66 otherFunctions with_handlers
## 20: ad0ea27476c50b66 preDigest n:7eef4eae85fd9229
## 21: ad0ea27476c50b66 preDigest mean:c40c00762a0dac94
## 22: ad0ea27476c50b66 preDigest sd:853b1797f54b229c
## 23: ad0ea27476c50b66 preDigest .FUN:4f604aa46882b368
## 24: ad0ea27476c50b66 accessed 2024-12-12 06:31:29.31291
## 25: ad0ea27476c50b66 elapsedTimeLoad 0.004675388 secs
## 26: ad0ea27476c50b66 accessed 2024-12-12 06:31:29.401225
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:28.272841
## 2: 2024-12-12 06:31:28.272841
## 3: 2024-12-12 06:31:28.272841
## 4: 2024-12-12 06:31:28.272841
## 5: 2024-12-12 06:31:28.272841
## 6: 2024-12-12 06:31:28.272841
## 7: 2024-12-12 06:31:28.272841
## 8: 2024-12-12 06:31:28.272841
## 9: 2024-12-12 06:31:28.272841
## 10: 2024-12-12 06:31:28.272841
## 11: 2024-12-12 06:31:28.272841
## 12: 2024-12-12 06:31:28.272841
## 13: 2024-12-12 06:31:28.272841
## 14: 2024-12-12 06:31:28.272841
## 15: 2024-12-12 06:31:28.272841
## 16: 2024-12-12 06:31:28.272841
## 17: 2024-12-12 06:31:28.272841
## 18: 2024-12-12 06:31:28.272841
## 19: 2024-12-12 06:31:28.272841
## 20: 2024-12-12 06:31:28.272841
## 21: 2024-12-12 06:31:28.272841
## 22: 2024-12-12 06:31:28.272841
## 23: 2024-12-12 06:31:28.272841
## 24: 2024-12-12 06:31:29.31291
## 25: 2024-12-12 06:31:29.3162
## 26: 2024-12-12 06:31:29.401225
## createdDate
# Remove all Cache items that happened within a rnorm() call
clearCache(tmpDir, userTags = "rnorm", ask = FALSE)
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## 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
# Also, can set a time before caching happens and remove based on this
# --> a useful, simple way to control Cache
ranNumsA <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: ad0ea27476c50b66.rds; fn: rnorm
startTime <- Sys.time()
Sys.sleep(1)
ranNumsB <- Cache(rnorm, 5, cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: ccacbf62081a42b4.rds; fn: rnorm
## Cache size:
## Total (including Rasters): 256 bytes
## Selected objects (not including Rasters): 256 bytes
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: ccacbf62081a42b4 objectName b
## 2: ccacbf62081a42b4 function rnorm
## 3: ccacbf62081a42b4 class numeric
## 4: ccacbf62081a42b4 object.size 1024
## 5: ccacbf62081a42b4 accessed 2024-12-12 06:31:30.54391
## 6: ccacbf62081a42b4 inCloud FALSE
## 7: ccacbf62081a42b4 fromDisk FALSE
## 8: ccacbf62081a42b4 resultHash
## 9: ccacbf62081a42b4 elapsedTimeDigest 0.002096653 secs
## 10: ccacbf62081a42b4 elapsedTimeFirstRun 8.893013e-05 secs
## 11: ccacbf62081a42b4 otherFunctions vweave_rmarkdown
## 12: ccacbf62081a42b4 otherFunctions process_file
## 13: ccacbf62081a42b4 otherFunctions process_group
## 14: ccacbf62081a42b4 otherFunctions call_block
## 15: ccacbf62081a42b4 otherFunctions block_exec
## 16: ccacbf62081a42b4 otherFunctions eng_r
## 17: ccacbf62081a42b4 otherFunctions in_input_dir
## 18: ccacbf62081a42b4 otherFunctions in_dir
## 19: ccacbf62081a42b4 otherFunctions with_handlers
## 20: ccacbf62081a42b4 preDigest n:a4f076b3db622faf
## 21: ccacbf62081a42b4 preDigest mean:c40c00762a0dac94
## 22: ccacbf62081a42b4 preDigest sd:853b1797f54b229c
## 23: ccacbf62081a42b4 preDigest .FUN:4f604aa46882b368
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:30.544583
## 2: 2024-12-12 06:31:30.544583
## 3: 2024-12-12 06:31:30.544583
## 4: 2024-12-12 06:31:30.544583
## 5: 2024-12-12 06:31:30.544583
## 6: 2024-12-12 06:31:30.544583
## 7: 2024-12-12 06:31:30.544583
## 8: 2024-12-12 06:31:30.544583
## 9: 2024-12-12 06:31:30.544583
## 10: 2024-12-12 06:31:30.544583
## 11: 2024-12-12 06:31:30.544583
## 12: 2024-12-12 06:31:30.544583
## 13: 2024-12-12 06:31:30.544583
## 14: 2024-12-12 06:31:30.544583
## 15: 2024-12-12 06:31:30.544583
## 16: 2024-12-12 06:31:30.544583
## 17: 2024-12-12 06:31:30.544583
## 18: 2024-12-12 06:31:30.544583
## 19: 2024-12-12 06:31:30.544583
## 20: 2024-12-12 06:31:30.544583
## 21: 2024-12-12 06:31:30.544583
## 22: 2024-12-12 06:31:30.544583
## 23: 2024-12-12 06:31:30.544583
## createdDate
# default userTags is "and" matching; for "or" matching use |
ranNumsA <- Cache(runif, 4, cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: deaa37372f85861b.rds; fn: runif
## Saved! Cache file: ad0ea27476c50b66.rds; fn: rnorm
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 504 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: ad0ea27476c50b66 objectName b
## 2: ad0ea27476c50b66 function rnorm
## 3: ad0ea27476c50b66 class numeric
## 4: ad0ea27476c50b66 object.size 1008
## 5: ad0ea27476c50b66 accessed 2024-12-12 06:31:30.663732
## 6: ad0ea27476c50b66 inCloud FALSE
## 7: ad0ea27476c50b66 fromDisk FALSE
## 8: ad0ea27476c50b66 resultHash
## 9: ad0ea27476c50b66 elapsedTimeDigest 0.001788139 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun 7.987022e-05 secs
## 11: ad0ea27476c50b66 otherFunctions vweave_rmarkdown
## 12: ad0ea27476c50b66 otherFunctions process_file
## 13: ad0ea27476c50b66 otherFunctions process_group
## 14: ad0ea27476c50b66 otherFunctions call_block
## 15: ad0ea27476c50b66 otherFunctions block_exec
## 16: ad0ea27476c50b66 otherFunctions eng_r
## 17: ad0ea27476c50b66 otherFunctions in_input_dir
## 18: ad0ea27476c50b66 otherFunctions in_dir
## 19: ad0ea27476c50b66 otherFunctions with_handlers
## 20: ad0ea27476c50b66 preDigest n:7eef4eae85fd9229
## 21: ad0ea27476c50b66 preDigest mean:c40c00762a0dac94
## 22: ad0ea27476c50b66 preDigest sd:853b1797f54b229c
## 23: ad0ea27476c50b66 preDigest .FUN:4f604aa46882b368
## 24: deaa37372f85861b objectName a
## 25: deaa37372f85861b function runif
## 26: deaa37372f85861b class numeric
## 27: deaa37372f85861b object.size 1008
## 28: deaa37372f85861b accessed 2024-12-12 06:31:30.644447
## 29: deaa37372f85861b inCloud FALSE
## 30: deaa37372f85861b fromDisk FALSE
## 31: deaa37372f85861b resultHash
## 32: deaa37372f85861b elapsedTimeDigest 0.002010584 secs
## 33: deaa37372f85861b elapsedTimeFirstRun 8.249283e-05 secs
## 34: deaa37372f85861b otherFunctions vweave_rmarkdown
## 35: deaa37372f85861b otherFunctions process_file
## 36: deaa37372f85861b otherFunctions process_group
## 37: deaa37372f85861b otherFunctions call_block
## 38: deaa37372f85861b otherFunctions block_exec
## 39: deaa37372f85861b otherFunctions eng_r
## 40: deaa37372f85861b otherFunctions in_input_dir
## 41: deaa37372f85861b otherFunctions in_dir
## 42: deaa37372f85861b otherFunctions with_handlers
## 43: deaa37372f85861b preDigest n:7eef4eae85fd9229
## 44: deaa37372f85861b preDigest min:c40c00762a0dac94
## 45: deaa37372f85861b preDigest max:853b1797f54b229c
## 46: deaa37372f85861b preDigest .FUN:881ec847b7161f3c
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:30.664379
## 2: 2024-12-12 06:31:30.664379
## 3: 2024-12-12 06:31:30.664379
## 4: 2024-12-12 06:31:30.664379
## 5: 2024-12-12 06:31:30.664379
## 6: 2024-12-12 06:31:30.664379
## 7: 2024-12-12 06:31:30.664379
## 8: 2024-12-12 06:31:30.664379
## 9: 2024-12-12 06:31:30.664379
## 10: 2024-12-12 06:31:30.664379
## 11: 2024-12-12 06:31:30.664379
## 12: 2024-12-12 06:31:30.664379
## 13: 2024-12-12 06:31:30.664379
## 14: 2024-12-12 06:31:30.664379
## 15: 2024-12-12 06:31:30.664379
## 16: 2024-12-12 06:31:30.664379
## 17: 2024-12-12 06:31:30.664379
## 18: 2024-12-12 06:31:30.664379
## 19: 2024-12-12 06:31:30.664379
## 20: 2024-12-12 06:31:30.664379
## 21: 2024-12-12 06:31:30.664379
## 22: 2024-12-12 06:31:30.664379
## 23: 2024-12-12 06:31:30.664379
## 24: 2024-12-12 06:31:30.645143
## 25: 2024-12-12 06:31:30.645143
## 26: 2024-12-12 06:31:30.645143
## 27: 2024-12-12 06:31:30.645143
## 28: 2024-12-12 06:31:30.645143
## 29: 2024-12-12 06:31:30.645143
## 30: 2024-12-12 06:31:30.645143
## 31: 2024-12-12 06:31:30.645143
## 32: 2024-12-12 06:31:30.645143
## 33: 2024-12-12 06:31:30.645143
## 34: 2024-12-12 06:31:30.645143
## 35: 2024-12-12 06:31:30.645143
## 36: 2024-12-12 06:31:30.645143
## 37: 2024-12-12 06:31:30.645143
## 38: 2024-12-12 06:31:30.645143
## 39: 2024-12-12 06:31:30.645143
## 40: 2024-12-12 06:31:30.645143
## 41: 2024-12-12 06:31:30.645143
## 42: 2024-12-12 06:31:30.645143
## 43: 2024-12-12 06:31:30.645143
## 44: 2024-12-12 06:31:30.645143
## 45: 2024-12-12 06:31:30.645143
## 46: 2024-12-12 06:31:30.645143
## createdDate
# 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): 504 bytes
## Selected objects (not including Rasters): 504 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: ad0ea27476c50b66 objectName b
## 2: ad0ea27476c50b66 function rnorm
## 3: ad0ea27476c50b66 class numeric
## 4: ad0ea27476c50b66 object.size 1008
## 5: ad0ea27476c50b66 accessed 2024-12-12 06:31:30.663732
## 6: ad0ea27476c50b66 inCloud FALSE
## 7: ad0ea27476c50b66 fromDisk FALSE
## 8: ad0ea27476c50b66 resultHash
## 9: ad0ea27476c50b66 elapsedTimeDigest 0.001788139 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun 7.987022e-05 secs
## 11: ad0ea27476c50b66 otherFunctions vweave_rmarkdown
## 12: ad0ea27476c50b66 otherFunctions process_file
## 13: ad0ea27476c50b66 otherFunctions process_group
## 14: ad0ea27476c50b66 otherFunctions call_block
## 15: ad0ea27476c50b66 otherFunctions block_exec
## 16: ad0ea27476c50b66 otherFunctions eng_r
## 17: ad0ea27476c50b66 otherFunctions in_input_dir
## 18: ad0ea27476c50b66 otherFunctions in_dir
## 19: ad0ea27476c50b66 otherFunctions with_handlers
## 20: ad0ea27476c50b66 preDigest n:7eef4eae85fd9229
## 21: ad0ea27476c50b66 preDigest mean:c40c00762a0dac94
## 22: ad0ea27476c50b66 preDigest sd:853b1797f54b229c
## 23: ad0ea27476c50b66 preDigest .FUN:4f604aa46882b368
## 24: deaa37372f85861b objectName a
## 25: deaa37372f85861b function runif
## 26: deaa37372f85861b class numeric
## 27: deaa37372f85861b object.size 1008
## 28: deaa37372f85861b accessed 2024-12-12 06:31:30.644447
## 29: deaa37372f85861b inCloud FALSE
## 30: deaa37372f85861b fromDisk FALSE
## 31: deaa37372f85861b resultHash
## 32: deaa37372f85861b elapsedTimeDigest 0.002010584 secs
## 33: deaa37372f85861b elapsedTimeFirstRun 8.249283e-05 secs
## 34: deaa37372f85861b otherFunctions vweave_rmarkdown
## 35: deaa37372f85861b otherFunctions process_file
## 36: deaa37372f85861b otherFunctions process_group
## 37: deaa37372f85861b otherFunctions call_block
## 38: deaa37372f85861b otherFunctions block_exec
## 39: deaa37372f85861b otherFunctions eng_r
## 40: deaa37372f85861b otherFunctions in_input_dir
## 41: deaa37372f85861b otherFunctions in_dir
## 42: deaa37372f85861b otherFunctions with_handlers
## 43: deaa37372f85861b preDigest n:7eef4eae85fd9229
## 44: deaa37372f85861b preDigest min:c40c00762a0dac94
## 45: deaa37372f85861b preDigest max:853b1797f54b229c
## 46: deaa37372f85861b preDigest .FUN:881ec847b7161f3c
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:30.664379
## 2: 2024-12-12 06:31:30.664379
## 3: 2024-12-12 06:31:30.664379
## 4: 2024-12-12 06:31:30.664379
## 5: 2024-12-12 06:31:30.664379
## 6: 2024-12-12 06:31:30.664379
## 7: 2024-12-12 06:31:30.664379
## 8: 2024-12-12 06:31:30.664379
## 9: 2024-12-12 06:31:30.664379
## 10: 2024-12-12 06:31:30.664379
## 11: 2024-12-12 06:31:30.664379
## 12: 2024-12-12 06:31:30.664379
## 13: 2024-12-12 06:31:30.664379
## 14: 2024-12-12 06:31:30.664379
## 15: 2024-12-12 06:31:30.664379
## 16: 2024-12-12 06:31:30.664379
## 17: 2024-12-12 06:31:30.664379
## 18: 2024-12-12 06:31:30.664379
## 19: 2024-12-12 06:31:30.664379
## 20: 2024-12-12 06:31:30.664379
## 21: 2024-12-12 06:31:30.664379
## 22: 2024-12-12 06:31:30.664379
## 23: 2024-12-12 06:31:30.664379
## 24: 2024-12-12 06:31:30.645143
## 25: 2024-12-12 06:31:30.645143
## 26: 2024-12-12 06:31:30.645143
## 27: 2024-12-12 06:31:30.645143
## 28: 2024-12-12 06:31:30.645143
## 29: 2024-12-12 06:31:30.645143
## 30: 2024-12-12 06:31:30.645143
## 31: 2024-12-12 06:31:30.645143
## 32: 2024-12-12 06:31:30.645143
## 33: 2024-12-12 06:31:30.645143
## 34: 2024-12-12 06:31:30.645143
## 35: 2024-12-12 06:31:30.645143
## 36: 2024-12-12 06:31:30.645143
## 37: 2024-12-12 06:31:30.645143
## 38: 2024-12-12 06:31:30.645143
## 39: 2024-12-12 06:31:30.645143
## 40: 2024-12-12 06:31:30.645143
## 41: 2024-12-12 06:31:30.645143
## 42: 2024-12-12 06:31:30.645143
## 43: 2024-12-12 06:31:30.645143
## 44: 2024-12-12 06:31:30.645143
## 45: 2024-12-12 06:31:30.645143
## 46: 2024-12-12 06:31:30.645143
## createdDate
# keep only objects that are either runif or rnorm ("or" search)
keepCache(tmpDir, userTags = "runif|rnorm", ask = FALSE)
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 504 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: ad0ea27476c50b66 objectName b
## 2: ad0ea27476c50b66 function rnorm
## 3: ad0ea27476c50b66 class numeric
## 4: ad0ea27476c50b66 object.size 1008
## 5: ad0ea27476c50b66 accessed 2024-12-12 06:31:30.663732
## 6: ad0ea27476c50b66 inCloud FALSE
## 7: ad0ea27476c50b66 fromDisk FALSE
## 8: ad0ea27476c50b66 resultHash
## 9: ad0ea27476c50b66 elapsedTimeDigest 0.001788139 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun 7.987022e-05 secs
## 11: ad0ea27476c50b66 otherFunctions vweave_rmarkdown
## 12: ad0ea27476c50b66 otherFunctions process_file
## 13: ad0ea27476c50b66 otherFunctions process_group
## 14: ad0ea27476c50b66 otherFunctions call_block
## 15: ad0ea27476c50b66 otherFunctions block_exec
## 16: ad0ea27476c50b66 otherFunctions eng_r
## 17: ad0ea27476c50b66 otherFunctions in_input_dir
## 18: ad0ea27476c50b66 otherFunctions in_dir
## 19: ad0ea27476c50b66 otherFunctions with_handlers
## 20: ad0ea27476c50b66 preDigest n:7eef4eae85fd9229
## 21: ad0ea27476c50b66 preDigest mean:c40c00762a0dac94
## 22: ad0ea27476c50b66 preDigest sd:853b1797f54b229c
## 23: ad0ea27476c50b66 preDigest .FUN:4f604aa46882b368
## 24: deaa37372f85861b objectName a
## 25: deaa37372f85861b function runif
## 26: deaa37372f85861b class numeric
## 27: deaa37372f85861b object.size 1008
## 28: deaa37372f85861b accessed 2024-12-12 06:31:30.644447
## 29: deaa37372f85861b inCloud FALSE
## 30: deaa37372f85861b fromDisk FALSE
## 31: deaa37372f85861b resultHash
## 32: deaa37372f85861b elapsedTimeDigest 0.002010584 secs
## 33: deaa37372f85861b elapsedTimeFirstRun 8.249283e-05 secs
## 34: deaa37372f85861b otherFunctions vweave_rmarkdown
## 35: deaa37372f85861b otherFunctions process_file
## 36: deaa37372f85861b otherFunctions process_group
## 37: deaa37372f85861b otherFunctions call_block
## 38: deaa37372f85861b otherFunctions block_exec
## 39: deaa37372f85861b otherFunctions eng_r
## 40: deaa37372f85861b otherFunctions in_input_dir
## 41: deaa37372f85861b otherFunctions in_dir
## 42: deaa37372f85861b otherFunctions with_handlers
## 43: deaa37372f85861b preDigest n:7eef4eae85fd9229
## 44: deaa37372f85861b preDigest min:c40c00762a0dac94
## 45: deaa37372f85861b preDigest max:853b1797f54b229c
## 46: deaa37372f85861b preDigest .FUN:881ec847b7161f3c
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:30.664379
## 2: 2024-12-12 06:31:30.664379
## 3: 2024-12-12 06:31:30.664379
## 4: 2024-12-12 06:31:30.664379
## 5: 2024-12-12 06:31:30.664379
## 6: 2024-12-12 06:31:30.664379
## 7: 2024-12-12 06:31:30.664379
## 8: 2024-12-12 06:31:30.664379
## 9: 2024-12-12 06:31:30.664379
## 10: 2024-12-12 06:31:30.664379
## 11: 2024-12-12 06:31:30.664379
## 12: 2024-12-12 06:31:30.664379
## 13: 2024-12-12 06:31:30.664379
## 14: 2024-12-12 06:31:30.664379
## 15: 2024-12-12 06:31:30.664379
## 16: 2024-12-12 06:31:30.664379
## 17: 2024-12-12 06:31:30.664379
## 18: 2024-12-12 06:31:30.664379
## 19: 2024-12-12 06:31:30.664379
## 20: 2024-12-12 06:31:30.664379
## 21: 2024-12-12 06:31:30.664379
## 22: 2024-12-12 06:31:30.664379
## 23: 2024-12-12 06:31:30.664379
## 24: 2024-12-12 06:31:30.645143
## 25: 2024-12-12 06:31:30.645143
## 26: 2024-12-12 06:31:30.645143
## 27: 2024-12-12 06:31:30.645143
## 28: 2024-12-12 06:31:30.645143
## 29: 2024-12-12 06:31:30.645143
## 30: 2024-12-12 06:31:30.645143
## 31: 2024-12-12 06:31:30.645143
## 32: 2024-12-12 06:31:30.645143
## 33: 2024-12-12 06:31:30.645143
## 34: 2024-12-12 06:31:30.645143
## 35: 2024-12-12 06:31:30.645143
## 36: 2024-12-12 06:31:30.645143
## 37: 2024-12-12 06:31:30.645143
## 38: 2024-12-12 06:31:30.645143
## 39: 2024-12-12 06:31:30.645143
## 40: 2024-12-12 06:31:30.645143
## 41: 2024-12-12 06:31:30.645143
## 42: 2024-12-12 06:31:30.645143
## 43: 2024-12-12 06:31:30.645143
## 44: 2024-12-12 06:31:30.645143
## 45: 2024-12-12 06:31:30.645143
## 46: 2024-12-12 06:31:30.645143
## createdDate
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
Cache(terra::project, ras, rasCRS, res = 5, cachePath = tmpDir)
)
## Saved! Cache file: 2fcf969127c6a43e.rds; fn: terra::project
# second time is much faster
reRun <- suppressWarnings(
# project raster generates warnings when run non-interactively
Cache(terra::project, ras, rasCRS, res = 5, cachePath = tmpDir)
)
## Object to retrieve (fn: terra::project, 2fcf969127c6a43e.rds) ...
## Loaded! Cached result from previous terra::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, 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
Cache(rnorm, n = 3, mean = mean)
}
outer <- function(n) {
Cache(inner, 0.1, cachePath = tmpdir2)
}
# make 2 different cache paths
tmpdir1 <- file.path(tempdir(), "first")
tmpdir2 <- file.path(tempdir(), "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
Cache(outer, n = 2, cachePath = tmpdir1, notOlderThan = Sys.time())
## No cachePath supplied and getOption('reproducible.cachePath') is inside a temporary directory;
## this will not persist across R sessions.
## Saved! Cache file: efa1ccee79a31d4c.rds; fn: rnorm
## Saved! Cache file: 33ceb4fb525fd08f.rds; fn: inner
## Saved! Cache file: dffc8e3bf8c23b6e.rds; fn: outer
## [1] 1.1396995 -1.0882882 0.8871979
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## attr(,"tags")
## [1] "cacheId:dffc8e3bf8c23b6e"
## attr(,"call")
## [1] ""
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: dffc8e3bf8c23b6e function outer
## 2: dffc8e3bf8c23b6e class numeric
## 3: dffc8e3bf8c23b6e object.size 1008
## 4: dffc8e3bf8c23b6e accessed 2024-12-12 06:31:31.002818
## 5: dffc8e3bf8c23b6e inCloud FALSE
## 6: dffc8e3bf8c23b6e fromDisk FALSE
## 7: dffc8e3bf8c23b6e resultHash
## 8: dffc8e3bf8c23b6e elapsedTimeDigest 0.001801729 secs
## 9: dffc8e3bf8c23b6e elapsedTimeFirstRun 0.06084132 secs
## 10: dffc8e3bf8c23b6e otherFunctions vweave_rmarkdown
## 11: dffc8e3bf8c23b6e otherFunctions process_file
## 12: dffc8e3bf8c23b6e otherFunctions process_group
## 13: dffc8e3bf8c23b6e otherFunctions call_block
## 14: dffc8e3bf8c23b6e otherFunctions block_exec
## 15: dffc8e3bf8c23b6e otherFunctions eng_r
## 16: dffc8e3bf8c23b6e otherFunctions in_input_dir
## 17: dffc8e3bf8c23b6e otherFunctions in_dir
## 18: dffc8e3bf8c23b6e otherFunctions with_handlers
## 19: dffc8e3bf8c23b6e preDigest n:82dc709f2b91918a
## 20: dffc8e3bf8c23b6e preDigest .FUN:b89dd186387954a0
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:31.003439
## 2: 2024-12-12 06:31:31.003439
## 3: 2024-12-12 06:31:31.003439
## 4: 2024-12-12 06:31:31.003439
## 5: 2024-12-12 06:31:31.003439
## 6: 2024-12-12 06:31:31.003439
## 7: 2024-12-12 06:31:31.003439
## 8: 2024-12-12 06:31:31.003439
## 9: 2024-12-12 06:31:31.003439
## 10: 2024-12-12 06:31:31.003439
## 11: 2024-12-12 06:31:31.003439
## 12: 2024-12-12 06:31:31.003439
## 13: 2024-12-12 06:31:31.003439
## 14: 2024-12-12 06:31:31.003439
## 15: 2024-12-12 06:31:31.003439
## 16: 2024-12-12 06:31:31.003439
## 17: 2024-12-12 06:31:31.003439
## 18: 2024-12-12 06:31:31.003439
## 19: 2024-12-12 06:31:31.003439
## 20: 2024-12-12 06:31:31.003439
## createdDate
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: 33ceb4fb525fd08f function inner
## 2: 33ceb4fb525fd08f class numeric
## 3: 33ceb4fb525fd08f object.size 1008
## 4: 33ceb4fb525fd08f accessed 2024-12-12 06:31:30.990251
## 5: 33ceb4fb525fd08f inCloud FALSE
## 6: 33ceb4fb525fd08f fromDisk FALSE
## 7: 33ceb4fb525fd08f resultHash
## 8: 33ceb4fb525fd08f elapsedTimeDigest 0.001784325 secs
## 9: 33ceb4fb525fd08f elapsedTimeFirstRun 0.02917814 secs
## 10: 33ceb4fb525fd08f otherFunctions vweave_rmarkdown
## 11: 33ceb4fb525fd08f otherFunctions process_file
## 12: 33ceb4fb525fd08f otherFunctions process_group
## 13: 33ceb4fb525fd08f otherFunctions call_block
## 14: 33ceb4fb525fd08f otherFunctions block_exec
## 15: 33ceb4fb525fd08f otherFunctions eng_r
## 16: 33ceb4fb525fd08f otherFunctions in_input_dir
## 17: 33ceb4fb525fd08f otherFunctions in_dir
## 18: 33ceb4fb525fd08f otherFunctions with_handlers
## 19: 33ceb4fb525fd08f otherFunctions out
## 20: 33ceb4fb525fd08f preDigest mean:22413394efd9f6a3
## 21: 33ceb4fb525fd08f preDigest .FUN:87e2c30917a34d25
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:30.99091
## 2: 2024-12-12 06:31:30.99091
## 3: 2024-12-12 06:31:30.99091
## 4: 2024-12-12 06:31:30.99091
## 5: 2024-12-12 06:31:30.99091
## 6: 2024-12-12 06:31:30.99091
## 7: 2024-12-12 06:31:30.99091
## 8: 2024-12-12 06:31:30.99091
## 9: 2024-12-12 06:31:30.99091
## 10: 2024-12-12 06:31:30.99091
## 11: 2024-12-12 06:31:30.99091
## 12: 2024-12-12 06:31:30.99091
## 13: 2024-12-12 06:31:30.99091
## 14: 2024-12-12 06:31:30.99091
## 15: 2024-12-12 06:31:30.99091
## 16: 2024-12-12 06:31:30.99091
## 17: 2024-12-12 06:31:30.99091
## 18: 2024-12-12 06:31:30.99091
## 19: 2024-12-12 06:31:30.99091
## 20: 2024-12-12 06:31:30.99091
## 21: 2024-12-12 06:31:30.99091
## createdDate
# 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
Cache(rnorm, n = 3, mean = mean, notOlderThan = Sys.time() - 1e5, userTags = innerTag)
}
outer <- function(n) {
Cache(inner, 0.1)
}
aa <- Cache(outer, n = 2, 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.
## Object to retrieve (fn: rnorm, efa1ccee79a31d4c.rds) ...
## Loaded! Cached result from previous rnorm call
## Saved! Cache file: b06af03d5a73dc7d.rds; fn: inner
## Saved! Cache file: 88a34e1d033329e5.rds; fn: outer
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## cacheId tagKey tagValue
## <char> <char> <char>
## 1: 88a34e1d033329e5 outerTag outerTag
## 2: 88a34e1d033329e5 function outer
## 3: 88a34e1d033329e5 class numeric
## 4: 88a34e1d033329e5 object.size 1008
## 5: 88a34e1d033329e5 accessed 2024-12-12 06:31:31.095037
## 6: 88a34e1d033329e5 inCloud FALSE
## 7: 88a34e1d033329e5 fromDisk FALSE
## 8: 88a34e1d033329e5 resultHash
## 9: 88a34e1d033329e5 elapsedTimeDigest 0.001629829 secs
## 10: 88a34e1d033329e5 elapsedTimeFirstRun 0.036551 secs
## 11: 88a34e1d033329e5 otherFunctions vweave_rmarkdown
## 12: 88a34e1d033329e5 otherFunctions process_file
## 13: 88a34e1d033329e5 otherFunctions process_group
## 14: 88a34e1d033329e5 otherFunctions call_block
## 15: 88a34e1d033329e5 otherFunctions block_exec
## 16: 88a34e1d033329e5 otherFunctions eng_r
## 17: 88a34e1d033329e5 otherFunctions in_input_dir
## 18: 88a34e1d033329e5 otherFunctions in_dir
## 19: 88a34e1d033329e5 otherFunctions with_handlers
## 20: 88a34e1d033329e5 preDigest n:82dc709f2b91918a
## 21: 88a34e1d033329e5 preDigest .FUN:5f06fb5fbffe9e3b
## cacheId tagKey tagValue
## createdDate
## <char>
## 1: 2024-12-12 06:31:31.095654
## 2: 2024-12-12 06:31:31.095654
## 3: 2024-12-12 06:31:31.095654
## 4: 2024-12-12 06:31:31.095654
## 5: 2024-12-12 06:31:31.095654
## 6: 2024-12-12 06:31:31.095654
## 7: 2024-12-12 06:31:31.095654
## 8: 2024-12-12 06:31:31.095654
## 9: 2024-12-12 06:31:31.095654
## 10: 2024-12-12 06:31:31.095654
## 11: 2024-12-12 06:31:31.095654
## 12: 2024-12-12 06:31:31.095654
## 13: 2024-12-12 06:31:31.095654
## 14: 2024-12-12 06:31:31.095654
## 15: 2024-12-12 06:31:31.095654
## 16: 2024-12-12 06:31:31.095654
## 17: 2024-12-12 06:31:31.095654
## 18: 2024-12-12 06:31:31.095654
## 19: 2024-12-12 06:31:31.095654
## 20: 2024-12-12 06:31:31.095654
## 21: 2024-12-12 06:31:31.095654
## createdDate
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"
.
## Saved! Cache file: 422bae4ed2f770cc.rds; fn: rnorm
## [1] 1.511781
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## attr(,"tags")
## [1] "cacheId:422bae4ed2f770cc"
## attr(,"call")
## [1] ""
# manually look at output attribute which shows cacheId: 7072c305d8c69df0
Cache(rnorm, 1, cachePath = tmpdir1, cacheId = "422bae4ed2f770cc") # same value
## cacheId is same as calculated hash
## Object to retrieve (fn: rnorm, 422bae4ed2f770cc.rds) ...
## Loaded! Cached result from previous rnorm call
## [1] 1.511781
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] FALSE
##
## attr(,"tags")
## [1] "cacheId:422bae4ed2f770cc"
## attr(,"call")
## [1] ""
# override even with different inputs:
Cache(rnorm, 2, cachePath = tmpdir1, cacheId = "422bae4ed2f770cc")
## cacheId is not same as calculated hash. Manually searching for
## cacheId:422bae4ed2f770cc
## Saved! Cache file: 422bae4ed2f770cc.rds; fn: rnorm
## [1] -1.3770596 -0.4149946
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## attr(,"tags")
## [1] "cacheId:422bae4ed2f770cc"
## attr(,"call")
## [1] ""
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): 978 bytes
## Selected objects (not including Rasters): 978 bytes
## Loaded! Cached result from previous call
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.
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.