| Title: | Make Your 'targets' Pipelines into a Package |
| Version: | 0.3.0 |
| Description: | Runs 'targets' pipelines bundled inside a package and caches the results in the R user cache directory, so that users of the package do not need to rerun the pipeline themselves. Package authors can update the cached results at any time by releasing a new package version. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| Imports: | callr, cli, curl, filelock, fs, lifecycle, rlang (≥ 1.1.0), targets, withr |
| Suggests: | testthat (≥ 3.0.0), webfakes |
| Config/testthat/edition: | 3 |
| Depends: | R (≥ 4.0.0) |
| URL: | https://github.com/UchidaMizuki/tarchives, https://uchidamizuki.com/tarchives/ |
| BugReports: | https://github.com/UchidaMizuki/tarchives/issues |
| Config/roxygen2/version: | 8.0.0 |
| NeedsCompilation: | no |
| Packaged: | 2026-11-29 15:06:23 UTC; uchid |
| Author: | Mizuki Uchida |
| Maintainer: | Mizuki Uchida <dev@uchidamizuki.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-11-30 08:50:02 UTC |
tarchives: Make Your 'targets' Pipelines into a Package
Description
Runs 'targets' pipelines bundled inside a package and caches the results in the R user cache directory, so that users of the package do not need to rerun the pipeline themselves. Package authors can update the cached results at any time by releasing a new package version.
Author(s)
Maintainer: Mizuki Uchida dev@uchidamizuki.com (ORCID) [copyright holder]
Authors:
Mizuki Uchida dev@uchidamizuki.com (ORCID) [copyright holder]
See Also
Useful links:
Report bugs at https://github.com/UchidaMizuki/tarchives/issues
Function factory for archived targets
Description
Function factory for archived targets
Usage
tar_archive(
f,
package,
pipeline,
envir = parent.frame(),
script = targets::tar_config_get("script"),
store = targets::tar_config_get("store")
)
Arguments
f |
A function of targets package. |
package |
A scalar character of the package name. |
pipeline |
A scalar character of the pipeline name. |
envir |
An environment, where to run the target R script
(default: The |
script |
Character of length 1, path to the
target script file. Defaults to |
store |
Character of length 1, path to the
|
Value
A function.
Examples
tar_outdated_archive <- tar_archive(
targets::tar_outdated,
package = "tarchives",
pipeline = "example-model"
)
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
tar_outdated_archive()
)
List the archived pipelines in a package
Description
Returns the names of the pipelines bundled in a package's
inst/tarchives directory. Use it to discover which pipelines are
available before running them with tar_make_archive() or inspecting their
targets with tar_manifest_archive().
Usage
tar_archive_pipelines(
package,
envir = parent.frame(),
script = targets::tar_config_get("script")
)
Arguments
package |
A scalar character of the package name. |
envir |
An environment used to resolve |
script |
Character of length 1, path to the
target script file. Defaults to |
Value
A character vector of pipeline names. A pipeline is a directory in
inst/tarchives that contains a target script file (script), so the
shared R/ helper directory is not included.
See Also
tar_manifest_archive() to list the targets within a pipeline.
Examples
tar_archive_pipelines(package = "tarchives")
Path to the archived target script file
Description
Path to the archived target script file
Usage
tar_archive_script(
package,
pipeline,
envir = parent.frame(),
script = targets::tar_config_get("script")
)
Arguments
package |
A scalar character of the package name. |
pipeline |
A scalar character of the pipeline name. |
envir |
An environment used to resolve |
script |
Character of length 1, path to the
target script file. Defaults to |
Value
A scalar character of the path to the archived target script file.
Examples
tar_archive_script(package = "tarchives", pipeline = "example-model")
Path to the archived target store directory
Description
Path to the archived target store directory
Usage
tar_archive_store(package, pipeline, store = targets::tar_config_get("store"))
Arguments
package |
A scalar character of the package name. |
pipeline |
A scalar character of the pipeline name. |
store |
Character of length 1, path to the
|
Value
A scalar character of the path to the archived target store directory.
Examples
tar_archive_store(package = "tarchives", pipeline = "example-model")
Check a pipeline or target name
Description
Validates pipeline against the pipelines bundled in package (see
tar_archive_pipelines()), or name against the targets defined in a
pipeline (see tar_manifest_archive()). Errors with a "did you mean"
suggestion when the value doesn't match, so callers don't have to
reimplement this check themselves.
Usage
tar_check_archive_pipeline(
pipeline,
package,
envir = parent.frame(),
arg_nm = rlang::caller_arg(pipeline),
error_call = rlang::caller_env()
)
tar_check_archive_name(
name,
package,
pipeline,
envir = parent.frame(),
arg_nm = rlang::caller_arg(name),
error_call = rlang::caller_env()
)
Arguments
pipeline, name |
A scalar character of the pipeline or target name to check. |
package |
A scalar character of the package name. |
envir |
An environment used to resolve |
arg_nm, error_call |
Passed to |
Details
tar_check_archive_name() reads the pipeline's target script in a callr
subprocess, so anything the script does – such as calling
targets::tar_option_set() or attaching packages – can't affect the
caller's R session. This makes it safe to call from a function used in a
user's own target script. The target names are cached for the rest of the
session per version of package, so only the first check of each pipeline
pays the subprocess startup cost. Checks run by the target script itself
(e.g. when one data package's pipeline uses another's targets) are skipped
while its names are read, so the cost doesn't grow with the nesting depth.
Value
pipeline or name, invisibly, if valid. Otherwise, throws an
error.
See Also
tar_archive_pipelines() to list the pipelines in a package, and
tar_manifest_archive() to list the targets within a pipeline.
Examples
tar_check_archive_pipeline("example-model", package = "tarchives")
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
tar_check_archive_name(
"data",
package = "tarchives",
pipeline = "example-model"
)
)
Destroy an archived pipeline's storage
Description
tarchives version of targets::tar_destroy(). Removes the cached targets
store of an archived pipeline from the R user cache directory
(tools::R_user_dir("tarchives", "cache")).
Usage
tar_destroy_archive(
package,
pipeline,
destroy = "all",
batch_size = 1000L,
verbose = TRUE,
ask = NULL,
store = targets::tar_config_get("store")
)
Arguments
package |
A scalar character of the package name. |
pipeline |
A scalar character of the pipeline name. |
destroy |
Character of length 1, what to destroy. Choices:
|
batch_size |
Positive integer between 1 and 1000, number of target definition objects to delete from the cloud with each HTTP API request. Currently only supported for AWS. Cannot be more than 1000. |
verbose |
Logical of length 1, whether to print console messages to show progress when deleting each batch of targets from each cloud bucket. Batched deletion with verbosity is currently only supported for AWS. |
ask |
Logical of length 1, whether to pause with a menu prompt
before deleting files. To disable this menu, set the |
store |
Character of length 1, path to the
|
Value
NULL (invisibly).
Examples
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
{
tar_make_archive(package = "tarchives", pipeline = "example-model")
tar_destroy_archive(package = "tarchives", pipeline = "example-model", ask = FALSE)
}
)
Download files into the store of the running pipeline
Description
Usage
tar_download_file(url, file)
Arguments
url |
A character vector of URLs to download. |
file |
A character vector of the same length as |
Details
Downloads source files into the user/ directory of the store of the
pipeline that is running, for use in the command of a target with
format = "file".
tar_make_archive() runs a pipeline with the working directory set to the
pipeline's directory inside the installed package, while its store lives in
the user cache directory. A relative path such as "_targets/user/data.xlsx"
in a target's command therefore points into the package library, where the
files are lost when the package is reinstalled, can't be written in a
read-only library, and aren't removed by tar_destroy_archive().
tar_download_file() resolves file against targets::tar_path_store()
instead, so downloaded files are kept with the store.
Each file is downloaded to a temporary file next to its destination and
only moved into place once the download has succeeded, so a failed download
never replaces a good file. A download fails with an error of class
tarchives_error_download when the server responds with an HTTP error
status, or returns an HTML page for a file whose extension isn't .html or
.htm (typically an error page served after the file has moved).
The remote file isn't checked for changes on later builds. A changed url
already invalidates the target, since it is part of the command. To
download a file that was republished at the same URL, combine
tar_download_file() with a target factory such as
tarchetypes::tar_change().
Value
A character vector of the paths of the downloaded files, for a
target with format = "file".
Examples
if (identical(Sys.getenv("TAR_LONG_EXAMPLES"), "true")) {
targets::tar_dir({
targets::tar_script({
list(
targets::tar_target(
file_manual,
tarchives::tar_download_file(
url = "https://cran.r-project.org/web/packages/tarchives/tarchives.pdf",
file = "tarchives.pdf"
),
format = "file"
)
)
})
targets::tar_make()
targets::tar_read(file_manual)
})
}
Get a target's value from archive storage, building first if needed
Description
Usage
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.
|
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 |
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 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
|
callr_function |
A function from |
callr_arguments |
A list of arguments to |
envir |
An environment used to resolve |
store |
Character of length 1, path to the
|
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.
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.
Examples
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
tar_get_archive(model, package = "tarchives", pipeline = "example-model")
)
Load a target's value from archive storage
Description
tarchives version of targets::tar_load(). Reads one or more targets from
an archived pipeline's store and assigns them into an environment.
Usage
tar_load_archive(
names,
package,
pipeline,
branches = NULL,
meta = NULL,
strict = TRUE,
silent = FALSE,
envir = parent.frame(),
store = targets::tar_config_get("store")
)
Arguments
names |
Names of the targets to load.
The object supplied to |
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 for any targets that are patterns. |
meta |
Data frame of target metadata from |
strict |
Logical of length 1, whether to error out
if one of the selected targets is in the metadata
but cannot be loaded.
Set to |
silent |
Logical of length 1. Only relevant when
|
envir |
R environment in which to load target return values. |
store |
Character of length 1, directory path to the data store of the pipeline. |
Value
Nothing.
Examples
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
{
tar_make_archive(package = "tarchives", pipeline = "example-model")
tar_load_archive(model, package = "tarchives", pipeline = "example-model")
model
}
)
Run an archived pipeline of targets
Description
Run an archived pipeline of targets
Usage
tar_make_archive(
package,
pipeline,
names = NULL,
shortcut = targets::tar_config_get("shortcut"),
reporter = "silent",
seconds_meta_append = targets::tar_config_get("seconds_meta_append"),
seconds_meta_upload = targets::tar_config_get("seconds_meta_upload"),
seconds_reporter = targets::tar_config_get("seconds_reporter"),
seconds_interval = targets::tar_config_get("seconds_interval"),
callr_function = callr::r,
callr_arguments = targets::tar_callr_args_default(callr_function, reporter),
envir = parent.frame(),
script = targets::tar_config_get("script"),
store = targets::tar_config_get("store"),
garbage_collection = NULL,
use_crew = targets::tar_config_get("use_crew"),
terminate_controller = TRUE,
as_job = targets::tar_config_get("as_job")
)
Arguments
package |
A scalar character of the package name. |
pipeline |
A scalar character of the pipeline name. |
names |
Names of the targets to run or check. Set to |
shortcut |
Logical of length 1, how to interpret the |
reporter |
A scalar character of the reporter type. By default,
|
seconds_meta_append |
Positive numeric of length 1 with the minimum
number of seconds between saves to the local metadata and progress files
in the data store.
his is an aggressive optimization setting not recommended
for most users:
higher values generally make the pipeline run faster, but unsaved
work (in the event of a crash) is not up to date.
When the pipeline ends,
all the metadata and progress data is saved immediately,
regardless of When the pipeline is just skipping targets, the actual interval
between saves is |
seconds_meta_upload |
Positive numeric of length 1 with the minimum
number of seconds between uploads of the metadata and progress data
to the cloud
(see https://books.ropensci.org/targets/cloud-storage.html).
Higher values generally make the pipeline run faster, but unsaved
work (in the event of a crash) may not be backed up to the cloud.
When the pipeline ends,
all the metadata and progress data is uploaded immediately,
regardless of |
seconds_reporter |
Deprecated on 2025-03-31
( |
seconds_interval |
Deprecated on 2023-08-24
(targets version 1.2.2.9001).
Use |
callr_function |
A function from |
callr_arguments |
A list of arguments to |
envir |
An environment, where to run the target R script
(default: The |
script |
Character of length 1, path to the
target script file. Defaults to |
store |
Character of length 1, path to the
|
garbage_collection |
Deprecated. Use the |
use_crew |
Logical of length 1, whether to use |
terminate_controller |
Logical of length 1. For a |
as_job |
|
Value
NULL except if callr_function = callr::r_bg(), in which case
a handle to the callr background process is returned. Either way,
the value is invisibly returned.
Examples
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
tar_make_archive(package = "tarchives", pipeline = "example-model")
)
List the targets of an archived pipeline
Description
tarchives version of targets::tar_manifest(). Returns a data frame of the
targets defined in an archived pipeline's target script file.
Usage
tar_manifest_archive(
package,
pipeline,
names = NULL,
fields = NULL,
drop_missing = TRUE,
callr_function = callr::r,
callr_arguments = targets::tar_callr_args_default(callr_function),
envir = parent.frame(),
script = targets::tar_config_get("script")
)
Arguments
package |
A scalar character of the package name. |
pipeline |
A scalar character of the pipeline name. |
names |
Names of the targets to show. Set to |
fields |
Names of the fields, or columns, to show. Set to
|
drop_missing |
Logical of length 1, whether to automatically omit empty columns and columns with all missing values. |
callr_function |
A function from |
callr_arguments |
A list of arguments to |
envir |
An environment, where to run the target R script
(default: The |
script |
Character of length 1, path to the
target script file. Defaults to |
Value
A data frame of information about the targets in the pipeline. Rows appear in topological order (the order they will run without any influence from parallel computing or priorities).
See Also
tar_archive_pipelines() to list the pipelines in a package.
Examples
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
tar_manifest_archive(package = "tarchives", pipeline = "example-model")
)
Read metadata from archive storage
Description
tarchives version of targets::tar_meta(). Returns the metadata of an
archived pipeline's store.
Usage
tar_meta_archive(
package,
pipeline,
names = NULL,
fields = NULL,
targets_only = FALSE,
complete_only = FALSE,
store = targets::tar_config_get("store")
)
Arguments
package |
A scalar character of the package name. |
pipeline |
A scalar character of the pipeline name. |
names |
Optional, names of the targets. If supplied, |
fields |
Optional, names of columns/fields to select. If supplied,
|
targets_only |
Logical, whether to just show information about targets or also return metadata on functions and other global objects. |
complete_only |
Logical, whether to return only complete rows
(no |
store |
Character of length 1, path to the
|
Value
A data frame with one row per target/object and the selected fields.
Examples
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
{
tar_make_archive(package = "tarchives", pipeline = "example-model")
tar_meta_archive(package = "tarchives", pipeline = "example-model")
}
)
Read a target's value from archive storage
Description
Read a target's value from archive storage
Usage
tar_read_archive(
name,
package,
pipeline,
branches = NULL,
meta = NULL,
store = targets::tar_config_get("store")
)
tar_read_archive_raw(
name,
package,
pipeline,
branches = NULL,
meta = NULL,
store = targets::tar_config_get("store")
)
Arguments
name |
Name of the target to read.
|
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 |
store |
Character of length 1, path to the
|
Details
tar_read_archive() captures name with non-standard evaluation, whereas
tar_read_archive_raw() takes it as a character string.
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.
Examples
withr::with_envvar(
c(R_USER_CACHE_DIR = tempfile()),
{
tar_make_archive(package = "tarchives", pipeline = "example-model")
tar_read_archive(model, package = "tarchives", pipeline = "example-model")
}
)
Run archived R scripts
Description
Run archived R scripts
Usage
tar_source_archive(
package,
files = "R",
envir = targets::tar_option_get("envir"),
change_directory = FALSE
)
Arguments
package |
A scalar character of the package name. |
files |
Character vector of file and directory paths to look for R scripts to run. Paths must either be absolute paths or must be relative to the current working directory just before the function call. |
envir |
Environment to run the scripts. Defaults to
|
change_directory |
Logical, whether to temporarily change the working directory to the directory of each R script before running it. |
Value
NULL (invisibly)
Examples
tar_source_archive(package = "tarchives")
Declare a target to read an archive
Description
Declare a target to read an archive
Usage
tar_target_archive(
name,
package,
pipeline,
name_archive = NULL,
...,
pattern = NULL,
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
deps = NULL,
string = NULL,
format = targets::tar_option_get("format"),
repository = targets::tar_option_get("repository"),
iteration = targets::tar_option_get("iteration"),
error = targets::tar_option_get("error"),
memory = targets::tar_option_get("memory"),
garbage_collection = isTRUE(targets::tar_option_get("garbage_collection")),
deployment = targets::tar_option_get("deployment"),
priority = targets::tar_option_get("priority"),
resources = targets::tar_option_get("resources"),
storage = targets::tar_option_get("storage"),
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue"),
description = targets::tar_option_get("description")
)
tar_target_archive_raw(
name,
package,
pipeline,
name_archive = name,
...,
pattern = NULL,
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
deps = NULL,
string = NULL,
format = targets::tar_option_get("format"),
repository = targets::tar_option_get("repository"),
iteration = targets::tar_option_get("iteration"),
error = targets::tar_option_get("error"),
memory = targets::tar_option_get("memory"),
garbage_collection = isTRUE(targets::tar_option_get("garbage_collection")),
deployment = targets::tar_option_get("deployment"),
priority = targets::tar_option_get("priority"),
resources = targets::tar_option_get("resources"),
storage = targets::tar_option_get("storage"),
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue"),
description = targets::tar_option_get("description")
)
Arguments
name |
Symbol, name of the target.
In A target name must be a valid name for a symbol in R, and it
must not start with a dot. Subsequent targets
can refer to this name symbolically to induce a dependency relationship:
e.g. In most cases, The target name is the name of its local data file in storage. Some file systems are not case sensitive, which means converting a name to a different case may overwrite a different target. Please ensure all target names have unique names when converted to lower case. In addition, a target's
name determines its random number generator seed. In this way,
each target runs with a reproducible seed so someone else
running the same pipeline should get the same results,
and no two targets in the same pipeline share the same seed.
(Even dynamic branches have different names and thus different seeds.)
You can recover the seed of a completed target
with |
package |
A scalar character of the package name. |
pipeline |
A scalar character of the pipeline name. |
name_archive |
Symbol, name of the archived target. If |
... |
Arguments to pass to |
pattern |
Code to define a dynamic branching branching for a target.
In To demonstrate dynamic branching patterns, suppose we have
a pipeline with numeric vector targets |
packages |
Character vector of packages to load right before
the target runs or the output data is reloaded for
downstream targets. Use |
library |
Character vector of library paths to try
when loading |
deps |
Optional character vector of the adjacent upstream
dependencies of the target, including targets and global objects.
If |
string |
Optional string representation of the command.
Internally, the string gets hashed to check if the command changed
since last run, which helps |
format |
Optional storage format for the target's return value.
With the exception of |
repository |
Character of length 1, remote repository for target storage. Choices:
Note: if |
iteration |
Character of length 1, name of the iteration mode of the target. Choices:
|
error |
Character of length 1, what to do if the target stops and throws an error. Options:
|
memory |
Character of length 1, memory strategy. Possible values:
For cloud-based file targets
(e.g. |
garbage_collection |
Logical: |
deployment |
Character of length 1. If |
priority |
Deprecated on 2025-04-08 ( |
resources |
Object returned by |
storage |
Character string to control when the output of the target
is saved to storage. Only relevant when using
|
retrieval |
Character string to control when the current target
loads its dependencies into memory before running.
(Here, a "dependency" is another target upstream that the current one
depends on.) Only relevant when using
|
cue |
An optional object from |
description |
Character of length 1, a custom free-form human-readable
text description of the target. Descriptions appear as target labels
in functions like |
Details
tar_target_archive() captures name and name_archive with non-standard
evaluation, whereas tar_target_archive_raw() takes them as character
strings.
The archive is built (if outdated) and read when the target runs, not when
the target script is sourced, so inspecting the pipeline with
targets::tar_manifest() or targets::tar_visnetwork() does not trigger a
build. The target tracks the installed version of package, so it reruns
and refreshes the data when a new version of the package providing the
archive is installed, and is skipped otherwise. Downstream targets still
only rebuild when the value actually changes.
Value
A target definition object. Users should not modify these directly,
just feed them to list() in your target script file
(default: _targets.R).
Examples
tar_target_archive(
model,
package = "tarchives",
pipeline = "example-model"
)
Use tarchives
Description
Set up tarchives for an existing package.
Usage
use_tarchives(store = targets::tar_config_get("store"))
Arguments
store |
Character of length 1, path to the
|
Value
No return value, called for side effects.
Examples
withr::with_tempdir(
use_tarchives()
)