# NOT RUN {
isolate_example("contain side effects", {
# Suppose you have a workflow that uses the `digest()` function,
# which computes the hash of an object.
library(digest) # Has the digest() function.
g <- function(x) {
digest(x)
}
f <- function(x) {
g(x)
}
plan <- drake_plan(x = f(1))
cache <- storr::storr_environment() # optional
# Here are the reproducibly tracked objects in the workflow.
config <- drake_config(plan, cache = cache, history = FALSE)
tracked(config)
# But the digest() function has dependencies too.
head(deps_code(digest))
# Why doesn't `drake` import them? Because it knows `digest()`
# is from a package, and it doesn't usually dive into functions
# from packages. We need to call expose_imports() to expose
# a package's inner functions.
expose_imports(digest)
config <- drake_config(plan, cache = cache, history = FALSE)
new_objects <- tracked(config)
head(new_objects, 10)
length(new_objects)
# Now when you call `make()`, `drake` will dive into `digest`
# to import dependencies.
make(plan, cache = cache, history = FALSE)
head(cached(cache = cache), 10)
length(cached(cache = cache))
# Why would you want to expose a whole package like this?
# Because you may want to wrap up your data science project
# as a formal R package. In that case, `expose_imports()`
# tells `drake` to reproducibly track all of your code,
# not just the exported API functions you mention in
# workflow plan commands.
# Note: if you use `digest::digest()`` instead of just `digest()`,
# `drake` does not dive into the function body anymore.
g <- function(x) {
digest::digest(x) # Was previously just digest()
}
config <- drake_config(plan, cache = cache, history = FALSE)
tracked(config)
})
# }
Run the code above in your browser using DataLab