R/tar_select_targets.R

Defines functions tar_select_targets

Documented in tar_select_targets

#' @title Select target objects from a target list
#' @export
#' @family target selection
#' @description Select target objects from a target list.
#' @return A list of target objects. See the "Target objects" section
#'   of this help file.
#' @inheritSection tar_map Target objects
#' @param targets A list of target objects as described in the
#'   "Target objects" section. It does not matter how nested
#'   the list is as long as the only leaf nodes are targets.
#' @param ... One or more comma-separated `tidyselect` expressions,
#'   e.g. `starts_with("prefix")`. Just like `...` in `dplyr::select()`.
#' @examples
#' if (identical(Sys.getenv("TAR_LONG_EXAMPLES"), "true")) {
#' targets::tar_dir({ # tar_dir() runs code from a temporary directory.
#' targets <- list(
#'   list(
#'     targets::tar_target(x, 1),
#'     targets::tar_target(y1, 2)
#'   ),
#'   targets::tar_target(y2, 3),
#'   targets::tar_target(z, 4)
#' )
#' tar_select_targets(targets, starts_with("y"), contains("z"))
#' })
#' }
tar_select_targets <- function(targets, ...) {
  targets <- unlist(list(targets), recursive = TRUE)
  names(targets) <- map_chr(targets, ~.x$settings$name)
  out <- tidyselect::eval_select(
    rlang::expr(c(...)),
    data = targets,
    strict = FALSE
  )
  targets[sort(names(out))]
}

Try the tarchetypes package in your browser

Any scripts or data that you put into this service are public.

tarchetypes documentation built on Oct. 4, 2023, 5:08 p.m.