Skip to content

Commit

Permalink
Add names of targets in tar_plans to environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Noam Ross authored and noamross committed Dec 9, 2023
1 parent ec6c129 commit 5578c0d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#' Collect all targets and lists of targets in the environment
all_targets <- function(env = parent.env(environment()), type = "tar_target") {
all_targets <- function(env = parent.env(environment()), type = "tar_target", add_list_names = TRUE) {

# Function to determine if an object is a type (a target), or a list on only that type
rfn <- function(obj) inherits(obj, type) || (is.list(obj) && all(vapply(obj, rfn, logical(1))))
# Get the names everything in the environment (e.g. sourced in the _targets file)
objs <- ls(env)
out <- list()

for(o in objs) {
obj <- get(o, envir = env)
if (rfn(obj)) {
out[[length(out) + 1]] <- obj
obj <- get(o, envir = env) # Get each top-level object in turn
if (rfn(obj)) { # For targets and lists of targets
out[[length(out) + 1]] <- obj # Add them to the output

# If the object is a list of targets, add a vector of those names to the environment
# So that one can call `tar_make(list_name)` to make all the targets in that list
if(add_list_names && is.list(obj)) {
target_names <- vapply(obj, \(x) x$settings$name, character(1))
assign(o, target_names, envir = env)
}
}
}
return(out)
Expand Down

0 comments on commit 5578c0d

Please sign in to comment.