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; however, it also uses fastdigest::fastdigest
to make it substantially faster in many cases. 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)
tmpDir <- file.path(tempdir(), "reproducible_examples", "Cache")
checkPath(tmpDir, create = TRUE)
## [1] "/tmp/RtmpUaFvn0/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.220 0.143 2.366
# 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.474 0.144 2.743
# vastly faster the second time
system.time(map2 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir))
## loading cached result from previous projectRaster call, adding to memoised copy
## user system elapsed
## 0.113 0.000 0.113
# even faster the third time
system.time(map3 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir))
## loading memoised result from previous projectRaster call.
## user system elapsed
## 0.037 0.005 0.042
all.equal(map1, map2) # TRUE
## [1] TRUE
all.equal(map1, map3) # TRUE
## [1] TRUE
library(raster)
# magrittr, if loaded, gives an error below
try(detach("package:magrittr", unload = TRUE), silent = TRUE)
try(clearCache(tmpDir), silent = TRUE) # just to make sure it is clear
## Cache size:
## Total (including Rasters): 1.9 Mb
## Selected objects (not including Rasters): 1.9 Mb
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
ranNumsC <- rnorm(10, 16) %>% Cache(cacheRepo = tmpDir) # recovers cached copy
## loading memoised result from previous 'rnorm' pipe sequence call.
ranNumsD <- Cache(quote(rnorm(n = 10, 16)), cacheRepo = tmpDir) # recovers cached copy
## loading memoised result from previous rnorm call.
# Any minor change makes it different
ranNumsE <- rnorm(10, 6) %>% Cache(cacheRepo = tmpDir) # different
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
wholeCache <- showCache(tmpDir)
## Cache size:
## Total (including Rasters): 476 bytes
## Selected objects (not including Rasters): 476 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): 476 bytes
## Selected objects (not including Rasters): 476 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) # remove ones not recently accessed
## Cache size:
## Total (including Rasters): 476 bytes
## Selected objects (not including Rasters): 476 bytes
showCache(tmpDir) # still has more recently accessed
## Cache size:
## Total (including Rasters): 0 bytes
## Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows) of 3 cols: md5hash,name,createdDate
clearCache(tmpDir)
## Cache size:
## Total (including Rasters): 0 bytes
## Selected objects (not including Rasters): 0 bytes
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)
## Cache size:
## Total (including Rasters): 476 bytes
## Selected objects (not including Rasters): 476 bytes
## artifact tagKey
## 1: 129c4fbd611bb1c66a677fa2e8f6e860 format
## 2: 129c4fbd611bb1c66a677fa2e8f6e860 name
## 3: 129c4fbd611bb1c66a677fa2e8f6e860 class
## 4: 129c4fbd611bb1c66a677fa2e8f6e860 date
## 5: 129c4fbd611bb1c66a677fa2e8f6e860 cacheId
## 6: 129c4fbd611bb1c66a677fa2e8f6e860 objectName
## 7: 129c4fbd611bb1c66a677fa2e8f6e860 function
## 8: 129c4fbd611bb1c66a677fa2e8f6e860 object.size
## 9: 129c4fbd611bb1c66a677fa2e8f6e860 accessed
## 10: 129c4fbd611bb1c66a677fa2e8f6e860 otherFunctions
## 11: 129c4fbd611bb1c66a677fa2e8f6e860 otherFunctions
## 12: 129c4fbd611bb1c66a677fa2e8f6e860 otherFunctions
## 13: 129c4fbd611bb1c66a677fa2e8f6e860 otherFunctions
## 14: 129c4fbd611bb1c66a677fa2e8f6e860 otherFunctions
## 15: 129c4fbd611bb1c66a677fa2e8f6e860 preDigest
## 16: 129c4fbd611bb1c66a677fa2e8f6e860 preDigest
## 17: b25cd11467694fc736ec444b8c25c187 format
## 18: b25cd11467694fc736ec444b8c25c187 name
## 19: b25cd11467694fc736ec444b8c25c187 class
## 20: b25cd11467694fc736ec444b8c25c187 date
## 21: b25cd11467694fc736ec444b8c25c187 cacheId
## 22: b25cd11467694fc736ec444b8c25c187 objectName
## 23: b25cd11467694fc736ec444b8c25c187 function
## 24: b25cd11467694fc736ec444b8c25c187 object.size
## 25: b25cd11467694fc736ec444b8c25c187 accessed
## 26: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 27: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 28: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 29: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 30: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 31: b25cd11467694fc736ec444b8c25c187 preDigest
## 32: b25cd11467694fc736ec444b8c25c187 preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2018-08-07 11:05:59
## 2: 129c4fbd611bb1c66a677fa2e8f6e860 2018-08-07 11:05:59
## 3: numeric 2018-08-07 11:05:59
## 4: 2018-08-07 11:05:59 2018-08-07 11:05:59
## 5: e37bb635c97bc2eeecab63816b881bbc 2018-08-07 11:05:59
## 6: b 2018-08-07 11:05:59
## 7: runif 2018-08-07 11:05:59
## 8: 952 2018-08-07 11:05:59
## 9: 2018-08-07 11:05:59 2018-08-07 11:05:59
## 10: process_file 2018-08-07 11:05:59
## 11: process_group 2018-08-07 11:05:59
## 12: process_group.block 2018-08-07 11:05:59
## 13: call_block 2018-08-07 11:05:59
## 14: block_exec 2018-08-07 11:05:59
## 15: n:969a49ec15bcd4323ff31538af321264 2018-08-07 11:05:59
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-08-07 11:05:59
## 17: rda 2018-08-07 11:05:59
## 18: b25cd11467694fc736ec444b8c25c187 2018-08-07 11:05:59
## 19: numeric 2018-08-07 11:05:59
## 20: 2018-08-07 11:05:59 2018-08-07 11:05:59
## 21: 85874f26b2e0c1ef689a7d379d275ebf 2018-08-07 11:05:59
## 22: a 2018-08-07 11:05:59
## 23: rnorm 2018-08-07 11:05:59
## 24: 952 2018-08-07 11:05:59
## 25: 2018-08-07 11:05:59 2018-08-07 11:05:59
## 26: process_file 2018-08-07 11:05:59
## 27: process_group 2018-08-07 11:05:59
## 28: process_group.block 2018-08-07 11:05:59
## 29: call_block 2018-08-07 11:05:59
## 30: block_exec 2018-08-07 11:05:59
## 31: n:969a49ec15bcd4323ff31538af321264 2018-08-07 11:05:59
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-08-07 11:05:59
## tagValue createdDate
# Keep all Cache items created with an rnorm() call
keepCache(tmpDir, userTags = "rnorm")
## Cache size:
## Total (including Rasters): 476 bytes
## Selected objects (not including Rasters): 238 bytes
## artifact tagKey
## 1: b25cd11467694fc736ec444b8c25c187 format
## 2: b25cd11467694fc736ec444b8c25c187 name
## 3: b25cd11467694fc736ec444b8c25c187 class
## 4: b25cd11467694fc736ec444b8c25c187 date
## 5: b25cd11467694fc736ec444b8c25c187 cacheId
## 6: b25cd11467694fc736ec444b8c25c187 objectName
## 7: b25cd11467694fc736ec444b8c25c187 function
## 8: b25cd11467694fc736ec444b8c25c187 object.size
## 9: b25cd11467694fc736ec444b8c25c187 accessed
## 10: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 11: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 12: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 13: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 14: b25cd11467694fc736ec444b8c25c187 otherFunctions
## 15: b25cd11467694fc736ec444b8c25c187 preDigest
## 16: b25cd11467694fc736ec444b8c25c187 preDigest
## tagValue createdDate
## 1: rda 2018-08-07 11:05:59
## 2: b25cd11467694fc736ec444b8c25c187 2018-08-07 11:05:59
## 3: numeric 2018-08-07 11:05:59
## 4: 2018-08-07 11:05:59 2018-08-07 11:05:59
## 5: 85874f26b2e0c1ef689a7d379d275ebf 2018-08-07 11:05:59
## 6: a 2018-08-07 11:05:59
## 7: rnorm 2018-08-07 11:05:59
## 8: 952 2018-08-07 11:05:59
## 9: 2018-08-07 11:05:59 2018-08-07 11:05:59
## 10: process_file 2018-08-07 11:05:59
## 11: process_group 2018-08-07 11:05:59
## 12: process_group.block 2018-08-07 11:05:59
## 13: call_block 2018-08-07 11:05:59
## 14: block_exec 2018-08-07 11:05:59
## 15: n:969a49ec15bcd4323ff31538af321264 2018-08-07 11:05:59
## 16: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-08-07 11:05:59
# Remove all Cache items that happened within a rnorm() call
clearCache(tmpDir, userTags = "rnorm")
## Cache size:
## Total (including Rasters): 238 bytes
## Selected objects (not including Rasters): 238 bytes
showCache(tmpDir) ## empty
## Cache size:
## Total (including Rasters): 0 bytes
## Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows) of 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): 476 bytes
## Selected objects (not including Rasters): 476 bytes
## artifact tagKey
## 1: a12547f9875d9c1eed262d32532d4e9e format
## 2: a12547f9875d9c1eed262d32532d4e9e name
## 3: a12547f9875d9c1eed262d32532d4e9e class
## 4: a12547f9875d9c1eed262d32532d4e9e date
## 5: a12547f9875d9c1eed262d32532d4e9e cacheId
## 6: a12547f9875d9c1eed262d32532d4e9e objectName
## 7: a12547f9875d9c1eed262d32532d4e9e function
## 8: a12547f9875d9c1eed262d32532d4e9e object.size
## 9: a12547f9875d9c1eed262d32532d4e9e accessed
## 10: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 11: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 12: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 13: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 14: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 15: a12547f9875d9c1eed262d32532d4e9e preDigest
## 16: a12547f9875d9c1eed262d32532d4e9e preDigest
## 17: fbc518cef93ac0f40d8c3a7eac57207c format
## 18: fbc518cef93ac0f40d8c3a7eac57207c name
## 19: fbc518cef93ac0f40d8c3a7eac57207c class
## 20: fbc518cef93ac0f40d8c3a7eac57207c date
## 21: fbc518cef93ac0f40d8c3a7eac57207c cacheId
## 22: fbc518cef93ac0f40d8c3a7eac57207c objectName
## 23: fbc518cef93ac0f40d8c3a7eac57207c function
## 24: fbc518cef93ac0f40d8c3a7eac57207c object.size
## 25: fbc518cef93ac0f40d8c3a7eac57207c accessed
## 26: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 27: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 28: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 29: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 30: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 31: fbc518cef93ac0f40d8c3a7eac57207c preDigest
## 32: fbc518cef93ac0f40d8c3a7eac57207c preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2018-08-07 11:06:00
## 2: a12547f9875d9c1eed262d32532d4e9e 2018-08-07 11:06:00
## 3: numeric 2018-08-07 11:06:00
## 4: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 5: e37bb635c97bc2eeecab63816b881bbc 2018-08-07 11:06:00
## 6: a 2018-08-07 11:06:00
## 7: runif 2018-08-07 11:06:00
## 8: 952 2018-08-07 11:06:00
## 9: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 10: process_file 2018-08-07 11:06:00
## 11: process_group 2018-08-07 11:06:00
## 12: process_group.block 2018-08-07 11:06:00
## 13: call_block 2018-08-07 11:06:00
## 14: block_exec 2018-08-07 11:06:00
## 15: n:969a49ec15bcd4323ff31538af321264 2018-08-07 11:06:00
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-08-07 11:06:00
## 17: rda 2018-08-07 11:06:00
## 18: fbc518cef93ac0f40d8c3a7eac57207c 2018-08-07 11:06:00
## 19: numeric 2018-08-07 11:06:00
## 20: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 21: 85874f26b2e0c1ef689a7d379d275ebf 2018-08-07 11:06:00
## 22: b 2018-08-07 11:06:00
## 23: rnorm 2018-08-07 11:06:00
## 24: 952 2018-08-07 11:06:00
## 25: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 26: process_file 2018-08-07 11:06:00
## 27: process_group 2018-08-07 11:06:00
## 28: process_group.block 2018-08-07 11:06:00
## 29: call_block 2018-08-07 11:06:00
## 30: block_exec 2018-08-07 11:06:00
## 31: n:969a49ec15bcd4323ff31538af321264 2018-08-07 11:06:00
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-08-07 11:06:00
## 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): 476 bytes
## Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows) of 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): 476 bytes
## Selected objects (not including Rasters): 476 bytes
## artifact tagKey
## 1: a12547f9875d9c1eed262d32532d4e9e format
## 2: a12547f9875d9c1eed262d32532d4e9e name
## 3: a12547f9875d9c1eed262d32532d4e9e class
## 4: a12547f9875d9c1eed262d32532d4e9e date
## 5: a12547f9875d9c1eed262d32532d4e9e cacheId
## 6: a12547f9875d9c1eed262d32532d4e9e objectName
## 7: a12547f9875d9c1eed262d32532d4e9e function
## 8: a12547f9875d9c1eed262d32532d4e9e object.size
## 9: a12547f9875d9c1eed262d32532d4e9e accessed
## 10: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 11: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 12: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 13: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 14: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 15: a12547f9875d9c1eed262d32532d4e9e preDigest
## 16: a12547f9875d9c1eed262d32532d4e9e preDigest
## 17: fbc518cef93ac0f40d8c3a7eac57207c format
## 18: fbc518cef93ac0f40d8c3a7eac57207c name
## 19: fbc518cef93ac0f40d8c3a7eac57207c class
## 20: fbc518cef93ac0f40d8c3a7eac57207c date
## 21: fbc518cef93ac0f40d8c3a7eac57207c cacheId
## 22: fbc518cef93ac0f40d8c3a7eac57207c objectName
## 23: fbc518cef93ac0f40d8c3a7eac57207c function
## 24: fbc518cef93ac0f40d8c3a7eac57207c object.size
## 25: fbc518cef93ac0f40d8c3a7eac57207c accessed
## 26: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 27: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 28: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 29: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 30: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 31: fbc518cef93ac0f40d8c3a7eac57207c preDigest
## 32: fbc518cef93ac0f40d8c3a7eac57207c preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2018-08-07 11:06:00
## 2: a12547f9875d9c1eed262d32532d4e9e 2018-08-07 11:06:00
## 3: numeric 2018-08-07 11:06:00
## 4: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 5: e37bb635c97bc2eeecab63816b881bbc 2018-08-07 11:06:00
## 6: a 2018-08-07 11:06:00
## 7: runif 2018-08-07 11:06:00
## 8: 952 2018-08-07 11:06:00
## 9: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 10: process_file 2018-08-07 11:06:00
## 11: process_group 2018-08-07 11:06:00
## 12: process_group.block 2018-08-07 11:06:00
## 13: call_block 2018-08-07 11:06:00
## 14: block_exec 2018-08-07 11:06:00
## 15: n:969a49ec15bcd4323ff31538af321264 2018-08-07 11:06:00
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-08-07 11:06:00
## 17: rda 2018-08-07 11:06:00
## 18: fbc518cef93ac0f40d8c3a7eac57207c 2018-08-07 11:06:00
## 19: numeric 2018-08-07 11:06:00
## 20: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 21: 85874f26b2e0c1ef689a7d379d275ebf 2018-08-07 11:06:00
## 22: b 2018-08-07 11:06:00
## 23: rnorm 2018-08-07 11:06:00
## 24: 952 2018-08-07 11:06:00
## 25: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 26: process_file 2018-08-07 11:06:00
## 27: process_group 2018-08-07 11:06:00
## 28: process_group.block 2018-08-07 11:06:00
## 29: call_block 2018-08-07 11:06:00
## 30: block_exec 2018-08-07 11:06:00
## 31: n:969a49ec15bcd4323ff31538af321264 2018-08-07 11:06:00
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-08-07 11:06:00
## tagValue createdDate
# keep only objects that are either runif or rnorm ("or" search)
keepCache(tmpDir, userTags = "runif|rnorm")
## Cache size:
## Total (including Rasters): 476 bytes
## Selected objects (not including Rasters): 476 bytes
## artifact tagKey
## 1: a12547f9875d9c1eed262d32532d4e9e format
## 2: a12547f9875d9c1eed262d32532d4e9e name
## 3: a12547f9875d9c1eed262d32532d4e9e class
## 4: a12547f9875d9c1eed262d32532d4e9e date
## 5: a12547f9875d9c1eed262d32532d4e9e cacheId
## 6: a12547f9875d9c1eed262d32532d4e9e objectName
## 7: a12547f9875d9c1eed262d32532d4e9e function
## 8: a12547f9875d9c1eed262d32532d4e9e object.size
## 9: a12547f9875d9c1eed262d32532d4e9e accessed
## 10: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 11: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 12: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 13: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 14: a12547f9875d9c1eed262d32532d4e9e otherFunctions
## 15: a12547f9875d9c1eed262d32532d4e9e preDigest
## 16: a12547f9875d9c1eed262d32532d4e9e preDigest
## 17: fbc518cef93ac0f40d8c3a7eac57207c format
## 18: fbc518cef93ac0f40d8c3a7eac57207c name
## 19: fbc518cef93ac0f40d8c3a7eac57207c class
## 20: fbc518cef93ac0f40d8c3a7eac57207c date
## 21: fbc518cef93ac0f40d8c3a7eac57207c cacheId
## 22: fbc518cef93ac0f40d8c3a7eac57207c objectName
## 23: fbc518cef93ac0f40d8c3a7eac57207c function
## 24: fbc518cef93ac0f40d8c3a7eac57207c object.size
## 25: fbc518cef93ac0f40d8c3a7eac57207c accessed
## 26: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 27: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 28: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 29: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 30: fbc518cef93ac0f40d8c3a7eac57207c otherFunctions
## 31: fbc518cef93ac0f40d8c3a7eac57207c preDigest
## 32: fbc518cef93ac0f40d8c3a7eac57207c preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2018-08-07 11:06:00
## 2: a12547f9875d9c1eed262d32532d4e9e 2018-08-07 11:06:00
## 3: numeric 2018-08-07 11:06:00
## 4: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 5: e37bb635c97bc2eeecab63816b881bbc 2018-08-07 11:06:00
## 6: a 2018-08-07 11:06:00
## 7: runif 2018-08-07 11:06:00
## 8: 952 2018-08-07 11:06:00
## 9: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 10: process_file 2018-08-07 11:06:00
## 11: process_group 2018-08-07 11:06:00
## 12: process_group.block 2018-08-07 11:06:00
## 13: call_block 2018-08-07 11:06:00
## 14: block_exec 2018-08-07 11:06:00
## 15: n:969a49ec15bcd4323ff31538af321264 2018-08-07 11:06:00
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-08-07 11:06:00
## 17: rda 2018-08-07 11:06:00
## 18: fbc518cef93ac0f40d8c3a7eac57207c 2018-08-07 11:06:00
## 19: numeric 2018-08-07 11:06:00
## 20: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 21: 85874f26b2e0c1ef689a7d379d275ebf 2018-08-07 11:06:00
## 22: b 2018-08-07 11:06:00
## 23: rnorm 2018-08-07 11:06:00
## 24: 952 2018-08-07 11:06:00
## 25: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 26: process_file 2018-08-07 11:06:00
## 27: process_group 2018-08-07 11:06:00
## 28: process_group.block 2018-08-07 11:06:00
## 29: call_block 2018-08-07 11:06:00
## 30: block_exec 2018-08-07 11:06:00
## 31: n:969a49ec15bcd4323ff31538af321264 2018-08-07 11:06:00
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-08-07 11:06:00
## tagValue createdDate
clearCache(tmpDir)
## Cache size:
## Total (including Rasters): 476 bytes
## Selected objects (not including Rasters): 476 bytes
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
# 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 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.2417744 0.8798532 1.8686174
## attr(,"tags")
## [1] "cacheId:e09bf93970f9e94d5c639cfa8ca722f0"
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## attr(,"call")
## [1] ""
showCache(tmpdir1) # 2 function calls
## Cache size:
## Total (including Rasters): 476 bytes
## Selected objects (not including Rasters): 476 bytes
## artifact tagKey
## 1: 21b0fbddb437e25f2ff1cf6f84e03804 format
## 2: 21b0fbddb437e25f2ff1cf6f84e03804 name
## 3: 21b0fbddb437e25f2ff1cf6f84e03804 class
## 4: 21b0fbddb437e25f2ff1cf6f84e03804 date
## 5: 21b0fbddb437e25f2ff1cf6f84e03804 cacheId
## 6: 21b0fbddb437e25f2ff1cf6f84e03804 function
## 7: 21b0fbddb437e25f2ff1cf6f84e03804 object.size
## 8: 21b0fbddb437e25f2ff1cf6f84e03804 accessed
## 9: 21b0fbddb437e25f2ff1cf6f84e03804 otherFunctions
## 10: 21b0fbddb437e25f2ff1cf6f84e03804 otherFunctions
## 11: 21b0fbddb437e25f2ff1cf6f84e03804 otherFunctions
## 12: 21b0fbddb437e25f2ff1cf6f84e03804 otherFunctions
## 13: 21b0fbddb437e25f2ff1cf6f84e03804 otherFunctions
## 14: 21b0fbddb437e25f2ff1cf6f84e03804 preDigest
## 15: 21b0fbddb437e25f2ff1cf6f84e03804 preDigest
## 16: d9dd71da44acdefc43bd8f7db9d308cd format
## 17: d9dd71da44acdefc43bd8f7db9d308cd name
## 18: d9dd71da44acdefc43bd8f7db9d308cd class
## 19: d9dd71da44acdefc43bd8f7db9d308cd date
## 20: d9dd71da44acdefc43bd8f7db9d308cd cacheId
## 21: d9dd71da44acdefc43bd8f7db9d308cd function
## 22: d9dd71da44acdefc43bd8f7db9d308cd object.size
## 23: d9dd71da44acdefc43bd8f7db9d308cd accessed
## 24: d9dd71da44acdefc43bd8f7db9d308cd otherFunctions
## 25: d9dd71da44acdefc43bd8f7db9d308cd otherFunctions
## 26: d9dd71da44acdefc43bd8f7db9d308cd otherFunctions
## 27: d9dd71da44acdefc43bd8f7db9d308cd otherFunctions
## 28: d9dd71da44acdefc43bd8f7db9d308cd otherFunctions
## 29: d9dd71da44acdefc43bd8f7db9d308cd preDigest
## 30: d9dd71da44acdefc43bd8f7db9d308cd preDigest
## 31: d9dd71da44acdefc43bd8f7db9d308cd preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2018-08-07 11:06:00
## 2: 21b0fbddb437e25f2ff1cf6f84e03804 2018-08-07 11:06:00
## 3: numeric 2018-08-07 11:06:00
## 4: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 5: e09bf93970f9e94d5c639cfa8ca722f0 2018-08-07 11:06:00
## 6: outer 2018-08-07 11:06:00
## 7: 952 2018-08-07 11:06:00
## 8: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 9: process_file 2018-08-07 11:06:00
## 10: process_group 2018-08-07 11:06:00
## 11: process_group.block 2018-08-07 11:06:00
## 12: call_block 2018-08-07 11:06:00
## 13: block_exec 2018-08-07 11:06:00
## 14: n:8128a6180a705341ab7c05cfa945edfb 2018-08-07 11:06:00
## 15: .FUN:b5f6bcbdd9f23e39c2c5d4020e73a6ff 2018-08-07 11:06:00
## 16: rda 2018-08-07 11:06:00
## 17: d9dd71da44acdefc43bd8f7db9d308cd 2018-08-07 11:06:00
## 18: numeric 2018-08-07 11:06:00
## 19: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 20: cec73d63ad3864af8bcd7efc5fae864d 2018-08-07 11:06:00
## 21: rnorm 2018-08-07 11:06:00
## 22: 952 2018-08-07 11:06:00
## 23: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 24: process_file 2018-08-07 11:06:00
## 25: process_group 2018-08-07 11:06:00
## 26: process_group.block 2018-08-07 11:06:00
## 27: call_block 2018-08-07 11:06:00
## 28: block_exec 2018-08-07 11:06:00
## 29: n:4ae3e6b6364de42fdc243469d73448cc 2018-08-07 11:06:00
## 30: mean:c28b87a0be6a99966bdaa5e556974b43 2018-08-07 11:06:00
## 31: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-08-07 11:06:00
## tagValue createdDate
showCache(tmpdir2) # 1 function call
## Cache size:
## Total (including Rasters): 238 bytes
## Selected objects (not including Rasters): 238 bytes
## artifact tagKey
## 1: a0b80588bbabc414a4469941351d0579 format
## 2: a0b80588bbabc414a4469941351d0579 name
## 3: a0b80588bbabc414a4469941351d0579 class
## 4: a0b80588bbabc414a4469941351d0579 date
## 5: a0b80588bbabc414a4469941351d0579 cacheId
## 6: a0b80588bbabc414a4469941351d0579 function
## 7: a0b80588bbabc414a4469941351d0579 object.size
## 8: a0b80588bbabc414a4469941351d0579 accessed
## 9: a0b80588bbabc414a4469941351d0579 otherFunctions
## 10: a0b80588bbabc414a4469941351d0579 otherFunctions
## 11: a0b80588bbabc414a4469941351d0579 otherFunctions
## 12: a0b80588bbabc414a4469941351d0579 otherFunctions
## 13: a0b80588bbabc414a4469941351d0579 otherFunctions
## 14: a0b80588bbabc414a4469941351d0579 preDigest
## 15: a0b80588bbabc414a4469941351d0579 preDigest
## tagValue createdDate
## 1: rda 2018-08-07 11:06:00
## 2: a0b80588bbabc414a4469941351d0579 2018-08-07 11:06:00
## 3: numeric 2018-08-07 11:06:00
## 4: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 5: 19b808ac6871e0184e63c421a116cb61 2018-08-07 11:06:00
## 6: inner 2018-08-07 11:06:00
## 7: 952 2018-08-07 11:06:00
## 8: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 9: process_file 2018-08-07 11:06:00
## 10: process_group 2018-08-07 11:06:00
## 11: process_group.block 2018-08-07 11:06:00
## 12: call_block 2018-08-07 11:06:00
## 13: block_exec 2018-08-07 11:06:00
## 14: mean:c28b87a0be6a99966bdaa5e556974b43 2018-08-07 11:06:00
## 15: .FUN:56a1302d7ef43383766d7af6ca072c4e 2018-08-07 11:06:00
# userTags get appended
# all items have the outer tag propagate, plus inner ones only have inner ones
clearCache(tmpdir1)
## Cache size:
## Total (including Rasters): 476 bytes
## Selected objects (not including Rasters): 476 bytes
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): 714 bytes
## Selected objects (not including Rasters): 714 bytes
## artifact tagKey
## 1: 5cdf5467f1160de931286a18c96d669a format
## 2: 5cdf5467f1160de931286a18c96d669a name
## 3: 5cdf5467f1160de931286a18c96d669a class
## 4: 5cdf5467f1160de931286a18c96d669a date
## 5: 5cdf5467f1160de931286a18c96d669a cacheId
## 6: 5cdf5467f1160de931286a18c96d669a innerTag
## 7: 5cdf5467f1160de931286a18c96d669a outerTag
## 8: 5cdf5467f1160de931286a18c96d669a function
## 9: 5cdf5467f1160de931286a18c96d669a object.size
## 10: 5cdf5467f1160de931286a18c96d669a accessed
## 11: 5cdf5467f1160de931286a18c96d669a otherFunctions
## 12: 5cdf5467f1160de931286a18c96d669a otherFunctions
## 13: 5cdf5467f1160de931286a18c96d669a otherFunctions
## 14: 5cdf5467f1160de931286a18c96d669a otherFunctions
## 15: 5cdf5467f1160de931286a18c96d669a otherFunctions
## 16: 5cdf5467f1160de931286a18c96d669a preDigest
## 17: 5cdf5467f1160de931286a18c96d669a preDigest
## 18: 5cdf5467f1160de931286a18c96d669a preDigest
## 19: dd943b69f9017b3af1a0f6da95936123 format
## 20: dd943b69f9017b3af1a0f6da95936123 name
## 21: dd943b69f9017b3af1a0f6da95936123 class
## 22: dd943b69f9017b3af1a0f6da95936123 date
## 23: dd943b69f9017b3af1a0f6da95936123 cacheId
## 24: dd943b69f9017b3af1a0f6da95936123 outerTag
## 25: dd943b69f9017b3af1a0f6da95936123 function
## 26: dd943b69f9017b3af1a0f6da95936123 object.size
## 27: dd943b69f9017b3af1a0f6da95936123 accessed
## 28: dd943b69f9017b3af1a0f6da95936123 otherFunctions
## 29: dd943b69f9017b3af1a0f6da95936123 otherFunctions
## 30: dd943b69f9017b3af1a0f6da95936123 otherFunctions
## 31: dd943b69f9017b3af1a0f6da95936123 otherFunctions
## 32: dd943b69f9017b3af1a0f6da95936123 otherFunctions
## 33: dd943b69f9017b3af1a0f6da95936123 preDigest
## 34: dd943b69f9017b3af1a0f6da95936123 preDigest
## 35: dfe9fa463103a9de94aaabf0b9bdc8f3 format
## 36: dfe9fa463103a9de94aaabf0b9bdc8f3 name
## 37: dfe9fa463103a9de94aaabf0b9bdc8f3 class
## 38: dfe9fa463103a9de94aaabf0b9bdc8f3 date
## 39: dfe9fa463103a9de94aaabf0b9bdc8f3 cacheId
## 40: dfe9fa463103a9de94aaabf0b9bdc8f3 outerTag
## 41: dfe9fa463103a9de94aaabf0b9bdc8f3 function
## 42: dfe9fa463103a9de94aaabf0b9bdc8f3 object.size
## 43: dfe9fa463103a9de94aaabf0b9bdc8f3 accessed
## 44: dfe9fa463103a9de94aaabf0b9bdc8f3 otherFunctions
## 45: dfe9fa463103a9de94aaabf0b9bdc8f3 otherFunctions
## 46: dfe9fa463103a9de94aaabf0b9bdc8f3 otherFunctions
## 47: dfe9fa463103a9de94aaabf0b9bdc8f3 otherFunctions
## 48: dfe9fa463103a9de94aaabf0b9bdc8f3 otherFunctions
## 49: dfe9fa463103a9de94aaabf0b9bdc8f3 preDigest
## 50: dfe9fa463103a9de94aaabf0b9bdc8f3 preDigest
## artifact tagKey
## tagValue createdDate
## 1: rda 2018-08-07 11:06:00
## 2: 5cdf5467f1160de931286a18c96d669a 2018-08-07 11:06:00
## 3: numeric 2018-08-07 11:06:00
## 4: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 5: cec73d63ad3864af8bcd7efc5fae864d 2018-08-07 11:06:00
## 6: innerTag 2018-08-07 11:06:00
## 7: outerTag 2018-08-07 11:06:00
## 8: rnorm 2018-08-07 11:06:00
## 9: 952 2018-08-07 11:06:00
## 10: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 11: process_file 2018-08-07 11:06:00
## 12: process_group 2018-08-07 11:06:00
## 13: process_group.block 2018-08-07 11:06:00
## 14: call_block 2018-08-07 11:06:00
## 15: block_exec 2018-08-07 11:06:00
## 16: n:4ae3e6b6364de42fdc243469d73448cc 2018-08-07 11:06:00
## 17: mean:c28b87a0be6a99966bdaa5e556974b43 2018-08-07 11:06:00
## 18: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-08-07 11:06:00
## 19: rda 2018-08-07 11:06:00
## 20: dd943b69f9017b3af1a0f6da95936123 2018-08-07 11:06:00
## 21: numeric 2018-08-07 11:06:00
## 22: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 23: 44f57deb36c53cd9c395e04c51fea77a 2018-08-07 11:06:00
## 24: outerTag 2018-08-07 11:06:00
## 25: outer 2018-08-07 11:06:00
## 26: 952 2018-08-07 11:06:00
## 27: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 28: process_file 2018-08-07 11:06:00
## 29: process_group 2018-08-07 11:06:00
## 30: process_group.block 2018-08-07 11:06:00
## 31: call_block 2018-08-07 11:06:00
## 32: block_exec 2018-08-07 11:06:00
## 33: n:8128a6180a705341ab7c05cfa945edfb 2018-08-07 11:06:00
## 34: .FUN:62302feda89e19149a56ca40fde725e1 2018-08-07 11:06:00
## 35: rda 2018-08-07 11:06:00
## 36: dfe9fa463103a9de94aaabf0b9bdc8f3 2018-08-07 11:06:00
## 37: numeric 2018-08-07 11:06:00
## 38: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 39: 994d1330fbd961f795ab0dc508271963 2018-08-07 11:06:00
## 40: outerTag 2018-08-07 11:06:00
## 41: inner 2018-08-07 11:06:00
## 42: 952 2018-08-07 11:06:00
## 43: 2018-08-07 11:06:00 2018-08-07 11:06:00
## 44: process_file 2018-08-07 11:06:00
## 45: process_group 2018-08-07 11:06:00
## 46: process_group.block 2018-08-07 11:06:00
## 47: call_block 2018-08-07 11:06:00
## 48: block_exec 2018-08-07 11:06:00
## 49: mean:c28b87a0be6a99966bdaa5e556974b43 2018-08-07 11:06:00
## 50: .FUN:b910401646b09073940de757678db03d 2018-08-07 11:06:00
## 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"
.
### cacheId
set.seed(1)
Cache(rnorm, 1, cacheRepo = tmpdir1)
## [1] -0.6264538
## attr(,"tags")
## [1] "cacheId:23dc247384c1b270f0d36de4bca1b138"
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## 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(,"tags")
## [1] "cacheId:ad184ce64541972b50afd8e7b75f821b"
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
##
## 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(,"tags")
## [1] "cacheId:ad184ce64541972b50afd8e7b75f821b"
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] FALSE
##
## attr(,"call")
## [1] ""
## cleanup
unlink(c("filename.rda", "filename1.rda"))
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.1 Kb
## Selected objects (not including Rasters): 3.1 Kb
## 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.
Cache(<functionName>, <other arguments>)
This will allow fine scale control of individual function calls.