Load the packages, environment, and random number generator seed of a target.
Usage
tar_workspace(
name,
envir = parent.frame(),
packages = TRUE,
source = TRUE,
script = targets::tar_config_get("script"),
store = targets::tar_config_get("store")
)Arguments
- name
Symbol, name of the target whose workspace to read.
- envir
Environment in which to put the objects.
- packages
Logical, whether to load the required packages of the target.
- source
Logical, whether to run
_targets.Rto load user-defined global object dependencies intoenvir. IfTRUE, thenenvirshould either be the global environment or inherit from the global environment.- script
Character of length 1, path to the target script file. Defaults to
tar_config_get("script"), which in turn defaults to_targets.R. When you set this argument, the value oftar_config_get("script")is temporarily changed for the current function call. Seetar_script(),tar_config_get(), andtar_config_set()for details about the target script file and how to set it persistently for a project.- store
Character of length 1, path to the
targetsdata store. Defaults totar_config_get("store"), which in turn defaults to_targets/. When you set this argument, the value oftar_config_get("store")is temporarily changed for the current function call. Seetar_config_get()andtar_config_set()for details about how to set the data store path persistently for a project.
Value
This function returns NULL, but it does load
the target's required packages, as well as multiple objects
into the environment (envir argument) in order to replicate the
workspace where the error happened. These objects include
the global objects at the time tar_make() was called and the
dependency targets. The random number generator seed for the
target is also assigned with tar_seed_set().
Details
If you activate workspaces through the workspaces argument
of tar_option_set(), then under the circumstances you specify,
targets will save a special workspace file to a location in
in _targets/workspaces/. The workspace file is a compact reference
that allows tar_workspace() to load the target's dependencies
and random number generator seed as long as the data objects
are still in the data store (usually files in _targets/objects/).
When you are done debugging, you can remove the workspace files
using tar_destroy(destroy = "workspaces").
If tar_option_get("repository_meta") is "aws" or "gcp", then
tar_make() uploads workspaces to the bucket and prefix provided.
Download one of these workspaces with tar_workspace_download().
Downloaded workspaces can be loaded the usual way with
tar_workspace(), and you should see them in
character vector returned by tar_workspaces().
Trusting custom storage code
tar_format() and tar_repository_cas() save your custom
functions as text in the data store: in the metadata
(_targets/meta/meta) and in any saved workspaces
(_targets/workspaces/). targets runs that code again
whenever it reads those targets. If you get a data store
from someone else, the code in its metadata
could do anything, and you cannot see it in _targets.R.
For this reason, if the targets being read use a custom format or a
CAS repository, tar_read(), tar_load(), and tar_workspace()
first ask whether you trust the data store.
In a non-interactive session, they throw an error instead.
To skip the check, e.g. on a continuous integration server
or in a project you trust, set the TAR_ASK environment
variable to "false" (see tar_envvars()).
The pipeline itself never asks. tar_make() and its relatives
already work from the existing data store, so running the pipeline
is a choice to trust it. If you do not trust a data store,
delete or move it and run the pipeline from scratch. Then every
custom function in the new metadata comes from your own _targets.R.
Likewise, calls to tar_read() and tar_load() inside the
pipeline (for example, in a report) are your own code,
in plain sight rather than hidden in the metadata,
so you are responsible for them like any other code
you write. A data store you cannot rebuild is different:
the code hidden in its metadata is the only way to read the data,
which is why reading it from outside the pipeline asks first.
R CMD check is also exempt. It runs package examples, tests,
and vignettes that the package developer wrote and chose to run,
and it is never interactive, so asking would only make
checks fail on CRAN.
Even ordinary storage formats like "rds" and "qs" can hold
R objects such as functions, so treat any data store you get
from someone else with the same care as code from that person.
See also
Other debug:
tar_load_globals(),
tar_traceback(),
tar_workspace_download(),
tar_workspaces()
Examples
if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) { # for CRAN
tar_dir({ # tar_dir() runs code from a temp dir for CRAN.
tmp <- sample(1)
tar_script({
library(targets)
library(tarchetypes)
list(
tar_target(x, "loaded"),
tar_target(y, stop(x))
)
}, ask = FALSE)
# The following code throws an error for demonstration purposes.
try(tar_make())
exists("x") # Should be FALSE.
tail(.Random.seed) # for comparison to the RNG state after tar_workspace(y)
tar_workspace(y)
exists("x") # Should be TRUE.
print(x) # "loaded"
# Should be different: tar_workspace() runs
# tar_seed_set(tar_meta(y, seed)$seed)
tail(.Random.seed)
})
}