Get a target's value from archive storage, building first if needed
Source:R/tar_get_archive.R
tar_get_archive.RdUsage
tar_get_archive(
name,
package,
pipeline,
branches = NULL,
meta = NULL,
reporter = "silent",
callr_function = callr::r,
callr_arguments = targets::tar_callr_args_default(callr_function, reporter),
envir = parent.frame(),
store = targets::tar_config_get("store")
)
tar_get_archive_raw(
name,
package,
pipeline,
branches = NULL,
meta = NULL,
reporter = "silent",
callr_function = callr::r,
callr_arguments = targets::tar_callr_args_default(callr_function, reporter),
envir = parent.frame(),
store = targets::tar_config_get("store")
)Arguments
- name
Name of the target to read.
tar_read()expects an unevaluated symbol for thenameargument, whereastar_read_raw()expects a character string.- package
A scalar character of the package name.
- pipeline
A scalar character of the pipeline name.
- branches
Integer of indices of the branches to load if the target is a pattern.
- meta
Data frame of metadata from
tar_meta().tar_read()with the default arguments can be inefficient for large pipelines because all the metadata is stored in a single file. However, if you calltar_meta()beforehand and supply it to themetaargument, then successive calls totar_read()may run much faster.- reporter
Character of length 1, name of the reporter to user. Controls how messages are printed as targets run in the pipeline.
The default value of
reporteris the value returned bytar_config_get("reporter_make"). The default oftar_config_get("reporter_make")is"terse"if the calling R session is either:1. Non-interactive (`interactive()` returns `FALSE`), or 2. Inside a literate programming document (the `knitr.in.progress` global option is `TRUE`).Otherwise, the default is
"balanced". You can always set the reporter manually. Choices:"balanced": a reporter that balances efficiency with informative detail. Uses acliprogress bar instead of printing messages for individual dynamic branches. To the right of the progress bar is a text string like "22.6s, 4510+, 124-" (22.6 seconds elapsed, 4510 targets completed successfully so far, 124 targets skipped so far).For best results with the balanced reporter, you may need to adjust your
clisettings. See global optionscli.num_colorsandcli.dynamicat https://cli.r-lib.org/reference/cli-config.html. On that page is also theCLI_TICK_TIMEenvironment variable which controls the time delay between progress bar updates. If the delay is too low, then overhead from printing to the console may slow down the pipeline."terse": like the"balanced"reporter, but without a progress bar."silent": print nothing."timestamp": same as the"verbose"reporter except that each message begins with a time stamp."verbose": print messages for individual targets as they dispatch or complete. Each individual target-specific time (e.g. "3.487 seconds") is strictly the elapsed runtime of the target and does not include steps like data retrieval and output storage.
- callr_function
A function from
callrto start a fresh clean R process to do the work. Set toNULLto run in the current session instead of an external process (but restart your R session just before you do in order to clear debris out of the global environment).callr_functionneeds to beNULLfor interactive debugging, e.g.tar_option_set(debug = "your_target"). However,callr_functionshould not beNULLfor serious reproducible work.- callr_arguments
A list of arguments to
callr_function.- envir
An environment used to resolve
package, so that a package currently loaded withpkgload::load_all()(e.g. during interactive package development) is resolved to its source directory instead of its installed one. Defaults to the calling environment.- 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
The target's return value from its file in
_targets/objects/, or the paths to the custom files and directories
if format = "file" was set.
Details
Combines tar_make_archive() and tar_read_archive_raw(): builds name
unless it has already been confirmed up to date for the installed version
of package earlier in the same R session, then reads it.
tar_get_archive() captures name with non-standard evaluation, whereas
tar_get_archive_raw() takes it as a character string.
Unlike tar_read_archive() – which, mirroring targets::tar_read(),
only ever reads and errors if the store doesn't exist yet –
tar_get_archive() is for callers that just want the current data without
separately calling tar_make_archive() themselves first. The build (when
needed) runs in a callr subprocess by default, same as
tar_make_archive() itself, so a crash or state leak (loaded packages,
changed options()) during the build can't take down the caller's own R
session. This only runs at most once per name per installed version of
package per session, so that isolation is cheap to keep; pass
callr_function = NULL yourself only if you understand and accept running
the build in-process.
Examples
# \donttest{
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
tar_get_archive(model, package = "tarchives", pipeline = "example-model")
)
#>
#> Call:
#> lm(formula = Sepal.Width ~ Sepal.Length, data = data)
#>
#> Coefficients:
#> (Intercept) Sepal.Length
#> 1.131 0.278
#>
# }