Package 'epitargets'

Title: Economic Policy Institute tools for working with targets
Description: Additional utilities to use the targets R package.
Authors: Ben Zipperer [aut, cre], Economic Policy Institute [cph, fnd]
Maintainer: Ben Zipperer <[email protected]>
License: MIT + file LICENSE
Version: 0.4.0
Built: 2026-07-24 08:02:37 UTC
Source: https://github.com/economic/epitargets

Help Index


Collect date targets into a tibble

Description

Gathers the values of companion ⁠_date⁠ targets (created by tar_target_date() or tar_age_date()) into a tibble::tibble with columns name and time.

Usage

collect_target_date(...)

Arguments

...

Date values from ⁠_date⁠ targets. Pass each ⁠_date⁠ target by name (e.g., ⁠x_date, y_date⁠).

Value

A tibble::tibble with columns name (character) and time.

Examples

collect_target_date(x_date = as.Date("2024-01-01"), y_date = as.Date("2024-06-15"))

Write data to a CSV file and return the file path

Description

A thin wrapper around readr::write_csv() that writes data to file and returns the file path. This is useful as a targets format function where the target value should be the path to the written file.

Usage

create_csv(data, file, ...)

Arguments

data

A data frame to write.

file

A string giving the file path to write to.

...

Additional arguments passed to readr::write_csv().

Value

The file path file, invisibly.

Examples

targets::tar_dir({
  create_csv(data.frame(x = 1:3), "test.csv")
})

RStudio addin to read a target and stash it

Description

Reads the target named under the cursor (or the active selection) and stashes it via tar_read_stash(), leaving a copy in the global environment as .target. Bind it to a keyboard shortcut in the RStudio addin manager or a Positron keybinding, analogous to targets::rstudio_addin_tar_read().

Usage

rstudio_addin_tar_read_stash()

Details

The symbol under the cursor is extracted with atcursor::get_word_or_selection(), which works in RStudio, VS Code, and Positron. atcursor is an optional dependency; if it is not installed the addin prompts to install it from its r-universe repository. If the cursor is on whitespace with no selection, the addin reports this and does nothing.

Value

The target value, invisibly (also stashed as .target).


Create an age-cued target with a companion date target

Description

Like tar_target_date() but adds an age-based cue via tarchetypes::tar_cue_age_raw() so the target automatically re-runs after a configurable time period. Useful for targets that fetch data from APIs.

Usage

tar_age_date(name, command, age = as.difftime(1, units = "days"), ...)

Arguments

name

Symbol, name of the target.

command

Expression, R command to run the target.

age

A difftime object specifying the maximum age before the target re-runs. Defaults to 1 day.

...

Additional arguments passed to targets::tar_target_raw().

Value

A list of two target objects: the primary target (with an age cue) and the companion ⁠_date⁠ target.

Examples

if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) {
targets::tar_dir({
  targets::tar_script({
    library(targets)
    list(
      epitargets::tar_age_date(x, 1 + 1, age = as.difftime(1, units = "days"))
    )
  })
  targets::tar_make()
  targets::tar_read(x)
  targets::tar_read(x_date)
})
}

Read a CSV file as a target

Description

A convenience wrapper around tarchetypes::tar_file_read() for CSV files. Creates a pair of targets: one to track the file with format = "file", and another to read the file with readr::read_csv().

Usage

tar_csv_read(name, command, .read_csv_args = list(show_col_types = FALSE), ...)

Arguments

name

Symbol, name of the target.

command

Expression, R code that returns the file path to the CSV.

.read_csv_args

A named list of additional arguments passed to readr::read_csv(). Defaults to list(show_col_types = FALSE). Supplying this argument replaces the defaults entirely.

...

Additional arguments passed to targets::tar_target_raw() for the read target.

Details

The storage format of the read target is inherited from targets::tar_option_get("format") (by default "rds"). Callers can override it by passing e.g. format = "qs" via ....

Value

A list of two target objects: a file-tracking target (name_file) and a CSV-reading target (name).

Examples

if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) {
targets::tar_dir({
  targets::tar_script({
    library(targets)
    list(
      epitargets::tar_csv_read(my_data, "data.csv")
    )
  })
  targets::tar_manifest()
})
}

Read a Parquet file as a target

Description

A convenience wrapper around tarchetypes::tar_file_read() for Parquet files. Creates a pair of targets: one to track the file with format = "file", and another to read the file with arrow::read_parquet().

Usage

tar_parquet_read(name, command, .read_parquet_args, ...)

Arguments

name

Symbol, name of the target.

command

Expression, R code that returns the file path to the Parquet file.

.read_parquet_args

A named list of additional arguments passed to arrow::read_parquet().

...

Additional arguments passed to targets::tar_target_raw() for the read target.

Details

The read target's storage format defaults to "parquet" (overriding the usual targets::tar_option_get("format") inheritance), so the cached object is written as a Parquet file in ⁠_targets/objects/⁠. Callers can override this by passing e.g. format = "rds" via ....

Value

A list of two target objects: a file-tracking target (name_file) and a Parquet-reading target (name).

Examples

if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) {
targets::tar_dir({
  targets::tar_script({
    library(targets)
    list(
      epitargets::tar_parquet_read(my_data, "data.parquet")
    )
  })
  targets::tar_manifest()
})
}

Read a target and stash it in an environment

Description

Reads a target with targets::tar_read() and, in addition to returning it, assigns a copy into envir under a fixed scratch name (.target by default). This lets you keep a handle to the value for interactive exploration without re-running the read. It is the engine behind the rstudio_addin_tar_read_stash() addin.

Usage

tar_read_stash(
  name,
  stash_name = ".target",
  print_value = TRUE,
  envir = globalenv(),
  store = targets::tar_config_get("store"),
  ...
)

Arguments

name

Symbol or string, the target to read. NSE-friendly, matching targets::tar_read().

stash_name

String, the variable name to assign the value to in envir. Defaults to ".target", a leading-dot scratch slot. If it already exists in envir it is silently overwritten.

print_value

Logical. If TRUE (default), the value is printed after assignment so the addin behaves like the stock tar_read addin.

envir

Environment to stash the value into. Defaults to globalenv().

store

Character, the targets data store. Passed through to targets::tar_read_raw().

...

Additional arguments passed to targets::tar_read_raw() (e.g. branches).

Details

A resolved character target name is passed to targets::tar_read_raw() rather than targets::tar_read(), because the latter uses non-standard evaluation and would otherwise look up a target named after the variable rather than its value.

When called from a non-interactive context (a script or pipeline), a warning is emitted but the value is still stashed.

Value

The target value, invisibly.

Examples

if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) {
targets::tar_dir({
  targets::tar_script({
    library(targets)
    list(
      targets::tar_target(x, 1 + 1)
    )
  })
  targets::tar_make()
  tar_read_stash(x)
  .target
})
}

Create a target with a companion date target

Description

Wraps targets::tar_target_raw() to create a primary target and a companion ⁠_date⁠ target that records Sys.Date() whenever the primary target runs.

Usage

tar_target_date(name, command, ...)

Arguments

name

Symbol, name of the target.

command

Expression, R command to run the target.

...

Additional arguments passed to targets::tar_target_raw().

Value

A list of two target objects: the primary target and the companion ⁠_date⁠ target.

Examples

if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) {
targets::tar_dir({
  targets::tar_script({
    library(targets)
    list(
      epitargets::tar_target_date(x, 1 + 1)
    )
  })
  targets::tar_make()
  targets::tar_read(x)
  targets::tar_read(x_date)
})
}