Skip to content

Commit

Permalink
tar_map() tidyselect descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Feb 20, 2024
1 parent 3a78ae5 commit 471f9fb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
32 changes: 20 additions & 12 deletions R/tar_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@
#' used to generate the suffixes in the names of the new targets.
#' The value of `names` should be a `tidyselect` expression
#' such as a call to [any_of()] or [starts_with()].
#' @param descriptions Character of length 1, name of a column in `values`
#' @param descriptions Names of a column in `values`
#' to append to the custom description of each generated target.
#' Set to `NULL` to omit.
#' The value of `descriptions` should be a `tidyselect` expression
#' such as a call to [any_of()] or [starts_with()].
#' @param unlist Logical, whether to flatten the returned list of targets.
#' If `unlist = FALSE`, the list is nested and sub-lists
#' are named and grouped by the original input targets.
Expand All @@ -69,18 +70,17 @@
tar_map <- function(
values,
...,
names = -tidyselect::any_of(descriptions),
descriptions = NULL,
names = tidyselect::everything(),
descriptions = tidyselect::everything(),
unlist = FALSE
) {
targets <- unlist(list(...), recursive = TRUE) %|||% list()
targets::tar_assert_target_list(targets)
targets::tar_assert_chr(descriptions %|||% "x")
targets::tar_assert_scalar(descriptions %|||% "x")
targets::tar_assert_in(descriptions, base::names(values))
assert_values_list(values)
names_quosure <- rlang::enquo(names)
names <- eval_tidyselect(names_quosure, base::names(values))
descriptions_quosure <- rlang::enquo(descriptions)
descriptions <- eval_tidyselect(descriptions_quosure, base::names(values))
values <- tibble::as_tibble(values)
values <- tar_map_process_values(values)
values <- tar_map_extend_values(targets, values, names)
Expand Down Expand Up @@ -157,11 +157,10 @@ tar_map_iter <- function(values, target, command, pattern, descriptions) {
name <- as.character(values[[settings$name]])
command <- substitute_expr(command, values)
pattern <- substitute_expr(pattern, values) %||% NULL
base_description <- as.character(settings$description)
description <- if_any(
length(descriptions) > 0L,
trimws(paste(base_description, values[[descriptions]])),
base_description
description <- tar_map_extend_description(
description = settings$description,
values = values,
descriptions = descriptions
)
targets::tar_target_raw(
name = name,
Expand Down Expand Up @@ -192,3 +191,12 @@ tar_map_iter <- function(values, target, command, pattern, descriptions) {
description = description
)
}

tar_map_extend_description <- function(description, values, descriptions) {
description <- as.character(description)
if (length(descriptions) > 0L) {
suffix <- paste(as.character(values[descriptions]), collapse = " ")
description <- paste(description, suffix)
}
trimws(description)
}
9 changes: 5 additions & 4 deletions man/tar_map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions tests/testthat/test-tar_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ targets::tar_test("tar_map() manifest", {
)
})
out <- targets::tar_manifest(callr_function = NULL)
expect_equal(dim(out), c(4, 3))
expect_equal(dim(out), c(4, 4))
names <- c("x_12_56", "x_34_78", "y_12_56", "y_34_78")
expect_equal(sort(out$name), sort(c(names)))
expect_equal(out$command[out$name == "x_12_56"], "12 + 56")
Expand Down Expand Up @@ -221,7 +221,8 @@ targets::tar_test("tar_map() description from values only", {
name = letters[seq_len(4L)],
blurb = as.character(seq_len(4L))
),
descriptions = "blurb",
names = tidyselect::any_of("name"),
descriptions = tidyselect::any_of("blurb"),
tar_target(x, 1)
)
})
Expand All @@ -240,9 +241,11 @@ targets::tar_test("tar_map() description from both targets and values", {
tar_map(
values = list(
name = letters[seq_len(4L)],
blurb = as.character(seq_len(4L))
blurb = as.character(seq_len(4L)),
blurb2 = c("w", "x", "y", "z")
),
descriptions = "blurb",
names = tidyselect::any_of("name"),
descriptions = tidyselect::any_of(c("blurb", "blurb2")),
tar_target(x, 1, description = "info")
)
})
Expand All @@ -251,5 +254,8 @@ targets::tar_test("tar_map() description from both targets and values", {
fields = tidyselect::any_of("description"),
drop_missing = FALSE
)
expect_equal(manifest$description, paste("info", seq_len(4L)))
expect_equal(
manifest$description,
paste("info", seq_len(4L), c("w", "x", "y", "z"))
)
})
10 changes: 10 additions & 0 deletions tests/testthat/test-tar_rep.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ targets::tar_test("correct RNG state", {
}
})

targets::tar_test("tar_rep_bind() group", {
out <- tar_rep_bind(
out = list(a = tibble::tibble(a = "x")),
iteration = "group"
)
expect_true(tibble::is_tibble(out))
expect_equal(colnames(out), "a")
expect_equal(out$a, "x")
})

targets::tar_test("tar_rep_bind() error handling", {
expect_error(
tar_rep_bind(out = "x", iteration = "none"),
Expand Down

0 comments on commit 471f9fb

Please sign in to comment.