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.3.0
Built: 2026-05-24 06:03:17 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")
})

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()
})
}

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)
})
}