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
uses 2 key the archivist
functions saveToLocalRepo
and loadFromLocalRepo
, but does not use archivist::cache
. Similar to archivist::cache
, there is some reliance on digest::digest
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.
## Loading required package: sp
## Registered S3 method overwritten by 'R.oo':
## method from
## throw.default R.methodsS3
## [1] "/tmp/RtmpxsEeYG/reproducible_examples/Cache"
ras <- raster(extent(0, 1000, 0, 1000), vals = 1:1e6, res = 1)
crs(ras) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"
newCRS <- "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# No Cache
system.time(map1 <- projectRaster(ras, crs = newCRS))
## user system elapsed
## 2.599 0.198 2.802
# With Cache -- a little slower the first time because saving to disk
system.time(map1 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir,
notOlderThan = Sys.time()))
## user system elapsed
## 2.603 0.207 2.883
# vastly faster the second time
system.time(map2 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir))
## ...(Object to retrieve is large: 6.5 Mb)
## loading cached result from previous projectRaster call, adding to memoised copy
## user system elapsed
## 0.266 0.001 0.267
# even faster the third time
system.time(map3 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir))
## ...(Object to retrieve is large: 6.5 Mb)
## loading memoised result from previous projectRaster call.
## user system elapsed
## 0.023 0.003 0.025
## [1] TRUE
## [1] TRUE
##
## 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
ranNumsA <- Cache(rnorm, 10, 16, cacheRepo = tmpDir)
# All same
ranNumsB <- Cache(rnorm, 10, 16, cacheRepo = tmpDir) # recovers cached copy
## loading cached result from previous rnorm call, adding to memoised copy
## loading memoised result from previous 'rnorm' pipe sequence call.
## loading memoised result from previous rnorm call.
ranNumsA <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a")
ranNumsB <- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:b")
# access it again, from Cache
ranNumsA <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a")
## loading cached result from previous rnorm call, adding to memoised copy
## 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): 504 bytes
## Selected objects (not including Rasters): 504 bytes
# inverse join with 2 data.tables ... using: a[!b]
# i.e., return all of wholeCache that was not recently accessed
toRemove <- unique(wholeCache[!onlyRecentlyAccessed], by = "artifact")$artifact
clearCache(tmpDir, toRemove, ask = FALSE) # remove ones not recently accessed
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 504 bytes
## Cache size:
## Total (including Rasters): 0 bytes
## Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows and 3 cols): md5hash,name,createdDate
ranNumsA <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a")
ranNumsB <- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:b")
# 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
## artifact tagKey
## 1: 92dfc041c09e3480875a66d26b479943 format
## 2: 92dfc041c09e3480875a66d26b479943 name
## 3: 92dfc041c09e3480875a66d26b479943 class
## 4: 92dfc041c09e3480875a66d26b479943 date
## 5: 92dfc041c09e3480875a66d26b479943 cacheId
## 6: 92dfc041c09e3480875a66d26b479943 objectName
## 7: 92dfc041c09e3480875a66d26b479943 function
## 8: 92dfc041c09e3480875a66d26b479943 object.size
## 9: 92dfc041c09e3480875a66d26b479943 accessed
## 10: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 11: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 12: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 13: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 14: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 15: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 16: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 17: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 18: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 19: 92dfc041c09e3480875a66d26b479943 otherFunctions
## 20: 92dfc041c09e3480875a66d26b479943 preDigest
## 21: 92dfc041c09e3480875a66d26b479943 preDigest
## 22: deb774406e1a36c71f4814eec81c9ee7 format
## 23: deb774406e1a36c71f4814eec81c9ee7 name
## 24: deb774406e1a36c71f4814eec81c9ee7 class
## 25: deb774406e1a36c71f4814eec81c9ee7 date
## 26: deb774406e1a36c71f4814eec81c9ee7 cacheId
## 27: deb774406e1a36c71f4814eec81c9ee7 objectName
## 28: deb774406e1a36c71f4814eec81c9ee7 function
## 29: deb774406e1a36c71f4814eec81c9ee7 object.size
## 30: deb774406e1a36c71f4814eec81c9ee7 accessed
## 31: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 32: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 33: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 34: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 35: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 36: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 37: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 38: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 39: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 40: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 41: deb774406e1a36c71f4814eec81c9ee7 preDigest
## 42: deb774406e1a36c71f4814eec81c9ee7 preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2019-09-11 13:06:12
## 2: 92dfc041c09e3480875a66d26b479943 2019-09-11 13:06:12
## 3: numeric 2019-09-11 13:06:12
## 4: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 5: 3aef38d1fc02aee5 2019-09-11 13:06:12
## 6: b 2019-09-11 13:06:12
## 7: runif 2019-09-11 13:06:12
## 8: 1008 2019-09-11 13:06:12
## 9: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 10: vweave_rmarkdown 2019-09-11 13:06:12
## 11: process_file 2019-09-11 13:06:12
## 12: process_group 2019-09-11 13:06:12
## 13: process_group.block 2019-09-11 13:06:12
## 14: call_block 2019-09-11 13:06:12
## 15: block_exec 2019-09-11 13:06:12
## 16: in_dir 2019-09-11 13:06:12
## 17: timing_fn 2019-09-11 13:06:12
## 18: handle 2019-09-11 13:06:12
## 19: withVisible 2019-09-11 13:06:12
## 20: n:7eef4eae85fd9229 2019-09-11 13:06:12
## 21: .FUN:881ec847b7161f3c 2019-09-11 13:06:12
## 22: rda 2019-09-11 13:06:12
## 23: deb774406e1a36c71f4814eec81c9ee7 2019-09-11 13:06:12
## 24: numeric 2019-09-11 13:06:12
## 25: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 26: f7bee22047b8d0c1 2019-09-11 13:06:12
## 27: a 2019-09-11 13:06:12
## 28: rnorm 2019-09-11 13:06:12
## 29: 1008 2019-09-11 13:06:12
## 30: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 31: vweave_rmarkdown 2019-09-11 13:06:12
## 32: process_file 2019-09-11 13:06:12
## 33: process_group 2019-09-11 13:06:12
## 34: process_group.block 2019-09-11 13:06:12
## 35: call_block 2019-09-11 13:06:12
## 36: block_exec 2019-09-11 13:06:12
## 37: in_dir 2019-09-11 13:06:12
## 38: timing_fn 2019-09-11 13:06:12
## 39: handle 2019-09-11 13:06:12
## 40: withVisible 2019-09-11 13:06:12
## 41: n:7eef4eae85fd9229 2019-09-11 13:06:12
## 42: .FUN:4f604aa46882b368 2019-09-11 13:06:12
## tagValue createdDate
# Keep all Cache items created with an rnorm() call
keepCache(tmpDir, userTags = "rnorm", ask = FALSE)
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 252 bytes
## artifact tagKey
## 1: deb774406e1a36c71f4814eec81c9ee7 format
## 2: deb774406e1a36c71f4814eec81c9ee7 name
## 3: deb774406e1a36c71f4814eec81c9ee7 class
## 4: deb774406e1a36c71f4814eec81c9ee7 date
## 5: deb774406e1a36c71f4814eec81c9ee7 cacheId
## 6: deb774406e1a36c71f4814eec81c9ee7 objectName
## 7: deb774406e1a36c71f4814eec81c9ee7 function
## 8: deb774406e1a36c71f4814eec81c9ee7 object.size
## 9: deb774406e1a36c71f4814eec81c9ee7 accessed
## 10: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 11: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 12: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 13: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 14: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 15: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 16: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 17: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 18: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 19: deb774406e1a36c71f4814eec81c9ee7 otherFunctions
## 20: deb774406e1a36c71f4814eec81c9ee7 preDigest
## 21: deb774406e1a36c71f4814eec81c9ee7 preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2019-09-11 13:06:12
## 2: deb774406e1a36c71f4814eec81c9ee7 2019-09-11 13:06:12
## 3: numeric 2019-09-11 13:06:12
## 4: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 5: f7bee22047b8d0c1 2019-09-11 13:06:12
## 6: a 2019-09-11 13:06:12
## 7: rnorm 2019-09-11 13:06:12
## 8: 1008 2019-09-11 13:06:12
## 9: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 10: vweave_rmarkdown 2019-09-11 13:06:12
## 11: process_file 2019-09-11 13:06:12
## 12: process_group 2019-09-11 13:06:12
## 13: process_group.block 2019-09-11 13:06:12
## 14: call_block 2019-09-11 13:06:12
## 15: block_exec 2019-09-11 13:06:12
## 16: in_dir 2019-09-11 13:06:12
## 17: timing_fn 2019-09-11 13:06:12
## 18: handle 2019-09-11 13:06:12
## 19: withVisible 2019-09-11 13:06:12
## 20: n:7eef4eae85fd9229 2019-09-11 13:06:12
## 21: .FUN:4f604aa46882b368 2019-09-11 13:06:12
## tagValue 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 3 cols): md5hash,name,createdDate
# default userTags is "and" matching; for "or" matching use |
ranNumsA <- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:a")
ranNumsB <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:b")
# 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
## artifact tagKey
## 1: a4d11c23c568a3e99003c55d15a16781 format
## 2: a4d11c23c568a3e99003c55d15a16781 name
## 3: a4d11c23c568a3e99003c55d15a16781 class
## 4: a4d11c23c568a3e99003c55d15a16781 date
## 5: a4d11c23c568a3e99003c55d15a16781 cacheId
## 6: a4d11c23c568a3e99003c55d15a16781 objectName
## 7: a4d11c23c568a3e99003c55d15a16781 function
## 8: a4d11c23c568a3e99003c55d15a16781 object.size
## 9: a4d11c23c568a3e99003c55d15a16781 accessed
## 10: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 11: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 12: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 13: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 14: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 15: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 16: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 17: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 18: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 19: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 20: a4d11c23c568a3e99003c55d15a16781 preDigest
## 21: a4d11c23c568a3e99003c55d15a16781 preDigest
## 22: ed11e3916d1da1036dca15cba81b337d format
## 23: ed11e3916d1da1036dca15cba81b337d name
## 24: ed11e3916d1da1036dca15cba81b337d class
## 25: ed11e3916d1da1036dca15cba81b337d date
## 26: ed11e3916d1da1036dca15cba81b337d cacheId
## 27: ed11e3916d1da1036dca15cba81b337d objectName
## 28: ed11e3916d1da1036dca15cba81b337d function
## 29: ed11e3916d1da1036dca15cba81b337d object.size
## 30: ed11e3916d1da1036dca15cba81b337d accessed
## 31: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 32: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 33: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 34: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 35: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 36: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 37: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 38: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 39: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 40: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 41: ed11e3916d1da1036dca15cba81b337d preDigest
## 42: ed11e3916d1da1036dca15cba81b337d preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2019-09-11 13:06:12
## 2: a4d11c23c568a3e99003c55d15a16781 2019-09-11 13:06:12
## 3: numeric 2019-09-11 13:06:12
## 4: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 5: 3aef38d1fc02aee5 2019-09-11 13:06:12
## 6: a 2019-09-11 13:06:12
## 7: runif 2019-09-11 13:06:12
## 8: 1008 2019-09-11 13:06:12
## 9: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 10: vweave_rmarkdown 2019-09-11 13:06:12
## 11: process_file 2019-09-11 13:06:12
## 12: process_group 2019-09-11 13:06:12
## 13: process_group.block 2019-09-11 13:06:12
## 14: call_block 2019-09-11 13:06:12
## 15: block_exec 2019-09-11 13:06:12
## 16: in_dir 2019-09-11 13:06:12
## 17: timing_fn 2019-09-11 13:06:12
## 18: handle 2019-09-11 13:06:12
## 19: withVisible 2019-09-11 13:06:12
## 20: n:7eef4eae85fd9229 2019-09-11 13:06:12
## 21: .FUN:881ec847b7161f3c 2019-09-11 13:06:12
## 22: rda 2019-09-11 13:06:12
## 23: ed11e3916d1da1036dca15cba81b337d 2019-09-11 13:06:12
## 24: numeric 2019-09-11 13:06:12
## 25: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 26: f7bee22047b8d0c1 2019-09-11 13:06:12
## 27: b 2019-09-11 13:06:12
## 28: rnorm 2019-09-11 13:06:12
## 29: 1008 2019-09-11 13:06:12
## 30: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 31: vweave_rmarkdown 2019-09-11 13:06:12
## 32: process_file 2019-09-11 13:06:12
## 33: process_group 2019-09-11 13:06:12
## 34: process_group.block 2019-09-11 13:06:12
## 35: call_block 2019-09-11 13:06:12
## 36: block_exec 2019-09-11 13:06:12
## 37: in_dir 2019-09-11 13:06:12
## 38: timing_fn 2019-09-11 13:06:12
## 39: handle 2019-09-11 13:06:12
## 40: withVisible 2019-09-11 13:06:12
## 41: n:7eef4eae85fd9229 2019-09-11 13:06:12
## 42: .FUN:4f604aa46882b368 2019-09-11 13:06:12
## tagValue 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): 504 bytes
## Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows and 4 cols): artifact,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
## artifact tagKey
## 1: a4d11c23c568a3e99003c55d15a16781 format
## 2: a4d11c23c568a3e99003c55d15a16781 name
## 3: a4d11c23c568a3e99003c55d15a16781 class
## 4: a4d11c23c568a3e99003c55d15a16781 date
## 5: a4d11c23c568a3e99003c55d15a16781 cacheId
## 6: a4d11c23c568a3e99003c55d15a16781 objectName
## 7: a4d11c23c568a3e99003c55d15a16781 function
## 8: a4d11c23c568a3e99003c55d15a16781 object.size
## 9: a4d11c23c568a3e99003c55d15a16781 accessed
## 10: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 11: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 12: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 13: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 14: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 15: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 16: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 17: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 18: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 19: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 20: a4d11c23c568a3e99003c55d15a16781 preDigest
## 21: a4d11c23c568a3e99003c55d15a16781 preDigest
## 22: ed11e3916d1da1036dca15cba81b337d format
## 23: ed11e3916d1da1036dca15cba81b337d name
## 24: ed11e3916d1da1036dca15cba81b337d class
## 25: ed11e3916d1da1036dca15cba81b337d date
## 26: ed11e3916d1da1036dca15cba81b337d cacheId
## 27: ed11e3916d1da1036dca15cba81b337d objectName
## 28: ed11e3916d1da1036dca15cba81b337d function
## 29: ed11e3916d1da1036dca15cba81b337d object.size
## 30: ed11e3916d1da1036dca15cba81b337d accessed
## 31: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 32: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 33: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 34: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 35: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 36: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 37: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 38: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 39: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 40: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 41: ed11e3916d1da1036dca15cba81b337d preDigest
## 42: ed11e3916d1da1036dca15cba81b337d preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2019-09-11 13:06:12
## 2: a4d11c23c568a3e99003c55d15a16781 2019-09-11 13:06:12
## 3: numeric 2019-09-11 13:06:12
## 4: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 5: 3aef38d1fc02aee5 2019-09-11 13:06:12
## 6: a 2019-09-11 13:06:12
## 7: runif 2019-09-11 13:06:12
## 8: 1008 2019-09-11 13:06:12
## 9: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 10: vweave_rmarkdown 2019-09-11 13:06:12
## 11: process_file 2019-09-11 13:06:12
## 12: process_group 2019-09-11 13:06:12
## 13: process_group.block 2019-09-11 13:06:12
## 14: call_block 2019-09-11 13:06:12
## 15: block_exec 2019-09-11 13:06:12
## 16: in_dir 2019-09-11 13:06:12
## 17: timing_fn 2019-09-11 13:06:12
## 18: handle 2019-09-11 13:06:12
## 19: withVisible 2019-09-11 13:06:12
## 20: n:7eef4eae85fd9229 2019-09-11 13:06:12
## 21: .FUN:881ec847b7161f3c 2019-09-11 13:06:12
## 22: rda 2019-09-11 13:06:12
## 23: ed11e3916d1da1036dca15cba81b337d 2019-09-11 13:06:12
## 24: numeric 2019-09-11 13:06:12
## 25: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 26: f7bee22047b8d0c1 2019-09-11 13:06:12
## 27: b 2019-09-11 13:06:12
## 28: rnorm 2019-09-11 13:06:12
## 29: 1008 2019-09-11 13:06:12
## 30: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 31: vweave_rmarkdown 2019-09-11 13:06:12
## 32: process_file 2019-09-11 13:06:12
## 33: process_group 2019-09-11 13:06:12
## 34: process_group.block 2019-09-11 13:06:12
## 35: call_block 2019-09-11 13:06:12
## 36: block_exec 2019-09-11 13:06:12
## 37: in_dir 2019-09-11 13:06:12
## 38: timing_fn 2019-09-11 13:06:12
## 39: handle 2019-09-11 13:06:12
## 40: withVisible 2019-09-11 13:06:12
## 41: n:7eef4eae85fd9229 2019-09-11 13:06:12
## 42: .FUN:4f604aa46882b368 2019-09-11 13:06:12
## tagValue 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
## artifact tagKey
## 1: a4d11c23c568a3e99003c55d15a16781 format
## 2: a4d11c23c568a3e99003c55d15a16781 name
## 3: a4d11c23c568a3e99003c55d15a16781 class
## 4: a4d11c23c568a3e99003c55d15a16781 date
## 5: a4d11c23c568a3e99003c55d15a16781 cacheId
## 6: a4d11c23c568a3e99003c55d15a16781 objectName
## 7: a4d11c23c568a3e99003c55d15a16781 function
## 8: a4d11c23c568a3e99003c55d15a16781 object.size
## 9: a4d11c23c568a3e99003c55d15a16781 accessed
## 10: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 11: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 12: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 13: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 14: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 15: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 16: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 17: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 18: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 19: a4d11c23c568a3e99003c55d15a16781 otherFunctions
## 20: a4d11c23c568a3e99003c55d15a16781 preDigest
## 21: a4d11c23c568a3e99003c55d15a16781 preDigest
## 22: ed11e3916d1da1036dca15cba81b337d format
## 23: ed11e3916d1da1036dca15cba81b337d name
## 24: ed11e3916d1da1036dca15cba81b337d class
## 25: ed11e3916d1da1036dca15cba81b337d date
## 26: ed11e3916d1da1036dca15cba81b337d cacheId
## 27: ed11e3916d1da1036dca15cba81b337d objectName
## 28: ed11e3916d1da1036dca15cba81b337d function
## 29: ed11e3916d1da1036dca15cba81b337d object.size
## 30: ed11e3916d1da1036dca15cba81b337d accessed
## 31: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 32: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 33: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 34: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 35: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 36: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 37: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 38: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 39: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 40: ed11e3916d1da1036dca15cba81b337d otherFunctions
## 41: ed11e3916d1da1036dca15cba81b337d preDigest
## 42: ed11e3916d1da1036dca15cba81b337d preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2019-09-11 13:06:12
## 2: a4d11c23c568a3e99003c55d15a16781 2019-09-11 13:06:12
## 3: numeric 2019-09-11 13:06:12
## 4: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 5: 3aef38d1fc02aee5 2019-09-11 13:06:12
## 6: a 2019-09-11 13:06:12
## 7: runif 2019-09-11 13:06:12
## 8: 1008 2019-09-11 13:06:12
## 9: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 10: vweave_rmarkdown 2019-09-11 13:06:12
## 11: process_file 2019-09-11 13:06:12
## 12: process_group 2019-09-11 13:06:12
## 13: process_group.block 2019-09-11 13:06:12
## 14: call_block 2019-09-11 13:06:12
## 15: block_exec 2019-09-11 13:06:12
## 16: in_dir 2019-09-11 13:06:12
## 17: timing_fn 2019-09-11 13:06:12
## 18: handle 2019-09-11 13:06:12
## 19: withVisible 2019-09-11 13:06:12
## 20: n:7eef4eae85fd9229 2019-09-11 13:06:12
## 21: .FUN:881ec847b7161f3c 2019-09-11 13:06:12
## 22: rda 2019-09-11 13:06:12
## 23: ed11e3916d1da1036dca15cba81b337d 2019-09-11 13:06:12
## 24: numeric 2019-09-11 13:06:12
## 25: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 26: f7bee22047b8d0c1 2019-09-11 13:06:12
## 27: b 2019-09-11 13:06:12
## 28: rnorm 2019-09-11 13:06:12
## 29: 1008 2019-09-11 13:06:12
## 30: 2019-09-11 13:06:12 2019-09-11 13:06:12
## 31: vweave_rmarkdown 2019-09-11 13:06:12
## 32: process_file 2019-09-11 13:06:12
## 33: process_group 2019-09-11 13:06:12
## 34: process_group.block 2019-09-11 13:06:12
## 35: call_block 2019-09-11 13:06:12
## 36: block_exec 2019-09-11 13:06:12
## 37: in_dir 2019-09-11 13:06:12
## 38: timing_fn 2019-09-11 13:06:12
## 39: handle 2019-09-11 13:06:12
## 40: withVisible 2019-09-11 13:06:12
## 41: n:7eef4eae85fd9229 2019-09-11 13:06:12
## 42: .FUN:4f604aa46882b368 2019-09-11 13:06:12
## tagValue createdDate
ras <- raster(extent(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")
# A slow operation, like GIS operation
notCached <- suppressWarnings(
# project raster generates warnings when run non-interactively
projectRaster(ras, crs = crs(ras), res = 5, cacheRepo = tmpDir)
)
cached <- suppressWarnings(
# 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
reRun <- suppressWarnings(
# project raster generates warnings when run non-interactively
Cache(projectRaster, ras, crs = crs(ras), res = 5, cacheRepo = tmpDir)
)
## loading cached result from previous projectRaster call, adding to memoised copy
## [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 that 1 second. 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, cacheRepo = 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 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.7706254 0.2526370 -1.0965456
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## attr(,"tags")
## [1] "cacheId:a7af5367c13aba8f"
## attr(,"call")
## [1] ""
## Cache size:
## Total (including Rasters): 504 bytes
## Selected objects (not including Rasters): 504 bytes
## artifact tagKey
## 1: 1fcdd64e799624731d8fb7534e39675b format
## 2: 1fcdd64e799624731d8fb7534e39675b name
## 3: 1fcdd64e799624731d8fb7534e39675b class
## 4: 1fcdd64e799624731d8fb7534e39675b date
## 5: 1fcdd64e799624731d8fb7534e39675b cacheId
## 6: 1fcdd64e799624731d8fb7534e39675b function
## 7: 1fcdd64e799624731d8fb7534e39675b object.size
## 8: 1fcdd64e799624731d8fb7534e39675b accessed
## 9: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 10: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 11: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 12: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 13: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 14: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 15: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 16: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 17: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 18: 1fcdd64e799624731d8fb7534e39675b otherFunctions
## 19: 1fcdd64e799624731d8fb7534e39675b preDigest
## 20: 1fcdd64e799624731d8fb7534e39675b preDigest
## 21: 1fcdd64e799624731d8fb7534e39675b preDigest
## 22: c436b31675c7e99a603c897e81850837 format
## 23: c436b31675c7e99a603c897e81850837 name
## 24: c436b31675c7e99a603c897e81850837 class
## 25: c436b31675c7e99a603c897e81850837 date
## 26: c436b31675c7e99a603c897e81850837 cacheId
## 27: c436b31675c7e99a603c897e81850837 function
## 28: c436b31675c7e99a603c897e81850837 object.size
## 29: c436b31675c7e99a603c897e81850837 accessed
## 30: c436b31675c7e99a603c897e81850837 otherFunctions
## 31: c436b31675c7e99a603c897e81850837 otherFunctions
## 32: c436b31675c7e99a603c897e81850837 otherFunctions
## 33: c436b31675c7e99a603c897e81850837 otherFunctions
## 34: c436b31675c7e99a603c897e81850837 otherFunctions
## 35: c436b31675c7e99a603c897e81850837 otherFunctions
## 36: c436b31675c7e99a603c897e81850837 otherFunctions
## 37: c436b31675c7e99a603c897e81850837 otherFunctions
## 38: c436b31675c7e99a603c897e81850837 otherFunctions
## 39: c436b31675c7e99a603c897e81850837 otherFunctions
## 40: c436b31675c7e99a603c897e81850837 preDigest
## 41: c436b31675c7e99a603c897e81850837 preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2019-09-11 13:06:12
## 2: 1fcdd64e799624731d8fb7534e39675b 2019-09-11 13:06:13
## 3: numeric 2019-09-11 13:06:13
## 4: 2019-09-11 13:06:12 2019-09-11 13:06:13
## 5: 4ac2b7c0f42d1e46 2019-09-11 13:06:13
## 6: rnorm 2019-09-11 13:06:13
## 7: 1008 2019-09-11 13:06:13
## 8: 2019-09-11 13:06:12 2019-09-11 13:06:13
## 9: vweave_rmarkdown 2019-09-11 13:06:13
## 10: process_file 2019-09-11 13:06:13
## 11: process_group 2019-09-11 13:06:13
## 12: process_group.block 2019-09-11 13:06:13
## 13: call_block 2019-09-11 13:06:13
## 14: block_exec 2019-09-11 13:06:13
## 15: in_dir 2019-09-11 13:06:13
## 16: timing_fn 2019-09-11 13:06:13
## 17: handle 2019-09-11 13:06:13
## 18: withVisible 2019-09-11 13:06:13
## 19: n:7f12988bd88a0fb8 2019-09-11 13:06:13
## 20: mean:22413394efd9f6a3 2019-09-11 13:06:13
## 21: .FUN:4f604aa46882b368 2019-09-11 13:06:13
## 22: rda 2019-09-11 13:06:13
## 23: c436b31675c7e99a603c897e81850837 2019-09-11 13:06:13
## 24: numeric 2019-09-11 13:06:13
## 25: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 26: a7af5367c13aba8f 2019-09-11 13:06:13
## 27: outer 2019-09-11 13:06:13
## 28: 1008 2019-09-11 13:06:13
## 29: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 30: vweave_rmarkdown 2019-09-11 13:06:13
## 31: process_file 2019-09-11 13:06:13
## 32: process_group 2019-09-11 13:06:13
## 33: process_group.block 2019-09-11 13:06:13
## 34: call_block 2019-09-11 13:06:13
## 35: block_exec 2019-09-11 13:06:13
## 36: in_dir 2019-09-11 13:06:13
## 37: timing_fn 2019-09-11 13:06:13
## 38: handle 2019-09-11 13:06:13
## 39: withVisible 2019-09-11 13:06:13
## 40: n:82dc709f2b91918a 2019-09-11 13:06:13
## 41: .FUN:892a6afc47a63a90 2019-09-11 13:06:13
## tagValue createdDate
## Cache size:
## Total (including Rasters): 252 bytes
## Selected objects (not including Rasters): 252 bytes
## artifact tagKey
## 1: b2e6bec7b3fe10c1a7f01cbaaa121831 format
## 2: b2e6bec7b3fe10c1a7f01cbaaa121831 name
## 3: b2e6bec7b3fe10c1a7f01cbaaa121831 class
## 4: b2e6bec7b3fe10c1a7f01cbaaa121831 date
## 5: b2e6bec7b3fe10c1a7f01cbaaa121831 cacheId
## 6: b2e6bec7b3fe10c1a7f01cbaaa121831 function
## 7: b2e6bec7b3fe10c1a7f01cbaaa121831 object.size
## 8: b2e6bec7b3fe10c1a7f01cbaaa121831 accessed
## 9: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 10: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 11: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 12: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 13: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 14: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 15: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 16: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 17: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 18: b2e6bec7b3fe10c1a7f01cbaaa121831 otherFunctions
## 19: b2e6bec7b3fe10c1a7f01cbaaa121831 preDigest
## 20: b2e6bec7b3fe10c1a7f01cbaaa121831 preDigest
## tagValue createdDate
## 1: rda 2019-09-11 13:06:13
## 2: b2e6bec7b3fe10c1a7f01cbaaa121831 2019-09-11 13:06:13
## 3: numeric 2019-09-11 13:06:13
## 4: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 5: 33ceb4fb525fd08f 2019-09-11 13:06:13
## 6: inner 2019-09-11 13:06:13
## 7: 1008 2019-09-11 13:06:13
## 8: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 9: vweave_rmarkdown 2019-09-11 13:06:13
## 10: process_file 2019-09-11 13:06:13
## 11: process_group 2019-09-11 13:06:13
## 12: process_group.block 2019-09-11 13:06:13
## 13: call_block 2019-09-11 13:06:13
## 14: block_exec 2019-09-11 13:06:13
## 15: in_dir 2019-09-11 13:06:13
## 16: timing_fn 2019-09-11 13:06:13
## 17: handle 2019-09-11 13:06:13
## 18: withVisible 2019-09-11 13:06:13
## 19: mean:22413394efd9f6a3 2019-09-11 13:06:13
## 20: .FUN:87e2c30917a34d25 2019-09-11 13:06:13
# 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, cacheRepo = tmpdir1, userTags = outerTag)
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
## artifact tagKey
## 1: 09efb6cab5fec5b60fb0f15c96672044 format
## 2: 09efb6cab5fec5b60fb0f15c96672044 name
## 3: 09efb6cab5fec5b60fb0f15c96672044 class
## 4: 09efb6cab5fec5b60fb0f15c96672044 date
## 5: 09efb6cab5fec5b60fb0f15c96672044 cacheId
## 6: 09efb6cab5fec5b60fb0f15c96672044 innerTag
## 7: 09efb6cab5fec5b60fb0f15c96672044 outerTag
## 8: 09efb6cab5fec5b60fb0f15c96672044 function
## 9: 09efb6cab5fec5b60fb0f15c96672044 object.size
## 10: 09efb6cab5fec5b60fb0f15c96672044 accessed
## 11: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 12: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 13: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 14: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 15: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 16: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 17: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 18: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 19: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 20: 09efb6cab5fec5b60fb0f15c96672044 otherFunctions
## 21: 09efb6cab5fec5b60fb0f15c96672044 preDigest
## 22: 09efb6cab5fec5b60fb0f15c96672044 preDigest
## 23: 09efb6cab5fec5b60fb0f15c96672044 preDigest
## 24: b4032f10fd0fe5f660a1e217a00ae637 format
## 25: b4032f10fd0fe5f660a1e217a00ae637 name
## 26: b4032f10fd0fe5f660a1e217a00ae637 class
## 27: b4032f10fd0fe5f660a1e217a00ae637 date
## 28: b4032f10fd0fe5f660a1e217a00ae637 cacheId
## 29: b4032f10fd0fe5f660a1e217a00ae637 outerTag
## 30: b4032f10fd0fe5f660a1e217a00ae637 function
## 31: b4032f10fd0fe5f660a1e217a00ae637 object.size
## 32: b4032f10fd0fe5f660a1e217a00ae637 accessed
## 33: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 34: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 35: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 36: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 37: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 38: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 39: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 40: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 41: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 42: b4032f10fd0fe5f660a1e217a00ae637 otherFunctions
## 43: b4032f10fd0fe5f660a1e217a00ae637 preDigest
## 44: b4032f10fd0fe5f660a1e217a00ae637 preDigest
## 45: e151162a801ed95f8fcfa33557bd978d format
## 46: e151162a801ed95f8fcfa33557bd978d name
## 47: e151162a801ed95f8fcfa33557bd978d class
## 48: e151162a801ed95f8fcfa33557bd978d date
## 49: e151162a801ed95f8fcfa33557bd978d cacheId
## 50: e151162a801ed95f8fcfa33557bd978d outerTag
## 51: e151162a801ed95f8fcfa33557bd978d function
## 52: e151162a801ed95f8fcfa33557bd978d object.size
## 53: e151162a801ed95f8fcfa33557bd978d accessed
## 54: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 55: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 56: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 57: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 58: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 59: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 60: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 61: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 62: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 63: e151162a801ed95f8fcfa33557bd978d otherFunctions
## 64: e151162a801ed95f8fcfa33557bd978d preDigest
## 65: e151162a801ed95f8fcfa33557bd978d preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2019-09-11 13:06:13
## 2: 09efb6cab5fec5b60fb0f15c96672044 2019-09-11 13:06:13
## 3: numeric 2019-09-11 13:06:13
## 4: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 5: 4ac2b7c0f42d1e46 2019-09-11 13:06:13
## 6: innerTag 2019-09-11 13:06:13
## 7: outerTag 2019-09-11 13:06:13
## 8: rnorm 2019-09-11 13:06:13
## 9: 1008 2019-09-11 13:06:13
## 10: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 11: vweave_rmarkdown 2019-09-11 13:06:13
## 12: process_file 2019-09-11 13:06:13
## 13: process_group 2019-09-11 13:06:13
## 14: process_group.block 2019-09-11 13:06:13
## 15: call_block 2019-09-11 13:06:13
## 16: block_exec 2019-09-11 13:06:13
## 17: in_dir 2019-09-11 13:06:13
## 18: timing_fn 2019-09-11 13:06:13
## 19: handle 2019-09-11 13:06:13
## 20: withVisible 2019-09-11 13:06:13
## 21: n:7f12988bd88a0fb8 2019-09-11 13:06:13
## 22: mean:22413394efd9f6a3 2019-09-11 13:06:13
## 23: .FUN:4f604aa46882b368 2019-09-11 13:06:13
## 24: rda 2019-09-11 13:06:13
## 25: b4032f10fd0fe5f660a1e217a00ae637 2019-09-11 13:06:13
## 26: numeric 2019-09-11 13:06:13
## 27: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 28: b06af03d5a73dc7d 2019-09-11 13:06:13
## 29: outerTag 2019-09-11 13:06:13
## 30: inner 2019-09-11 13:06:13
## 31: 1008 2019-09-11 13:06:13
## 32: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 33: vweave_rmarkdown 2019-09-11 13:06:13
## 34: process_file 2019-09-11 13:06:13
## 35: process_group 2019-09-11 13:06:13
## 36: process_group.block 2019-09-11 13:06:13
## 37: call_block 2019-09-11 13:06:13
## 38: block_exec 2019-09-11 13:06:13
## 39: in_dir 2019-09-11 13:06:13
## 40: timing_fn 2019-09-11 13:06:13
## 41: handle 2019-09-11 13:06:13
## 42: withVisible 2019-09-11 13:06:13
## 43: mean:22413394efd9f6a3 2019-09-11 13:06:13
## 44: .FUN:7ad10bc1ae528d8c 2019-09-11 13:06:13
## 45: rda 2019-09-11 13:06:13
## 46: e151162a801ed95f8fcfa33557bd978d 2019-09-11 13:06:13
## 47: numeric 2019-09-11 13:06:13
## 48: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 49: 88a34e1d033329e5 2019-09-11 13:06:13
## 50: outerTag 2019-09-11 13:06:13
## 51: outer 2019-09-11 13:06:13
## 52: 1008 2019-09-11 13:06:13
## 53: 2019-09-11 13:06:13 2019-09-11 13:06:13
## 54: vweave_rmarkdown 2019-09-11 13:06:13
## 55: process_file 2019-09-11 13:06:13
## 56: process_group 2019-09-11 13:06:13
## 57: process_group.block 2019-09-11 13:06:13
## 58: call_block 2019-09-11 13:06:13
## 59: block_exec 2019-09-11 13:06:13
## 60: in_dir 2019-09-11 13:06:13
## 61: timing_fn 2019-09-11 13:06:13
## 62: handle 2019-09-11 13:06:13
## 63: withVisible 2019-09-11 13:06:13
## 64: n:82dc709f2b91918a 2019-09-11 13:06:13
## 65: .FUN:5f06fb5fbffe9e3b 2019-09-11 13:06:13
## tagValue 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"
.
## [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: ad184ce64541972b50afd8e7b75f821b
Cache(rnorm, 1, cacheRepo = tmpdir1, cacheId = "ad184ce64541972b50afd8e7b75f821b") # same value
## cacheId is not same as calculated hash. Manually searching for cacheId:ad184ce64541972b50afd8e7b75f821b
## [1] 0.1836433
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## attr(,"tags")
## [1] "cacheId:ad184ce64541972b50afd8e7b75f821b"
## attr(,"call")
## [1] ""
# override even with different inputs:
Cache(rnorm, 2, cacheRepo = tmpdir1, cacheId = "ad184ce64541972b50afd8e7b75f821b")
## cacheId is not same as calculated hash. Manually searching for cacheId:ad184ce64541972b50afd8e7b75f821b
## loading cached result from previous rnorm call, adding to memoised copy
## [1] 0.1836433
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] FALSE
##
## attr(,"tags")
## [1] "cacheId:ad184ce64541972b50afd8e7b75f821b"
## attr(,"call")
## [1] ""
Since the cache is simply an archivist
repository, all archivist
functions will work as is. 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).
if (requireNamespace("archivist")) {
# get the RasterLayer that was produced with the gaussMap function:
mapHash <- unique(showCache(tmpDir, userTags = "projectRaster")$artifact)
map <- archivist::loadFromLocalRepo(md5hash = mapHash[1], repoDir = tmpDir, value = TRUE)
plot(map)
}
## Cache size:
## Total (including Rasters): 3.3 Kb
## Selected objects (not including Rasters): 3.3 Kb
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.
Cache(<functionName>, <other arguments>)
This will allow fine scale control of individual function calls.