More complex resources are typically time-consuming to compile, or are unnecessary to compile more than once per R session.
caching_layer(object, ..., recompile. = FALSE)
object | active_resource. See |
---|---|
... | additional parameters to pass to the next layer in the resource parsing tower. |
recompile. | logical. Whether or not to force the resource to be recompiled (instead of retrieved from cache), regardless of whether the resource or any of its dependencies have been modified. |
The parsed resource, retrieved from cache since the last time the resource was executed if and only if the resource's file(s) and none of its (recursive) dependencies have been modified.
The caching_layer
provides an internal caching mechanism that
will remember the value of the parsed resource and re-use it
unless the resource or any of its dependencies have been
modified (see dependency_tracking for an explanation of
how this is accomplished).
The parameters must be named object
and ...
due to
this method's inclusion in a tower
.
active_resource
, tower
not_run({ # Imagine we are constructing a stagerunner from a sequence of functions. # However, some of those functions have been built by other resources. # Imagine the following structure. # (See github.com/robertzk/stagerunner for an explanation of stagerunners.) #=== /dir/runners/project1.R === list( "import data" = resource("importers/db"), # These are some functions "munge data" = resource("mungers/impute"), # built by the user "create model" = resource("models/lm"), # that live in other "export model" = resource("exporters/file") # files. ) #=== R console === d <- director("/dir") # Create a director object. d$register_parser("runners/", function(output) { stagerunner::stageRunner$new(new.env(), output) }, cache = TRUE) # Note the cache = TRUE argument. sr <- d$resource("runners/project1") # A fresh new stageRunner! sr2 <- d$resource("runners/project1") # Same one, since it used the cache. stopifnot(identical(sr, sr2)) # We can use base::Sys.setFileTime to pretend like we updated the # modified time of the /dir/connections/dev.R file, triggering # the caching layer to re-compile the resource. Sys.setFileTime(file.path(d$root(), "runners", "project1.R"), Sys.time() - as.difftime(1, units = "mins")) sr3 <- d$resource("runners/project1") # Now it re-builds the runner. stopifnot(!identical(sr, sr3)) # A new runner, with hardly any work! })