Cache method that accommodates environments, S4 methods, Rasters
Cache(FUN, ..., notOlderThan = NULL, objects = NULL, outputObjects = NULL, algo = "xxhash64", cacheRepo = NULL, compareRasterFileLength = 1e+06, userTags = c(), digestPathContent = FALSE, omitArgs = NULL, classOptions = list(), debugCache = character(), sideEffect = FALSE, makeCopy = FALSE, quick = FALSE) # S4 method for ANY Cache(FUN, ..., notOlderThan = NULL, objects = NULL, outputObjects = NULL, algo = "xxhash64", cacheRepo = NULL, compareRasterFileLength = 1e+06, userTags = c(), digestPathContent = FALSE, omitArgs = NULL, classOptions = list(), debugCache = character(), sideEffect = FALSE, makeCopy = FALSE, quick = FALSE)
FUN | A function to be called. |
---|---|
... | Arguments of |
notOlderThan | load an artifact from the database only if it was created after notOlderThan. |
objects | Character vector of objects to be digested. This is only applicable if there is a list, environment or simList with named objects within it. Only this/these objects will be considered for caching, i.e., only use a subset of the list, environment or simList objects. |
outputObjects | Optional character vector indicating which objects to
return. This is only relevant for |
algo | The algorithms to be used; currently available choices are
|
cacheRepo | A repository used for storing cached objects.
This is optional if |
compareRasterFileLength | Numeric. Optional. When there are Rasters, that
have file-backed storage, this is passed to the length arg in |
userTags | A character vector with Tags. These Tags will be added to the repository along with the artifact. |
digestPathContent | Logical. Should arguments that are of class |
omitArgs | Optional character string of arguments in the FUN to omit from the digest. |
classOptions | Optional list. This will pass into |
debugCache | Character or Logical. Either |
sideEffect | Logical. Check if files to be downloaded are found locally
in the |
makeCopy | Logical. If |
quick | Logical. If |
As with cache
, returns the value of the
function call or the cached version (i.e., the result from a previous call
to this same cached function with identical arguments).
Caching R objects using cache
has four important limitations:
the archivist
package detects different environments as different;
it also does not detect S4 methods correctly due to method inheritance;
it does not detect objects that have file-base storage of information
(specifically RasterLayer-class
objects);
the default hashing algorithm is relatively slow.
This version of the Cache
function accommodates those four special,
though quite common, cases by:
converting any environments into list equivalents;
identifying the dispatched S4 method (including those made through inheritance) before hashing so the correct method is being cached;
by hashing the linked file, rather than the Raster object.
Currently, only file-backed Raster*
objects are digested
(e.g., not ff
objects, or any other R object where the data
are on disk instead of in RAM);
using fastdigest
internally when the object
is in RAM, which can be up to ten times faster than
digest
. Note that file-backed objects are still
hashed using digest
.
If Cache
is called within a SpaDES module, then the cached entry will automatically
get 3 extra userTags
: eventTime
, eventType
, and moduleName
.
These can then be used in clearCache
to selectively remove cached objects
by eventTime
, eventType
or moduleName
.
Cache
will add a tag to the artifact in the database called accessed
,
which will assign the time that it was accessed, either read or write.
That way, artifacts can be shown (using showCache
) or removed (using
clearCache
) selectively, based on their access dates, rather than only
by their creation dates. See example in clearCache
.
Cache
(uppercase C) is used here so that it is not confused with, and does
not mask, the archivist::cache
function.
As indicated above, several objects require pre-treatment before
caching will work as expected. The function .robustDigest
accommodates this.
It is an S4 generic, meaning that developers can produce their own methods for
different classes of objects. Currently, there are methods for several types
of classes. See .robustDigest
.
See .robustDigest
for other specifics for other classes.
If a function has a path argument, there is some ambiguity about what should be done. Possibilities include:
hash the string as is (this will be very system specific, meaning a
Cache
call will not work if copied between systems or directories);
hash the basename(path)
;
hash the contents of the file.
If paths are passed in as is (i.e,. character string), the result will not be predictable.
Instead, one should use the wrapper function asPath(path)
, which sets the
class of the string to a Path
, and one should decide whether one wants
to digest the content of the file (using digestPathContent = TRUE
),
or just the filename ((digestPathContent = FALSE)
). See examples.
In general, it is expected that caching will only be used when stochasticity
is not relevant, or if a user has achieved sufficient stochasticity (e.g., via
sufficient number of calls to experiment
) such that no new explorations
of stochastic outcomes are required. It will also be very useful in a
reproducible workflow.
library(raster)#>#> #>#>#> #>tmpDir <- file.path(tempdir(), "reproducible_examples", "Cache") ## Example 1: basic cache use ranNumsA <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a") ranNumsB <- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:b") showCache(tmpDir, userTags = c("objectName"))#> artifact tagKey #> 1: 5d791310603a3579e3a1753f89e4da70 format #> 2: 5d791310603a3579e3a1753f89e4da70 name #> 3: 5d791310603a3579e3a1753f89e4da70 class #> 4: 5d791310603a3579e3a1753f89e4da70 date #> 5: 5d791310603a3579e3a1753f89e4da70 objectName #> 6: 5d791310603a3579e3a1753f89e4da70 function #> 7: 5d791310603a3579e3a1753f89e4da70 object.size #> 8: 5d791310603a3579e3a1753f89e4da70 accessed #> 9: 5d791310603a3579e3a1753f89e4da70 cacheId #> 10: decbe2d8d61894d086362ed4536cf4df format #> 11: decbe2d8d61894d086362ed4536cf4df name #> 12: decbe2d8d61894d086362ed4536cf4df class #> 13: decbe2d8d61894d086362ed4536cf4df date #> 14: decbe2d8d61894d086362ed4536cf4df objectName #> 15: decbe2d8d61894d086362ed4536cf4df function #> 16: decbe2d8d61894d086362ed4536cf4df object.size #> 17: decbe2d8d61894d086362ed4536cf4df accessed #> 18: decbe2d8d61894d086362ed4536cf4df cacheId #> tagValue createdDate #> 1: rda 2017-09-05 16:33:22 #> 2: Cache 2017-09-05 16:33:22 #> 3: numeric 2017-09-05 16:33:22 #> 4: 2017-09-05 16:33:22 2017-09-05 16:33:22 #> 5: a 2017-09-05 16:33:22 #> 6: rnorm 2017-09-05 16:33:22 #> 7: 528 2017-09-05 16:33:23 #> 8: 2017-09-05 16:33:22 2017-09-05 16:33:23 #> 9: 6c02ed3587ff36e0f232dc296099540b 2017-09-05 16:33:23 #> 10: rda 2017-09-05 16:33:23 #> 11: Cache 2017-09-05 16:33:23 #> 12: numeric 2017-09-05 16:33:23 #> 13: 2017-09-05 16:33:23 2017-09-05 16:33:23 #> 14: b 2017-09-05 16:33:23 #> 15: runif 2017-09-05 16:33:23 #> 16: 528 2017-09-05 16:33:23 #> 17: 2017-09-05 16:33:23 2017-09-05 16:33:23 #> 18: 9dd2494a488858266cd45d230f45a144 2017-09-05 16:33:23#> artifact tagKey #> 1: 5d791310603a3579e3a1753f89e4da70 format #> 2: 5d791310603a3579e3a1753f89e4da70 name #> 3: 5d791310603a3579e3a1753f89e4da70 class #> 4: 5d791310603a3579e3a1753f89e4da70 date #> 5: 5d791310603a3579e3a1753f89e4da70 objectName #> 6: 5d791310603a3579e3a1753f89e4da70 function #> 7: 5d791310603a3579e3a1753f89e4da70 object.size #> 8: 5d791310603a3579e3a1753f89e4da70 accessed #> 9: 5d791310603a3579e3a1753f89e4da70 cacheId #> tagValue createdDate #> 1: rda 2017-09-05 16:33:22 #> 2: Cache 2017-09-05 16:33:22 #> 3: numeric 2017-09-05 16:33:22 #> 4: 2017-09-05 16:33:22 2017-09-05 16:33:22 #> 5: a 2017-09-05 16:33:22 #> 6: rnorm 2017-09-05 16:33:22 #> 7: 528 2017-09-05 16:33:23 #> 8: 2017-09-05 16:33:22 2017-09-05 16:33:23 #> 9: 6c02ed3587ff36e0f232dc296099540b 2017-09-05 16:33:23#> artifact tagKey #> 1: decbe2d8d61894d086362ed4536cf4df format #> 2: decbe2d8d61894d086362ed4536cf4df name #> 3: decbe2d8d61894d086362ed4536cf4df class #> 4: decbe2d8d61894d086362ed4536cf4df date #> 5: decbe2d8d61894d086362ed4536cf4df objectName #> 6: decbe2d8d61894d086362ed4536cf4df function #> 7: decbe2d8d61894d086362ed4536cf4df object.size #> 8: decbe2d8d61894d086362ed4536cf4df accessed #> 9: decbe2d8d61894d086362ed4536cf4df cacheId #> tagValue createdDate #> 1: rda 2017-09-05 16:33:23 #> 2: Cache 2017-09-05 16:33:23 #> 3: numeric 2017-09-05 16:33:23 #> 4: 2017-09-05 16:33:23 2017-09-05 16:33:23 #> 5: b 2017-09-05 16:33:23 #> 6: runif 2017-09-05 16:33:23 #> 7: 528 2017-09-05 16:33:23 #> 8: 2017-09-05 16:33:23 2017-09-05 16:33:23 #> 9: 9dd2494a488858266cd45d230f45a144 2017-09-05 16:33:23clearCache(tmpDir, userTags = c("runif")) # remove only cached objects made during runif call showCache(tmpDir) # only those made during rnorm call#> artifact tagKey #> 1: 5d791310603a3579e3a1753f89e4da70 format #> 2: 5d791310603a3579e3a1753f89e4da70 name #> 3: 5d791310603a3579e3a1753f89e4da70 class #> 4: 5d791310603a3579e3a1753f89e4da70 date #> 5: 5d791310603a3579e3a1753f89e4da70 objectName #> 6: 5d791310603a3579e3a1753f89e4da70 function #> 7: 5d791310603a3579e3a1753f89e4da70 object.size #> 8: 5d791310603a3579e3a1753f89e4da70 accessed #> 9: 5d791310603a3579e3a1753f89e4da70 cacheId #> tagValue createdDate #> 1: rda 2017-09-05 16:33:22 #> 2: Cache 2017-09-05 16:33:22 #> 3: numeric 2017-09-05 16:33:22 #> 4: 2017-09-05 16:33:22 2017-09-05 16:33:22 #> 5: a 2017-09-05 16:33:22 #> 6: rnorm 2017-09-05 16:33:22 #> 7: 528 2017-09-05 16:33:23 #> 8: 2017-09-05 16:33:22 2017-09-05 16:33:23 #> 9: 6c02ed3587ff36e0f232dc296099540b 2017-09-05 16:33:23clearCache(tmpDir) ## Example 2: using the "accessed" tag ranNumsA <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a") ranNumsB <- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:b") # access it again, but "later" Sys.sleep(1) ranNumsA <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a")#>wholeCache <- showCache(tmpDir) # keep only items accessed "recently" (i.e., only objectName:a) onlyRecentlyAccessed <- showCache(tmpDir, userTags = max(wholeCache[tagKey == "accessed"]$tagValue)) # 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 showCache(tmpDir) # still has more recently accessed#> artifact tagKey #> 1: 1859dfdf3198d6377afbb546cdc9723c format #> 2: 1859dfdf3198d6377afbb546cdc9723c name #> 3: 1859dfdf3198d6377afbb546cdc9723c class #> 4: 1859dfdf3198d6377afbb546cdc9723c date #> 5: 1859dfdf3198d6377afbb546cdc9723c objectName #> 6: 1859dfdf3198d6377afbb546cdc9723c function #> 7: 1859dfdf3198d6377afbb546cdc9723c object.size #> 8: 1859dfdf3198d6377afbb546cdc9723c accessed #> 9: 1859dfdf3198d6377afbb546cdc9723c cacheId #> 10: 1859dfdf3198d6377afbb546cdc9723c accessed #> tagValue createdDate #> 1: rda 2017-09-05 16:33:23 #> 2: Cache 2017-09-05 16:33:23 #> 3: numeric 2017-09-05 16:33:23 #> 4: 2017-09-05 16:33:23 2017-09-05 16:33:23 #> 5: a 2017-09-05 16:33:23 #> 6: rnorm 2017-09-05 16:33:23 #> 7: 528 2017-09-05 16:33:23 #> 8: 2017-09-05 16:33:23 2017-09-05 16:33:23 #> 9: 6c02ed3587ff36e0f232dc296099540b 2017-09-05 16:33:23 #> 10: 2017-09-05 16:33:24 2017-09-05 16:33:24clearCache(tmpDir) ## Example 3: using keepCache 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)#> artifact tagKey #> 1: 832ac945443ee5a6db5d0f4f6a7703f1 format #> 2: 832ac945443ee5a6db5d0f4f6a7703f1 name #> 3: 832ac945443ee5a6db5d0f4f6a7703f1 class #> 4: 832ac945443ee5a6db5d0f4f6a7703f1 date #> 5: 832ac945443ee5a6db5d0f4f6a7703f1 objectName #> 6: 832ac945443ee5a6db5d0f4f6a7703f1 function #> 7: 832ac945443ee5a6db5d0f4f6a7703f1 object.size #> 8: 832ac945443ee5a6db5d0f4f6a7703f1 accessed #> 9: 832ac945443ee5a6db5d0f4f6a7703f1 cacheId #> 10: 8f71a97709b2d4630bfc1e9051803cc6 format #> 11: 8f71a97709b2d4630bfc1e9051803cc6 name #> 12: 8f71a97709b2d4630bfc1e9051803cc6 class #> 13: 8f71a97709b2d4630bfc1e9051803cc6 date #> 14: 8f71a97709b2d4630bfc1e9051803cc6 objectName #> 15: 8f71a97709b2d4630bfc1e9051803cc6 function #> 16: 8f71a97709b2d4630bfc1e9051803cc6 object.size #> 17: 8f71a97709b2d4630bfc1e9051803cc6 accessed #> 18: 8f71a97709b2d4630bfc1e9051803cc6 cacheId #> tagValue createdDate #> 1: rda 2017-09-05 16:33:24 #> 2: Cache 2017-09-05 16:33:24 #> 3: numeric 2017-09-05 16:33:24 #> 4: 2017-09-05 16:33:24 2017-09-05 16:33:24 #> 5: b 2017-09-05 16:33:24 #> 6: runif 2017-09-05 16:33:24 #> 7: 528 2017-09-05 16:33:24 #> 8: 2017-09-05 16:33:24 2017-09-05 16:33:24 #> 9: 9dd2494a488858266cd45d230f45a144 2017-09-05 16:33:24 #> 10: rda 2017-09-05 16:33:24 #> 11: Cache 2017-09-05 16:33:24 #> 12: numeric 2017-09-05 16:33:24 #> 13: 2017-09-05 16:33:24 2017-09-05 16:33:24 #> 14: a 2017-09-05 16:33:24 #> 15: rnorm 2017-09-05 16:33:24 #> 16: 528 2017-09-05 16:33:24 #> 17: 2017-09-05 16:33:24 2017-09-05 16:33:24 #> 18: 6c02ed3587ff36e0f232dc296099540b 2017-09-05 16:33:24#> artifact tagKey #> 1: 8f71a97709b2d4630bfc1e9051803cc6 format #> 2: 8f71a97709b2d4630bfc1e9051803cc6 name #> 3: 8f71a97709b2d4630bfc1e9051803cc6 class #> 4: 8f71a97709b2d4630bfc1e9051803cc6 date #> 5: 8f71a97709b2d4630bfc1e9051803cc6 objectName #> 6: 8f71a97709b2d4630bfc1e9051803cc6 function #> 7: 8f71a97709b2d4630bfc1e9051803cc6 object.size #> 8: 8f71a97709b2d4630bfc1e9051803cc6 accessed #> 9: 8f71a97709b2d4630bfc1e9051803cc6 cacheId #> tagValue createdDate #> 1: rda 2017-09-05 16:33:24 #> 2: Cache 2017-09-05 16:33:24 #> 3: numeric 2017-09-05 16:33:24 #> 4: 2017-09-05 16:33:24 2017-09-05 16:33:24 #> 5: a 2017-09-05 16:33:24 #> 6: rnorm 2017-09-05 16:33:24 #> 7: 528 2017-09-05 16:33:24 #> 8: 2017-09-05 16:33:24 2017-09-05 16:33:24 #> 9: 6c02ed3587ff36e0f232dc296099540b 2017-09-05 16:33:24# Remove all Cache items that happened within a rnorm() call clearCache(tmpDir, userTags = "rnorm") showCache(tmpDir) ## empty#> Empty data.table (0 rows) of 3 cols: md5hash,name,createdDate## Example 4: searching for multiple objects in the cache # 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)#> artifact tagKey #> 1: 272069fa0f28e15171fc3970210c7526 format #> 2: 272069fa0f28e15171fc3970210c7526 name #> 3: 272069fa0f28e15171fc3970210c7526 class #> 4: 272069fa0f28e15171fc3970210c7526 date #> 5: 272069fa0f28e15171fc3970210c7526 objectName #> 6: 272069fa0f28e15171fc3970210c7526 function #> 7: 272069fa0f28e15171fc3970210c7526 object.size #> 8: 272069fa0f28e15171fc3970210c7526 accessed #> 9: 272069fa0f28e15171fc3970210c7526 cacheId #> 10: f0832b8cd870cc65eea28f97be3c4f15 format #> 11: f0832b8cd870cc65eea28f97be3c4f15 name #> 12: f0832b8cd870cc65eea28f97be3c4f15 class #> 13: f0832b8cd870cc65eea28f97be3c4f15 date #> 14: f0832b8cd870cc65eea28f97be3c4f15 objectName #> 15: f0832b8cd870cc65eea28f97be3c4f15 function #> 16: f0832b8cd870cc65eea28f97be3c4f15 object.size #> 17: f0832b8cd870cc65eea28f97be3c4f15 accessed #> 18: f0832b8cd870cc65eea28f97be3c4f15 cacheId #> tagValue createdDate #> 1: rda 2017-09-05 16:33:25 #> 2: Cache 2017-09-05 16:33:25 #> 3: numeric 2017-09-05 16:33:25 #> 4: 2017-09-05 16:33:25 2017-09-05 16:33:25 #> 5: b 2017-09-05 16:33:25 #> 6: rnorm 2017-09-05 16:33:25 #> 7: 528 2017-09-05 16:33:25 #> 8: 2017-09-05 16:33:25 2017-09-05 16:33:25 #> 9: 6c02ed3587ff36e0f232dc296099540b 2017-09-05 16:33:25 #> 10: rda 2017-09-05 16:33:25 #> 11: Cache 2017-09-05 16:33:25 #> 12: numeric 2017-09-05 16:33:25 #> 13: 2017-09-05 16:33:25 2017-09-05 16:33:25 #> 14: a 2017-09-05 16:33:25 #> 15: runif 2017-09-05 16:33:25 #> 16: 528 2017-09-05 16:33:25 #> 17: 2017-09-05 16:33:24 2017-09-05 16:33:25 #> 18: 9dd2494a488858266cd45d230f45a144 2017-09-05 16:33:25# 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#> 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")#> artifact tagKey #> 1: 272069fa0f28e15171fc3970210c7526 format #> 2: 272069fa0f28e15171fc3970210c7526 name #> 3: 272069fa0f28e15171fc3970210c7526 class #> 4: 272069fa0f28e15171fc3970210c7526 date #> 5: 272069fa0f28e15171fc3970210c7526 objectName #> 6: 272069fa0f28e15171fc3970210c7526 function #> 7: 272069fa0f28e15171fc3970210c7526 object.size #> 8: 272069fa0f28e15171fc3970210c7526 accessed #> 9: 272069fa0f28e15171fc3970210c7526 cacheId #> 10: f0832b8cd870cc65eea28f97be3c4f15 format #> 11: f0832b8cd870cc65eea28f97be3c4f15 name #> 12: f0832b8cd870cc65eea28f97be3c4f15 class #> 13: f0832b8cd870cc65eea28f97be3c4f15 date #> 14: f0832b8cd870cc65eea28f97be3c4f15 objectName #> 15: f0832b8cd870cc65eea28f97be3c4f15 function #> 16: f0832b8cd870cc65eea28f97be3c4f15 object.size #> 17: f0832b8cd870cc65eea28f97be3c4f15 accessed #> 18: f0832b8cd870cc65eea28f97be3c4f15 cacheId #> tagValue createdDate #> 1: rda 2017-09-05 16:33:25 #> 2: Cache 2017-09-05 16:33:25 #> 3: numeric 2017-09-05 16:33:25 #> 4: 2017-09-05 16:33:25 2017-09-05 16:33:25 #> 5: b 2017-09-05 16:33:25 #> 6: rnorm 2017-09-05 16:33:25 #> 7: 528 2017-09-05 16:33:25 #> 8: 2017-09-05 16:33:25 2017-09-05 16:33:25 #> 9: 6c02ed3587ff36e0f232dc296099540b 2017-09-05 16:33:25 #> 10: rda 2017-09-05 16:33:25 #> 11: Cache 2017-09-05 16:33:25 #> 12: numeric 2017-09-05 16:33:25 #> 13: 2017-09-05 16:33:25 2017-09-05 16:33:25 #> 14: a 2017-09-05 16:33:25 #> 15: runif 2017-09-05 16:33:25 #> 16: 528 2017-09-05 16:33:25 #> 17: 2017-09-05 16:33:24 2017-09-05 16:33:25 #> 18: 9dd2494a488858266cd45d230f45a144 2017-09-05 16:33:25# keep only objects that are either runif or rnorm ("or" search) keepCache(tmpDir, userTags = "runif|rnorm")#> artifact tagKey #> 1: 272069fa0f28e15171fc3970210c7526 format #> 2: 272069fa0f28e15171fc3970210c7526 name #> 3: 272069fa0f28e15171fc3970210c7526 class #> 4: 272069fa0f28e15171fc3970210c7526 date #> 5: 272069fa0f28e15171fc3970210c7526 objectName #> 6: 272069fa0f28e15171fc3970210c7526 function #> 7: 272069fa0f28e15171fc3970210c7526 object.size #> 8: 272069fa0f28e15171fc3970210c7526 accessed #> 9: 272069fa0f28e15171fc3970210c7526 cacheId #> 10: f0832b8cd870cc65eea28f97be3c4f15 format #> 11: f0832b8cd870cc65eea28f97be3c4f15 name #> 12: f0832b8cd870cc65eea28f97be3c4f15 class #> 13: f0832b8cd870cc65eea28f97be3c4f15 date #> 14: f0832b8cd870cc65eea28f97be3c4f15 objectName #> 15: f0832b8cd870cc65eea28f97be3c4f15 function #> 16: f0832b8cd870cc65eea28f97be3c4f15 object.size #> 17: f0832b8cd870cc65eea28f97be3c4f15 accessed #> 18: f0832b8cd870cc65eea28f97be3c4f15 cacheId #> tagValue createdDate #> 1: rda 2017-09-05 16:33:25 #> 2: Cache 2017-09-05 16:33:25 #> 3: numeric 2017-09-05 16:33:25 #> 4: 2017-09-05 16:33:25 2017-09-05 16:33:25 #> 5: b 2017-09-05 16:33:25 #> 6: rnorm 2017-09-05 16:33:25 #> 7: 528 2017-09-05 16:33:25 #> 8: 2017-09-05 16:33:25 2017-09-05 16:33:25 #> 9: 6c02ed3587ff36e0f232dc296099540b 2017-09-05 16:33:25 #> 10: rda 2017-09-05 16:33:25 #> 11: Cache 2017-09-05 16:33:25 #> 12: numeric 2017-09-05 16:33:25 #> 13: 2017-09-05 16:33:25 2017-09-05 16:33:25 #> 14: a 2017-09-05 16:33:25 #> 15: runif 2017-09-05 16:33:25 #> 16: 528 2017-09-05 16:33:25 #> 17: 2017-09-05 16:33:24 2017-09-05 16:33:25 #> 18: 9dd2494a488858266cd45d230f45a144 2017-09-05 16:33:25clearCache(tmpDir) ## Example 5: using caching to speed up rerunning expensive computations ras <- raster(extent(0, 100, 0, 100), res = 1, vals = sample(1:5, replace = TRUE, size = 1e4), 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 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) )#># recovered cached version is same as non-cached version all.equal(notCached, reRun) ## TRUE#> [1] TRUE## Example 6: working with file paths # if passing a character string, it will take 2 complete passes to before # a cached copy is used when it is a save event (read or load is different) obj <- 1:10 fname <- tempfile(fileext = ".RData") Cache(saveRDS, obj, file = fname, cacheRepo = tmpDir)#> NULLCache(saveRDS, obj, file = fname, cacheRepo = tmpDir)#> NULLCache(saveRDS, obj, file = fname, cacheRepo = tmpDir) # cached copy is loaded#>#> [1] "NULL" #> attr(,"tags") #> [1] "cacheId:46db9ac49c0d1d44463ac3c5dfe3119a" #> attr(,"call") #> [1] ""# however, using asPath(), cached version retrieved after being run once fname1 <- tempfile(fileext = ".RData") Cache(saveRDS, obj, file = asPath(fname1), cacheRepo = tmpDir)#> NULL#>#> [1] "NULL" #> attr(,"tags") #> [1] "cacheId:e41f2fe323f8a5fa2ca2002f099a7976" #> attr(,"call") #> [1] ""clearCache(tmpDir) ## cleanup unlink(c("filename.rda", "filename1.rda")) unlink(dirname(tmpDir), recursive = TRUE)