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 fastdigest::fastdigest
on R
memory objects and digest::digest
on 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 using:
Cache(FUN = functionName, ...)
.
This will be a slight change to a function call, such as:
projectRaster(raster, crs = crs(newRaster))
to
Cache(projectRaster, raster, crs = crs(newRaster))
.
This is particularly useful for expensive operations.
library(raster)
## Loading required package: sp
library(reproducible)
<- file.path(tempdir(), "reproducible_examples", "Cache")
tmpDir checkPath(tmpDir, create = TRUE)
## [1] "/tmp/RtmpWKofw4/reproducible_examples/Cache"
<- raster(extent(0, 1000, 0, 1000), vals = 1:1e6, res = 1)
ras crs(ras) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"
<- "+init=epsg:4326" # A longlat crs
newCRS
# No Cache
system.time(suppressWarnings(map1 <- projectRaster(ras, crs = newCRS))) # Warnings due to new PROJ
## user system elapsed
## 1.787 0.169 1.958
# Try with memoise for this example -- for many simple cases, memoising will not be faster
<- options("reproducible.useMemoise" = TRUE)
opts # With Cache -- a little slower the first time because saving to disk
system.time(suppressWarnings(map1 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir,
notOlderThan = Sys.time())))
## user system elapsed
## 1.859 0.054 1.920
# vastly faster the second time
system.time(map2 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir))
## ...(Object to retrieve (7eed2ff11d587cb1.rds) is large: 6.5 Mb)
## loaded memoised result from previous projectRaster call
## user system elapsed
## 0.227 0.000 0.233
# may be faster the third time because of memoise; but this example is too simple to show
system.time(map3 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir))
## ...(Object to retrieve (7eed2ff11d587cb1.rds) is large: 6.5 Mb)
## loaded memoised result from previous projectRaster call
## user system elapsed
## 0.019 0.004 0.032
options(opts)
all.equal(map1, map2) # TRUE
## [1] TRUE
all.equal(map1, map3) # TRUE
## [1] TRUE
library(raster)
library(magrittr)
##
## Attaching package: 'magrittr'
## The following object is masked from 'package:raster':
##
## extract
try(clearCache(tmpDir, ask = FALSE), silent = TRUE) # just to make sure it is clear
<- Cache(rnorm, 10, 16, cacheRepo = tmpDir)
ranNumsA
# All same
<- Cache(rnorm, 10, 16, cacheRepo = tmpDir) # recovers cached copy ranNumsB
## ...(Object to retrieve (f11fb1a2880f8060.rds))
## loaded cached result from previous rnorm call
<- Cache(quote(rnorm(n = 10, 16)), cacheRepo = tmpDir) # recovers cached copy ranNumsD
## ...(Object to retrieve (f11fb1a2880f8060.rds))
## loaded cached result from previous rnorm call
# Any minor change makes it different
<- Cache(rnorm, 10, 6, cacheRepo = tmpDir) # different ranNumsE
<- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a")
ranNumsA <- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:b")
ranNumsB
# access it again, from Cache
Sys.sleep(1)
<- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a") ranNumsA
## ...(Object to retrieve (f7bee22047b8d0c1.rds))
## loaded cached result from previous rnorm call
<- showCache(tmpDir) wholeCache
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 504 bytes
# keep only items accessed "recently" (i.e., only objectName:a)
<- showCache(tmpDir, userTags = max(wholeCache[tagKey == "accessed"]$tagValue)) onlyRecentlyAccessed
## 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
<- unique(wholeCache[!onlyRecentlyAccessed, on = "cacheId"], by = "cacheId")$cacheId
toRemove clearCache(tmpDir, toRemove, ask = FALSE) # remove ones not recently accessed
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
showCache(tmpDir) # still has more recently accessed
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## cacheId tagKey tagValue
## 1: f7bee22047b8d0c1 objectName a
## 2: f7bee22047b8d0c1 function rnorm
## 3: f7bee22047b8d0c1 class numeric
## 4: f7bee22047b8d0c1 object.size 1008
## 5: f7bee22047b8d0c1 accessed 2022-08-16 09:24:12
## 6: f7bee22047b8d0c1 inCloud FALSE
## 7: f7bee22047b8d0c1 resultHash
## 8: f7bee22047b8d0c1 elapsedTimeDigest 0.0002450943 secs
## 9: f7bee22047b8d0c1 elapsedTimeFirstRun 2.193451e-05 secs
## 10: f7bee22047b8d0c1 otherFunctions vweave_rmarkdown
## 11: f7bee22047b8d0c1 otherFunctions process_file
## 12: f7bee22047b8d0c1 otherFunctions process_group
## 13: f7bee22047b8d0c1 otherFunctions process_group.block
## 14: f7bee22047b8d0c1 otherFunctions call_block
## 15: f7bee22047b8d0c1 otherFunctions block_exec
## 16: f7bee22047b8d0c1 otherFunctions eng_r
## 17: f7bee22047b8d0c1 otherFunctions in_input_dir
## 18: f7bee22047b8d0c1 otherFunctions in_dir
## 19: f7bee22047b8d0c1 otherFunctions timing_fn
## 20: f7bee22047b8d0c1 otherFunctions handle
## 21: f7bee22047b8d0c1 preDigest n:7eef4eae85fd9229
## 22: f7bee22047b8d0c1 preDigest .FUN:4f604aa46882b368
## 23: f7bee22047b8d0c1 file.size 174
## 24: f7bee22047b8d0c1 accessed 2022-08-16 09:24:13
## 25: f7bee22047b8d0c1 elapsedTimeLoad 0.008819103 secs
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:12
## 2: 2022-08-16 09:24:12
## 3: 2022-08-16 09:24:12
## 4: 2022-08-16 09:24:12
## 5: 2022-08-16 09:24:12
## 6: 2022-08-16 09:24:12
## 7: 2022-08-16 09:24:12
## 8: 2022-08-16 09:24:12
## 9: 2022-08-16 09:24:12
## 10: 2022-08-16 09:24:12
## 11: 2022-08-16 09:24:12
## 12: 2022-08-16 09:24:12
## 13: 2022-08-16 09:24:12
## 14: 2022-08-16 09:24:12
## 15: 2022-08-16 09:24:12
## 16: 2022-08-16 09:24:12
## 17: 2022-08-16 09:24:12
## 18: 2022-08-16 09:24:12
## 19: 2022-08-16 09:24:12
## 20: 2022-08-16 09:24:12
## 21: 2022-08-16 09:24:12
## 22: 2022-08-16 09:24:12
## 23: 2022-08-16 09:24:12
## 24: 2022-08-16 09:24:13
## 25: 2022-08-16 09:24:13
## createdDate
<- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a") ranNumsA
## ...(Object to retrieve (f7bee22047b8d0c1.rds))
## loaded cached result from previous rnorm call
<- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:b")
ranNumsB
# keep only those cached items from the last 24 hours
<- 60 * 60 * 24
oneDay 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
## 1: f7bee22047b8d0c1 objectName a
## 2: f7bee22047b8d0c1 function rnorm
## 3: f7bee22047b8d0c1 class numeric
## 4: f7bee22047b8d0c1 object.size 1008
## 5: f7bee22047b8d0c1 accessed 2022-08-16 09:24:12
## 6: f7bee22047b8d0c1 inCloud FALSE
## 7: f7bee22047b8d0c1 resultHash
## 8: f7bee22047b8d0c1 elapsedTimeDigest 0.0002450943 secs
## 9: f7bee22047b8d0c1 elapsedTimeFirstRun 2.193451e-05 secs
## 10: f7bee22047b8d0c1 otherFunctions vweave_rmarkdown
## 11: f7bee22047b8d0c1 otherFunctions process_file
## 12: f7bee22047b8d0c1 otherFunctions process_group
## 13: f7bee22047b8d0c1 otherFunctions process_group.block
## 14: f7bee22047b8d0c1 otherFunctions call_block
## 15: f7bee22047b8d0c1 otherFunctions block_exec
## 16: f7bee22047b8d0c1 otherFunctions eng_r
## 17: f7bee22047b8d0c1 otherFunctions in_input_dir
## 18: f7bee22047b8d0c1 otherFunctions in_dir
## 19: f7bee22047b8d0c1 otherFunctions timing_fn
## 20: f7bee22047b8d0c1 otherFunctions handle
## 21: f7bee22047b8d0c1 preDigest n:7eef4eae85fd9229
## 22: f7bee22047b8d0c1 preDigest .FUN:4f604aa46882b368
## 23: f7bee22047b8d0c1 file.size 174
## 24: f7bee22047b8d0c1 accessed 2022-08-16 09:24:13
## 25: f7bee22047b8d0c1 elapsedTimeLoad 0.002529383 secs
## 26: f7bee22047b8d0c1 accessed 2022-08-16 09:24:13
## 27: 3aef38d1fc02aee5 objectName b
## 28: 3aef38d1fc02aee5 function runif
## 29: 3aef38d1fc02aee5 class numeric
## 30: 3aef38d1fc02aee5 object.size 1008
## 31: 3aef38d1fc02aee5 accessed 2022-08-16 09:24:13
## 32: 3aef38d1fc02aee5 inCloud FALSE
## 33: 3aef38d1fc02aee5 resultHash
## 34: 3aef38d1fc02aee5 elapsedTimeDigest 0.0002377033 secs
## 35: 3aef38d1fc02aee5 elapsedTimeFirstRun 2.169609e-05 secs
## 36: 3aef38d1fc02aee5 otherFunctions vweave_rmarkdown
## 37: 3aef38d1fc02aee5 otherFunctions process_file
## 38: 3aef38d1fc02aee5 otherFunctions process_group
## 39: 3aef38d1fc02aee5 otherFunctions process_group.block
## 40: 3aef38d1fc02aee5 otherFunctions call_block
## 41: 3aef38d1fc02aee5 otherFunctions block_exec
## 42: 3aef38d1fc02aee5 otherFunctions eng_r
## 43: 3aef38d1fc02aee5 otherFunctions in_input_dir
## 44: 3aef38d1fc02aee5 otherFunctions in_dir
## 45: 3aef38d1fc02aee5 otherFunctions timing_fn
## 46: 3aef38d1fc02aee5 otherFunctions handle
## 47: 3aef38d1fc02aee5 preDigest n:7eef4eae85fd9229
## 48: 3aef38d1fc02aee5 preDigest .FUN:881ec847b7161f3c
## 49: 3aef38d1fc02aee5 file.size 167
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:12
## 2: 2022-08-16 09:24:12
## 3: 2022-08-16 09:24:12
## 4: 2022-08-16 09:24:12
## 5: 2022-08-16 09:24:12
## 6: 2022-08-16 09:24:12
## 7: 2022-08-16 09:24:12
## 8: 2022-08-16 09:24:12
## 9: 2022-08-16 09:24:12
## 10: 2022-08-16 09:24:12
## 11: 2022-08-16 09:24:12
## 12: 2022-08-16 09:24:12
## 13: 2022-08-16 09:24:12
## 14: 2022-08-16 09:24:12
## 15: 2022-08-16 09:24:12
## 16: 2022-08-16 09:24:12
## 17: 2022-08-16 09:24:12
## 18: 2022-08-16 09:24:12
## 19: 2022-08-16 09:24:12
## 20: 2022-08-16 09:24:12
## 21: 2022-08-16 09:24:12
## 22: 2022-08-16 09:24:12
## 23: 2022-08-16 09:24:12
## 24: 2022-08-16 09:24:13
## 25: 2022-08-16 09:24:13
## 26: 2022-08-16 09:24:13
## 27: 2022-08-16 09:24:13
## 28: 2022-08-16 09:24:13
## 29: 2022-08-16 09:24:13
## 30: 2022-08-16 09:24:13
## 31: 2022-08-16 09:24:13
## 32: 2022-08-16 09:24:13
## 33: 2022-08-16 09:24:13
## 34: 2022-08-16 09:24:13
## 35: 2022-08-16 09:24:13
## 36: 2022-08-16 09:24:13
## 37: 2022-08-16 09:24:13
## 38: 2022-08-16 09:24:13
## 39: 2022-08-16 09:24:13
## 40: 2022-08-16 09:24:13
## 41: 2022-08-16 09:24:13
## 42: 2022-08-16 09:24:13
## 43: 2022-08-16 09:24:13
## 44: 2022-08-16 09:24:13
## 45: 2022-08-16 09:24:13
## 46: 2022-08-16 09:24:13
## 47: 2022-08-16 09:24:13
## 48: 2022-08-16 09:24:13
## 49: 2022-08-16 09:24:13
## 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
## cacheId tagKey tagValue
## 1: f7bee22047b8d0c1 objectName a
## 2: f7bee22047b8d0c1 function rnorm
## 3: f7bee22047b8d0c1 class numeric
## 4: f7bee22047b8d0c1 object.size 1008
## 5: f7bee22047b8d0c1 accessed 2022-08-16 09:24:12
## 6: f7bee22047b8d0c1 inCloud FALSE
## 7: f7bee22047b8d0c1 resultHash
## 8: f7bee22047b8d0c1 elapsedTimeDigest 0.0002450943 secs
## 9: f7bee22047b8d0c1 elapsedTimeFirstRun 2.193451e-05 secs
## 10: f7bee22047b8d0c1 otherFunctions vweave_rmarkdown
## 11: f7bee22047b8d0c1 otherFunctions process_file
## 12: f7bee22047b8d0c1 otherFunctions process_group
## 13: f7bee22047b8d0c1 otherFunctions process_group.block
## 14: f7bee22047b8d0c1 otherFunctions call_block
## 15: f7bee22047b8d0c1 otherFunctions block_exec
## 16: f7bee22047b8d0c1 otherFunctions eng_r
## 17: f7bee22047b8d0c1 otherFunctions in_input_dir
## 18: f7bee22047b8d0c1 otherFunctions in_dir
## 19: f7bee22047b8d0c1 otherFunctions timing_fn
## 20: f7bee22047b8d0c1 otherFunctions handle
## 21: f7bee22047b8d0c1 preDigest n:7eef4eae85fd9229
## 22: f7bee22047b8d0c1 preDigest .FUN:4f604aa46882b368
## 23: f7bee22047b8d0c1 file.size 174
## 24: f7bee22047b8d0c1 accessed 2022-08-16 09:24:13
## 25: f7bee22047b8d0c1 elapsedTimeLoad 0.002529383 secs
## 26: f7bee22047b8d0c1 accessed 2022-08-16 09:24:13
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:12
## 2: 2022-08-16 09:24:12
## 3: 2022-08-16 09:24:12
## 4: 2022-08-16 09:24:12
## 5: 2022-08-16 09:24:12
## 6: 2022-08-16 09:24:12
## 7: 2022-08-16 09:24:12
## 8: 2022-08-16 09:24:12
## 9: 2022-08-16 09:24:12
## 10: 2022-08-16 09:24:12
## 11: 2022-08-16 09:24:12
## 12: 2022-08-16 09:24:12
## 13: 2022-08-16 09:24:12
## 14: 2022-08-16 09:24:12
## 15: 2022-08-16 09:24:12
## 16: 2022-08-16 09:24:12
## 17: 2022-08-16 09:24:12
## 18: 2022-08-16 09:24:12
## 19: 2022-08-16 09:24:12
## 20: 2022-08-16 09:24:12
## 21: 2022-08-16 09:24:12
## 22: 2022-08-16 09:24:12
## 23: 2022-08-16 09:24:12
## 24: 2022-08-16 09:24:13
## 25: 2022-08-16 09:24:13
## 26: 2022-08-16 09:24:13
## 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
showCache(tmpDir) ## 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
# Also, can set a time before caching happens and remove based on this
# --> a useful, simple way to control Cache
<- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a")
ranNumsA <- Sys.time()
startTime Sys.sleep(1)
<- Cache(rnorm, 5, cacheRepo = tmpDir, userTags = "objectName:b")
ranNumsB keepCache(tmpDir, after = startTime, ask = FALSE) # keep only those newer than startTime
## Cache size:
## Total (including Rasters): 256 bytes
## Selected objects (not including Rasters): 256 bytes
## cacheId tagKey tagValue
## 1: 142b4176fe87d51d objectName b
## 2: 142b4176fe87d51d function rnorm
## 3: 142b4176fe87d51d class numeric
## 4: 142b4176fe87d51d object.size 1024
## 5: 142b4176fe87d51d accessed 2022-08-16 09:24:14
## 6: 142b4176fe87d51d inCloud FALSE
## 7: 142b4176fe87d51d resultHash
## 8: 142b4176fe87d51d elapsedTimeDigest 0.0002467632 secs
## 9: 142b4176fe87d51d elapsedTimeFirstRun 2.455711e-05 secs
## 10: 142b4176fe87d51d otherFunctions vweave_rmarkdown
## 11: 142b4176fe87d51d otherFunctions process_file
## 12: 142b4176fe87d51d otherFunctions process_group
## 13: 142b4176fe87d51d otherFunctions process_group.block
## 14: 142b4176fe87d51d otherFunctions call_block
## 15: 142b4176fe87d51d otherFunctions block_exec
## 16: 142b4176fe87d51d otherFunctions eng_r
## 17: 142b4176fe87d51d otherFunctions in_input_dir
## 18: 142b4176fe87d51d otherFunctions in_dir
## 19: 142b4176fe87d51d otherFunctions timing_fn
## 20: 142b4176fe87d51d otherFunctions handle
## 21: 142b4176fe87d51d preDigest n:a4f076b3db622faf
## 22: 142b4176fe87d51d preDigest .FUN:4f604aa46882b368
## 23: 142b4176fe87d51d file.size 182
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:14
## 2: 2022-08-16 09:24:14
## 3: 2022-08-16 09:24:14
## 4: 2022-08-16 09:24:14
## 5: 2022-08-16 09:24:14
## 6: 2022-08-16 09:24:14
## 7: 2022-08-16 09:24:14
## 8: 2022-08-16 09:24:14
## 9: 2022-08-16 09:24:14
## 10: 2022-08-16 09:24:14
## 11: 2022-08-16 09:24:14
## 12: 2022-08-16 09:24:14
## 13: 2022-08-16 09:24:14
## 14: 2022-08-16 09:24:14
## 15: 2022-08-16 09:24:14
## 16: 2022-08-16 09:24:14
## 17: 2022-08-16 09:24:14
## 18: 2022-08-16 09:24:14
## 19: 2022-08-16 09:24:14
## 20: 2022-08-16 09:24:14
## 21: 2022-08-16 09:24:14
## 22: 2022-08-16 09:24:14
## 23: 2022-08-16 09:24:14
## createdDate
clearCache(tmpDir, ask = FALSE)
# default userTags is "and" matching; for "or" matching use |
<- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:a")
ranNumsA <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:b")
ranNumsB
# show all objects (runif and rnorm in this case)
showCache(tmpDir)
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 504 bytes
## cacheId tagKey tagValue
## 1: 3aef38d1fc02aee5 objectName a
## 2: 3aef38d1fc02aee5 function runif
## 3: 3aef38d1fc02aee5 class numeric
## 4: 3aef38d1fc02aee5 object.size 1008
## 5: 3aef38d1fc02aee5 accessed 2022-08-16 09:24:14
## 6: 3aef38d1fc02aee5 inCloud FALSE
## 7: 3aef38d1fc02aee5 resultHash
## 8: 3aef38d1fc02aee5 elapsedTimeDigest 0.0003283024 secs
## 9: 3aef38d1fc02aee5 elapsedTimeFirstRun 2.861023e-05 secs
## 10: 3aef38d1fc02aee5 otherFunctions vweave_rmarkdown
## 11: 3aef38d1fc02aee5 otherFunctions process_file
## 12: 3aef38d1fc02aee5 otherFunctions process_group
## 13: 3aef38d1fc02aee5 otherFunctions process_group.block
## 14: 3aef38d1fc02aee5 otherFunctions call_block
## 15: 3aef38d1fc02aee5 otherFunctions block_exec
## 16: 3aef38d1fc02aee5 otherFunctions eng_r
## 17: 3aef38d1fc02aee5 otherFunctions in_input_dir
## 18: 3aef38d1fc02aee5 otherFunctions in_dir
## 19: 3aef38d1fc02aee5 otherFunctions timing_fn
## 20: 3aef38d1fc02aee5 otherFunctions handle
## 21: 3aef38d1fc02aee5 preDigest n:7eef4eae85fd9229
## 22: 3aef38d1fc02aee5 preDigest .FUN:881ec847b7161f3c
## 23: 3aef38d1fc02aee5 file.size 169
## 24: f7bee22047b8d0c1 objectName b
## 25: f7bee22047b8d0c1 function rnorm
## 26: f7bee22047b8d0c1 class numeric
## 27: f7bee22047b8d0c1 object.size 1008
## 28: f7bee22047b8d0c1 accessed 2022-08-16 09:24:14
## 29: f7bee22047b8d0c1 inCloud FALSE
## 30: f7bee22047b8d0c1 resultHash
## 31: f7bee22047b8d0c1 elapsedTimeDigest 0.0002791882 secs
## 32: f7bee22047b8d0c1 elapsedTimeFirstRun 2.574921e-05 secs
## 33: f7bee22047b8d0c1 otherFunctions vweave_rmarkdown
## 34: f7bee22047b8d0c1 otherFunctions process_file
## 35: f7bee22047b8d0c1 otherFunctions process_group
## 36: f7bee22047b8d0c1 otherFunctions process_group.block
## 37: f7bee22047b8d0c1 otherFunctions call_block
## 38: f7bee22047b8d0c1 otherFunctions block_exec
## 39: f7bee22047b8d0c1 otherFunctions eng_r
## 40: f7bee22047b8d0c1 otherFunctions in_input_dir
## 41: f7bee22047b8d0c1 otherFunctions in_dir
## 42: f7bee22047b8d0c1 otherFunctions timing_fn
## 43: f7bee22047b8d0c1 otherFunctions handle
## 44: f7bee22047b8d0c1 preDigest n:7eef4eae85fd9229
## 45: f7bee22047b8d0c1 preDigest .FUN:4f604aa46882b368
## 46: f7bee22047b8d0c1 file.size 175
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:14
## 2: 2022-08-16 09:24:14
## 3: 2022-08-16 09:24:14
## 4: 2022-08-16 09:24:14
## 5: 2022-08-16 09:24:14
## 6: 2022-08-16 09:24:14
## 7: 2022-08-16 09:24:14
## 8: 2022-08-16 09:24:14
## 9: 2022-08-16 09:24:14
## 10: 2022-08-16 09:24:14
## 11: 2022-08-16 09:24:14
## 12: 2022-08-16 09:24:14
## 13: 2022-08-16 09:24:14
## 14: 2022-08-16 09:24:14
## 15: 2022-08-16 09:24:14
## 16: 2022-08-16 09:24:14
## 17: 2022-08-16 09:24:14
## 18: 2022-08-16 09:24:14
## 19: 2022-08-16 09:24:14
## 20: 2022-08-16 09:24:14
## 21: 2022-08-16 09:24:14
## 22: 2022-08-16 09:24:14
## 23: 2022-08-16 09:24:14
## 24: 2022-08-16 09:24:14
## 25: 2022-08-16 09:24:14
## 26: 2022-08-16 09:24:14
## 27: 2022-08-16 09:24:14
## 28: 2022-08-16 09:24:14
## 29: 2022-08-16 09:24:14
## 30: 2022-08-16 09:24:14
## 31: 2022-08-16 09:24:14
## 32: 2022-08-16 09:24:14
## 33: 2022-08-16 09:24:14
## 34: 2022-08-16 09:24:14
## 35: 2022-08-16 09:24:14
## 36: 2022-08-16 09:24:14
## 37: 2022-08-16 09:24:14
## 38: 2022-08-16 09:24:14
## 39: 2022-08-16 09:24:14
## 40: 2022-08-16 09:24:14
## 41: 2022-08-16 09:24:14
## 42: 2022-08-16 09:24:14
## 43: 2022-08-16 09:24:14
## 44: 2022-08-16 09:24:14
## 45: 2022-08-16 09:24:14
## 46: 2022-08-16 09:24:14
## 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
## 1: 3aef38d1fc02aee5 objectName a
## 2: 3aef38d1fc02aee5 function runif
## 3: 3aef38d1fc02aee5 class numeric
## 4: 3aef38d1fc02aee5 object.size 1008
## 5: 3aef38d1fc02aee5 accessed 2022-08-16 09:24:14
## 6: 3aef38d1fc02aee5 inCloud FALSE
## 7: 3aef38d1fc02aee5 resultHash
## 8: 3aef38d1fc02aee5 elapsedTimeDigest 0.0003283024 secs
## 9: 3aef38d1fc02aee5 elapsedTimeFirstRun 2.861023e-05 secs
## 10: 3aef38d1fc02aee5 otherFunctions vweave_rmarkdown
## 11: 3aef38d1fc02aee5 otherFunctions process_file
## 12: 3aef38d1fc02aee5 otherFunctions process_group
## 13: 3aef38d1fc02aee5 otherFunctions process_group.block
## 14: 3aef38d1fc02aee5 otherFunctions call_block
## 15: 3aef38d1fc02aee5 otherFunctions block_exec
## 16: 3aef38d1fc02aee5 otherFunctions eng_r
## 17: 3aef38d1fc02aee5 otherFunctions in_input_dir
## 18: 3aef38d1fc02aee5 otherFunctions in_dir
## 19: 3aef38d1fc02aee5 otherFunctions timing_fn
## 20: 3aef38d1fc02aee5 otherFunctions handle
## 21: 3aef38d1fc02aee5 preDigest n:7eef4eae85fd9229
## 22: 3aef38d1fc02aee5 preDigest .FUN:881ec847b7161f3c
## 23: 3aef38d1fc02aee5 file.size 169
## 24: f7bee22047b8d0c1 objectName b
## 25: f7bee22047b8d0c1 function rnorm
## 26: f7bee22047b8d0c1 class numeric
## 27: f7bee22047b8d0c1 object.size 1008
## 28: f7bee22047b8d0c1 accessed 2022-08-16 09:24:14
## 29: f7bee22047b8d0c1 inCloud FALSE
## 30: f7bee22047b8d0c1 resultHash
## 31: f7bee22047b8d0c1 elapsedTimeDigest 0.0002791882 secs
## 32: f7bee22047b8d0c1 elapsedTimeFirstRun 2.574921e-05 secs
## 33: f7bee22047b8d0c1 otherFunctions vweave_rmarkdown
## 34: f7bee22047b8d0c1 otherFunctions process_file
## 35: f7bee22047b8d0c1 otherFunctions process_group
## 36: f7bee22047b8d0c1 otherFunctions process_group.block
## 37: f7bee22047b8d0c1 otherFunctions call_block
## 38: f7bee22047b8d0c1 otherFunctions block_exec
## 39: f7bee22047b8d0c1 otherFunctions eng_r
## 40: f7bee22047b8d0c1 otherFunctions in_input_dir
## 41: f7bee22047b8d0c1 otherFunctions in_dir
## 42: f7bee22047b8d0c1 otherFunctions timing_fn
## 43: f7bee22047b8d0c1 otherFunctions handle
## 44: f7bee22047b8d0c1 preDigest n:7eef4eae85fd9229
## 45: f7bee22047b8d0c1 preDigest .FUN:4f604aa46882b368
## 46: f7bee22047b8d0c1 file.size 175
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:14
## 2: 2022-08-16 09:24:14
## 3: 2022-08-16 09:24:14
## 4: 2022-08-16 09:24:14
## 5: 2022-08-16 09:24:14
## 6: 2022-08-16 09:24:14
## 7: 2022-08-16 09:24:14
## 8: 2022-08-16 09:24:14
## 9: 2022-08-16 09:24:14
## 10: 2022-08-16 09:24:14
## 11: 2022-08-16 09:24:14
## 12: 2022-08-16 09:24:14
## 13: 2022-08-16 09:24:14
## 14: 2022-08-16 09:24:14
## 15: 2022-08-16 09:24:14
## 16: 2022-08-16 09:24:14
## 17: 2022-08-16 09:24:14
## 18: 2022-08-16 09:24:14
## 19: 2022-08-16 09:24:14
## 20: 2022-08-16 09:24:14
## 21: 2022-08-16 09:24:14
## 22: 2022-08-16 09:24:14
## 23: 2022-08-16 09:24:14
## 24: 2022-08-16 09:24:14
## 25: 2022-08-16 09:24:14
## 26: 2022-08-16 09:24:14
## 27: 2022-08-16 09:24:14
## 28: 2022-08-16 09:24:14
## 29: 2022-08-16 09:24:14
## 30: 2022-08-16 09:24:14
## 31: 2022-08-16 09:24:14
## 32: 2022-08-16 09:24:14
## 33: 2022-08-16 09:24:14
## 34: 2022-08-16 09:24:14
## 35: 2022-08-16 09:24:14
## 36: 2022-08-16 09:24:14
## 37: 2022-08-16 09:24:14
## 38: 2022-08-16 09:24:14
## 39: 2022-08-16 09:24:14
## 40: 2022-08-16 09:24:14
## 41: 2022-08-16 09:24:14
## 42: 2022-08-16 09:24:14
## 43: 2022-08-16 09:24:14
## 44: 2022-08-16 09:24:14
## 45: 2022-08-16 09:24:14
## 46: 2022-08-16 09:24:14
## 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
## 1: 3aef38d1fc02aee5 objectName a
## 2: 3aef38d1fc02aee5 function runif
## 3: 3aef38d1fc02aee5 class numeric
## 4: 3aef38d1fc02aee5 object.size 1008
## 5: 3aef38d1fc02aee5 accessed 2022-08-16 09:24:14
## 6: 3aef38d1fc02aee5 inCloud FALSE
## 7: 3aef38d1fc02aee5 resultHash
## 8: 3aef38d1fc02aee5 elapsedTimeDigest 0.0003283024 secs
## 9: 3aef38d1fc02aee5 elapsedTimeFirstRun 2.861023e-05 secs
## 10: 3aef38d1fc02aee5 otherFunctions vweave_rmarkdown
## 11: 3aef38d1fc02aee5 otherFunctions process_file
## 12: 3aef38d1fc02aee5 otherFunctions process_group
## 13: 3aef38d1fc02aee5 otherFunctions process_group.block
## 14: 3aef38d1fc02aee5 otherFunctions call_block
## 15: 3aef38d1fc02aee5 otherFunctions block_exec
## 16: 3aef38d1fc02aee5 otherFunctions eng_r
## 17: 3aef38d1fc02aee5 otherFunctions in_input_dir
## 18: 3aef38d1fc02aee5 otherFunctions in_dir
## 19: 3aef38d1fc02aee5 otherFunctions timing_fn
## 20: 3aef38d1fc02aee5 otherFunctions handle
## 21: 3aef38d1fc02aee5 preDigest n:7eef4eae85fd9229
## 22: 3aef38d1fc02aee5 preDigest .FUN:881ec847b7161f3c
## 23: 3aef38d1fc02aee5 file.size 169
## 24: f7bee22047b8d0c1 objectName b
## 25: f7bee22047b8d0c1 function rnorm
## 26: f7bee22047b8d0c1 class numeric
## 27: f7bee22047b8d0c1 object.size 1008
## 28: f7bee22047b8d0c1 accessed 2022-08-16 09:24:14
## 29: f7bee22047b8d0c1 inCloud FALSE
## 30: f7bee22047b8d0c1 resultHash
## 31: f7bee22047b8d0c1 elapsedTimeDigest 0.0002791882 secs
## 32: f7bee22047b8d0c1 elapsedTimeFirstRun 2.574921e-05 secs
## 33: f7bee22047b8d0c1 otherFunctions vweave_rmarkdown
## 34: f7bee22047b8d0c1 otherFunctions process_file
## 35: f7bee22047b8d0c1 otherFunctions process_group
## 36: f7bee22047b8d0c1 otherFunctions process_group.block
## 37: f7bee22047b8d0c1 otherFunctions call_block
## 38: f7bee22047b8d0c1 otherFunctions block_exec
## 39: f7bee22047b8d0c1 otherFunctions eng_r
## 40: f7bee22047b8d0c1 otherFunctions in_input_dir
## 41: f7bee22047b8d0c1 otherFunctions in_dir
## 42: f7bee22047b8d0c1 otherFunctions timing_fn
## 43: f7bee22047b8d0c1 otherFunctions handle
## 44: f7bee22047b8d0c1 preDigest n:7eef4eae85fd9229
## 45: f7bee22047b8d0c1 preDigest .FUN:4f604aa46882b368
## 46: f7bee22047b8d0c1 file.size 175
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:14
## 2: 2022-08-16 09:24:14
## 3: 2022-08-16 09:24:14
## 4: 2022-08-16 09:24:14
## 5: 2022-08-16 09:24:14
## 6: 2022-08-16 09:24:14
## 7: 2022-08-16 09:24:14
## 8: 2022-08-16 09:24:14
## 9: 2022-08-16 09:24:14
## 10: 2022-08-16 09:24:14
## 11: 2022-08-16 09:24:14
## 12: 2022-08-16 09:24:14
## 13: 2022-08-16 09:24:14
## 14: 2022-08-16 09:24:14
## 15: 2022-08-16 09:24:14
## 16: 2022-08-16 09:24:14
## 17: 2022-08-16 09:24:14
## 18: 2022-08-16 09:24:14
## 19: 2022-08-16 09:24:14
## 20: 2022-08-16 09:24:14
## 21: 2022-08-16 09:24:14
## 22: 2022-08-16 09:24:14
## 23: 2022-08-16 09:24:14
## 24: 2022-08-16 09:24:14
## 25: 2022-08-16 09:24:14
## 26: 2022-08-16 09:24:14
## 27: 2022-08-16 09:24:14
## 28: 2022-08-16 09:24:14
## 29: 2022-08-16 09:24:14
## 30: 2022-08-16 09:24:14
## 31: 2022-08-16 09:24:14
## 32: 2022-08-16 09:24:14
## 33: 2022-08-16 09:24:14
## 34: 2022-08-16 09:24:14
## 35: 2022-08-16 09:24:14
## 36: 2022-08-16 09:24:14
## 37: 2022-08-16 09:24:14
## 38: 2022-08-16 09:24:14
## 39: 2022-08-16 09:24:14
## 40: 2022-08-16 09:24:14
## 41: 2022-08-16 09:24:14
## 42: 2022-08-16 09:24:14
## 43: 2022-08-16 09:24:14
## 44: 2022-08-16 09:24:14
## 45: 2022-08-16 09:24:14
## 46: 2022-08-16 09:24:14
## createdDate
clearCache(tmpDir, ask = FALSE)
<- raster(extent(0, 5, 0, 5), res = 1,
ras vals = sample(1:5, replace = TRUE, size = 25),
crs = "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84")
# A slow operation, like GIS operation
<- suppressWarnings(
notCached # project raster generates warnings when run non-interactively
projectRaster(ras, crs = crs(ras), res = 5, cacheRepo = tmpDir)
)
<- suppressWarnings(
cached # project raster generates warnings when run non-interactively
# using quote works also
Cache(projectRaster, ras, crs = crs(ras), res = 5, cacheRepo = tmpDir)
)
# second time is much faster
<- suppressWarnings(
reRun # project raster generates warnings when run non-interactively
Cache(projectRaster, ras, crs = crs(ras), res = 5, cacheRepo = tmpDir)
)
## ...(Object to retrieve (2227f050d556cd94.rds))
## loaded cached result from previous projectRaster call
# recovered cached version is same as non-cached version
all.equal(notCached, reRun) ## TRUE
## [1] TRUE
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
<- function(mean) {
inner <- 1
d Cache(rnorm, n = 3, mean = mean)
}<- function(n) {
outer Cache(inner, 0.1, cacheRepo = tmpdir2)
}
# make 2 different cache paths
<- file.path(tempdir(), "first")
tmpdir1 <- file.path(tempdir(), "second")
tmpdir2
# Run the Cache ... notOlderThan propagates to all 3 Cache calls,
# but cacheRepo is tmpdir1 in top level Cache and all nested
# Cache calls, unless individually overridden ... here inner
# uses tmpdir2 repository
Cache(outer, n = 2, cacheRepo = tmpdir1, notOlderThan = Sys.time())
## [1] -0.08844588 0.91316783 0.60450152
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## attr(,"tags")
## [1] "cacheId:a7af5367c13aba8f"
## attr(,"call")
## [1] ""
showCache(tmpdir1) # 2 function calls
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 504 bytes
## cacheId tagKey tagValue
## 1: 4ac2b7c0f42d1e46 function rnorm
## 2: 4ac2b7c0f42d1e46 class numeric
## 3: 4ac2b7c0f42d1e46 object.size 1008
## 4: 4ac2b7c0f42d1e46 accessed 2022-08-16 09:24:14
## 5: 4ac2b7c0f42d1e46 inCloud FALSE
## 6: 4ac2b7c0f42d1e46 resultHash
## 7: 4ac2b7c0f42d1e46 elapsedTimeDigest 0.0002577305 secs
## 8: 4ac2b7c0f42d1e46 elapsedTimeFirstRun 2.193451e-05 secs
## 9: 4ac2b7c0f42d1e46 otherFunctions vweave_rmarkdown
## 10: 4ac2b7c0f42d1e46 otherFunctions process_file
## 11: 4ac2b7c0f42d1e46 otherFunctions process_group
## 12: 4ac2b7c0f42d1e46 otherFunctions process_group.block
## 13: 4ac2b7c0f42d1e46 otherFunctions call_block
## 14: 4ac2b7c0f42d1e46 otherFunctions block_exec
## 15: 4ac2b7c0f42d1e46 otherFunctions eng_r
## 16: 4ac2b7c0f42d1e46 otherFunctions in_input_dir
## 17: 4ac2b7c0f42d1e46 otherFunctions in_dir
## 18: 4ac2b7c0f42d1e46 otherFunctions timing_fn
## 19: 4ac2b7c0f42d1e46 otherFunctions handle
## 20: 4ac2b7c0f42d1e46 preDigest n:7f12988bd88a0fb8
## 21: 4ac2b7c0f42d1e46 preDigest mean:22413394efd9f6a3
## 22: 4ac2b7c0f42d1e46 preDigest .FUN:4f604aa46882b368
## 23: 4ac2b7c0f42d1e46 file.size 165
## 24: a7af5367c13aba8f function outer
## 25: a7af5367c13aba8f class numeric
## 26: a7af5367c13aba8f object.size 1008
## 27: a7af5367c13aba8f accessed 2022-08-16 09:24:14
## 28: a7af5367c13aba8f inCloud FALSE
## 29: a7af5367c13aba8f resultHash
## 30: a7af5367c13aba8f elapsedTimeDigest 0.0002706051 secs
## 31: a7af5367c13aba8f elapsedTimeFirstRun 0.03126216 secs
## 32: a7af5367c13aba8f otherFunctions vweave_rmarkdown
## 33: a7af5367c13aba8f otherFunctions process_file
## 34: a7af5367c13aba8f otherFunctions process_group
## 35: a7af5367c13aba8f otherFunctions process_group.block
## 36: a7af5367c13aba8f otherFunctions call_block
## 37: a7af5367c13aba8f otherFunctions block_exec
## 38: a7af5367c13aba8f otherFunctions eng_r
## 39: a7af5367c13aba8f otherFunctions in_input_dir
## 40: a7af5367c13aba8f otherFunctions in_dir
## 41: a7af5367c13aba8f otherFunctions timing_fn
## 42: a7af5367c13aba8f otherFunctions handle
## 43: a7af5367c13aba8f preDigest n:82dc709f2b91918a
## 44: a7af5367c13aba8f preDigest .FUN:892a6afc47a63a90
## 45: a7af5367c13aba8f file.size 165
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:14
## 2: 2022-08-16 09:24:14
## 3: 2022-08-16 09:24:14
## 4: 2022-08-16 09:24:14
## 5: 2022-08-16 09:24:14
## 6: 2022-08-16 09:24:14
## 7: 2022-08-16 09:24:14
## 8: 2022-08-16 09:24:14
## 9: 2022-08-16 09:24:14
## 10: 2022-08-16 09:24:14
## 11: 2022-08-16 09:24:14
## 12: 2022-08-16 09:24:14
## 13: 2022-08-16 09:24:14
## 14: 2022-08-16 09:24:14
## 15: 2022-08-16 09:24:14
## 16: 2022-08-16 09:24:14
## 17: 2022-08-16 09:24:14
## 18: 2022-08-16 09:24:14
## 19: 2022-08-16 09:24:14
## 20: 2022-08-16 09:24:14
## 21: 2022-08-16 09:24:14
## 22: 2022-08-16 09:24:14
## 23: 2022-08-16 09:24:14
## 24: 2022-08-16 09:24:14
## 25: 2022-08-16 09:24:14
## 26: 2022-08-16 09:24:14
## 27: 2022-08-16 09:24:14
## 28: 2022-08-16 09:24:14
## 29: 2022-08-16 09:24:14
## 30: 2022-08-16 09:24:14
## 31: 2022-08-16 09:24:14
## 32: 2022-08-16 09:24:14
## 33: 2022-08-16 09:24:14
## 34: 2022-08-16 09:24:14
## 35: 2022-08-16 09:24:14
## 36: 2022-08-16 09:24:14
## 37: 2022-08-16 09:24:14
## 38: 2022-08-16 09:24:14
## 39: 2022-08-16 09:24:14
## 40: 2022-08-16 09:24:14
## 41: 2022-08-16 09:24:14
## 42: 2022-08-16 09:24:14
## 43: 2022-08-16 09:24:14
## 44: 2022-08-16 09:24:14
## 45: 2022-08-16 09:24:14
## createdDate
showCache(tmpdir2) # 1 function call
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## cacheId tagKey tagValue
## 1: 33ceb4fb525fd08f function inner
## 2: 33ceb4fb525fd08f class numeric
## 3: 33ceb4fb525fd08f object.size 1008
## 4: 33ceb4fb525fd08f accessed 2022-08-16 09:24:14
## 5: 33ceb4fb525fd08f inCloud FALSE
## 6: 33ceb4fb525fd08f resultHash
## 7: 33ceb4fb525fd08f elapsedTimeDigest 0.0002763271 secs
## 8: 33ceb4fb525fd08f elapsedTimeFirstRun 0.01143837 secs
## 9: 33ceb4fb525fd08f otherFunctions vweave_rmarkdown
## 10: 33ceb4fb525fd08f otherFunctions process_file
## 11: 33ceb4fb525fd08f otherFunctions process_group
## 12: 33ceb4fb525fd08f otherFunctions process_group.block
## 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 timing_fn
## 19: 33ceb4fb525fd08f otherFunctions handle
## 20: 33ceb4fb525fd08f preDigest mean:22413394efd9f6a3
## 21: 33ceb4fb525fd08f preDigest .FUN:87e2c30917a34d25
## 22: 33ceb4fb525fd08f file.size 165
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:14
## 2: 2022-08-16 09:24:14
## 3: 2022-08-16 09:24:14
## 4: 2022-08-16 09:24:14
## 5: 2022-08-16 09:24:14
## 6: 2022-08-16 09:24:14
## 7: 2022-08-16 09:24:14
## 8: 2022-08-16 09:24:14
## 9: 2022-08-16 09:24:14
## 10: 2022-08-16 09:24:14
## 11: 2022-08-16 09:24:14
## 12: 2022-08-16 09:24:14
## 13: 2022-08-16 09:24:14
## 14: 2022-08-16 09:24:14
## 15: 2022-08-16 09:24:14
## 16: 2022-08-16 09:24:14
## 17: 2022-08-16 09:24:14
## 18: 2022-08-16 09:24:14
## 19: 2022-08-16 09:24:14
## 20: 2022-08-16 09:24:14
## 21: 2022-08-16 09:24:14
## 22: 2022-08-16 09:24:14
## 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 <- function(mean) {
inner <- 1
d Cache(rnorm, n = 3, mean = mean, notOlderThan = Sys.time() - 1e5, userTags = innerTag)
}<- function(n) {
outer Cache(inner, 0.1)
}<- Cache(outer, n = 2, cacheRepo = tmpdir1, userTags = outerTag)
aa showCache(tmpdir1) # rnorm function has outerTag and innerTag, inner and outer only have outerTag
## Cache size:
## Total (including Rasters): 756 bytes
## Selected objects (not including Rasters): 756 bytes
## cacheId tagKey tagValue
## 1: 4ac2b7c0f42d1e46 innerTag innerTag
## 2: 4ac2b7c0f42d1e46 outerTag outerTag
## 3: 4ac2b7c0f42d1e46 function rnorm
## 4: 4ac2b7c0f42d1e46 class numeric
## 5: 4ac2b7c0f42d1e46 object.size 1008
## 6: 4ac2b7c0f42d1e46 accessed 2022-08-16 09:24:14
## 7: 4ac2b7c0f42d1e46 inCloud FALSE
## 8: 4ac2b7c0f42d1e46 resultHash
## 9: 4ac2b7c0f42d1e46 elapsedTimeDigest 0.0002787113 secs
## 10: 4ac2b7c0f42d1e46 elapsedTimeFirstRun 2.026558e-05 secs
## 11: 4ac2b7c0f42d1e46 otherFunctions vweave_rmarkdown
## 12: 4ac2b7c0f42d1e46 otherFunctions process_file
## 13: 4ac2b7c0f42d1e46 otherFunctions process_group
## 14: 4ac2b7c0f42d1e46 otherFunctions process_group.block
## 15: 4ac2b7c0f42d1e46 otherFunctions call_block
## 16: 4ac2b7c0f42d1e46 otherFunctions block_exec
## 17: 4ac2b7c0f42d1e46 otherFunctions eng_r
## 18: 4ac2b7c0f42d1e46 otherFunctions in_input_dir
## 19: 4ac2b7c0f42d1e46 otherFunctions in_dir
## 20: 4ac2b7c0f42d1e46 otherFunctions timing_fn
## 21: 4ac2b7c0f42d1e46 otherFunctions handle
## 22: 4ac2b7c0f42d1e46 preDigest n:7f12988bd88a0fb8
## 23: 4ac2b7c0f42d1e46 preDigest mean:22413394efd9f6a3
## 24: 4ac2b7c0f42d1e46 preDigest .FUN:4f604aa46882b368
## 25: 4ac2b7c0f42d1e46 file.size 165
## 26: b06af03d5a73dc7d outerTag outerTag
## 27: b06af03d5a73dc7d function inner
## 28: b06af03d5a73dc7d class numeric
## 29: b06af03d5a73dc7d object.size 1008
## 30: b06af03d5a73dc7d accessed 2022-08-16 09:24:14
## 31: b06af03d5a73dc7d inCloud FALSE
## 32: b06af03d5a73dc7d resultHash
## 33: b06af03d5a73dc7d elapsedTimeDigest 0.0002954006 secs
## 34: b06af03d5a73dc7d elapsedTimeFirstRun 0.01262546 secs
## 35: b06af03d5a73dc7d otherFunctions vweave_rmarkdown
## 36: b06af03d5a73dc7d otherFunctions process_file
## 37: b06af03d5a73dc7d otherFunctions process_group
## 38: b06af03d5a73dc7d otherFunctions process_group.block
## 39: b06af03d5a73dc7d otherFunctions call_block
## 40: b06af03d5a73dc7d otherFunctions block_exec
## 41: b06af03d5a73dc7d otherFunctions eng_r
## 42: b06af03d5a73dc7d otherFunctions in_input_dir
## 43: b06af03d5a73dc7d otherFunctions in_dir
## 44: b06af03d5a73dc7d otherFunctions timing_fn
## 45: b06af03d5a73dc7d otherFunctions handle
## 46: b06af03d5a73dc7d preDigest mean:22413394efd9f6a3
## 47: b06af03d5a73dc7d preDigest .FUN:7ad10bc1ae528d8c
## 48: b06af03d5a73dc7d file.size 165
## 49: 88a34e1d033329e5 outerTag outerTag
## 50: 88a34e1d033329e5 function outer
## 51: 88a34e1d033329e5 class numeric
## 52: 88a34e1d033329e5 object.size 1008
## 53: 88a34e1d033329e5 accessed 2022-08-16 09:24:14
## 54: 88a34e1d033329e5 inCloud FALSE
## 55: 88a34e1d033329e5 resultHash
## 56: 88a34e1d033329e5 elapsedTimeDigest 0.0002644062 secs
## 57: 88a34e1d033329e5 elapsedTimeFirstRun 0.02417874 secs
## 58: 88a34e1d033329e5 otherFunctions vweave_rmarkdown
## 59: 88a34e1d033329e5 otherFunctions process_file
## 60: 88a34e1d033329e5 otherFunctions process_group
## 61: 88a34e1d033329e5 otherFunctions process_group.block
## 62: 88a34e1d033329e5 otherFunctions call_block
## 63: 88a34e1d033329e5 otherFunctions block_exec
## 64: 88a34e1d033329e5 otherFunctions eng_r
## 65: 88a34e1d033329e5 otherFunctions in_input_dir
## 66: 88a34e1d033329e5 otherFunctions in_dir
## 67: 88a34e1d033329e5 otherFunctions timing_fn
## 68: 88a34e1d033329e5 otherFunctions handle
## 69: 88a34e1d033329e5 preDigest n:82dc709f2b91918a
## 70: 88a34e1d033329e5 preDigest .FUN:5f06fb5fbffe9e3b
## 71: 88a34e1d033329e5 file.size 165
## cacheId tagKey tagValue
## createdDate
## 1: 2022-08-16 09:24:14
## 2: 2022-08-16 09:24:14
## 3: 2022-08-16 09:24:14
## 4: 2022-08-16 09:24:14
## 5: 2022-08-16 09:24:14
## 6: 2022-08-16 09:24:14
## 7: 2022-08-16 09:24:14
## 8: 2022-08-16 09:24:14
## 9: 2022-08-16 09:24:14
## 10: 2022-08-16 09:24:14
## 11: 2022-08-16 09:24:14
## 12: 2022-08-16 09:24:14
## 13: 2022-08-16 09:24:14
## 14: 2022-08-16 09:24:14
## 15: 2022-08-16 09:24:14
## 16: 2022-08-16 09:24:14
## 17: 2022-08-16 09:24:14
## 18: 2022-08-16 09:24:14
## 19: 2022-08-16 09:24:14
## 20: 2022-08-16 09:24:14
## 21: 2022-08-16 09:24:14
## 22: 2022-08-16 09:24:14
## 23: 2022-08-16 09:24:14
## 24: 2022-08-16 09:24:14
## 25: 2022-08-16 09:24:14
## 26: 2022-08-16 09:24:14
## 27: 2022-08-16 09:24:14
## 28: 2022-08-16 09:24:14
## 29: 2022-08-16 09:24:14
## 30: 2022-08-16 09:24:14
## 31: 2022-08-16 09:24:14
## 32: 2022-08-16 09:24:14
## 33: 2022-08-16 09:24:14
## 34: 2022-08-16 09:24:14
## 35: 2022-08-16 09:24:14
## 36: 2022-08-16 09:24:14
## 37: 2022-08-16 09:24:14
## 38: 2022-08-16 09:24:14
## 39: 2022-08-16 09:24:14
## 40: 2022-08-16 09:24:14
## 41: 2022-08-16 09:24:14
## 42: 2022-08-16 09:24:14
## 43: 2022-08-16 09:24:14
## 44: 2022-08-16 09:24:14
## 45: 2022-08-16 09:24:14
## 46: 2022-08-16 09:24:14
## 47: 2022-08-16 09:24:14
## 48: 2022-08-16 09:24:14
## 49: 2022-08-16 09:24:14
## 50: 2022-08-16 09:24:14
## 51: 2022-08-16 09:24:14
## 52: 2022-08-16 09:24:14
## 53: 2022-08-16 09:24:14
## 54: 2022-08-16 09:24:14
## 55: 2022-08-16 09:24:14
## 56: 2022-08-16 09:24:14
## 57: 2022-08-16 09:24:14
## 58: 2022-08-16 09:24:14
## 59: 2022-08-16 09:24:14
## 60: 2022-08-16 09:24:14
## 61: 2022-08-16 09:24:14
## 62: 2022-08-16 09:24:14
## 63: 2022-08-16 09:24:14
## 64: 2022-08-16 09:24:14
## 65: 2022-08-16 09:24:14
## 66: 2022-08-16 09:24:14
## 67: 2022-08-16 09:24:14
## 68: 2022-08-16 09:24:14
## 69: 2022-08-16 09:24:14
## 70: 2022-08-16 09:24:14
## 71: 2022-08-16 09:24:14
## 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"
.
### cacheId
set.seed(1)
Cache(rnorm, 1, cacheRepo = tmpdir1)
## [1] -0.6264538
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## attr(,"tags")
## [1] "cacheId:7072c305d8c69df0"
## attr(,"call")
## [1] ""
# manually look at output attribute which shows cacheId: 7072c305d8c69df0
Cache(rnorm, 1, cacheRepo = tmpdir1, cacheId = "7072c305d8c69df0") # same value
## cacheId is same as calculated hash
## ...(Object to retrieve (7072c305d8c69df0.rds))
## loaded cached result from previous rnorm call
## [1] -0.6264538
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] FALSE
##
## attr(,"tags")
## [1] "cacheId:7072c305d8c69df0"
## attr(,"call")
## [1] ""
# override even with different inputs:
Cache(rnorm, 2, cacheRepo = tmpdir1, cacheId = "7072c305d8c69df0")
## cacheId is not same as calculated hash. Manually searching for cacheId:7072c305d8c69df0
## ...(Object to retrieve (7072c305d8c69df0.rds))
## loaded cached result from previous rnorm call
## [1] -0.6264538
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] FALSE
##
## attr(,"tags")
## [1] "cacheId:7072c305d8c69df0"
## 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
<- unique(showCache(tmpDir, userTags = "projectRaster")$cacheId) mapHash
## Cache size:
## Total (including Rasters): 3.4 Kb
## Selected objects (not including Rasters): 3.4 Kb
<- loadFromCache(mapHash[1], cachePath = tmpDir)
map plot(map)
## cleanup
unlink(dirname(tmpDir), recursive = TRUE)
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.