-
Notifications
You must be signed in to change notification settings - Fork 18
/
tar_select_targets.R
36 lines (36 loc) · 1.26 KB
/
tar_select_targets.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#' @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) <- sort(map_chr(targets, ~.x$settings$name))
out <- tidyselect::eval_select(
rlang::expr(c(...)),
data = targets,
strict = FALSE
)
targets[names(out)]
}