diff --git a/NEWS.md b/NEWS.md index 676771f6..a65343c3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # targets 1.9.1.9000 * Use a more lookup-efficient data structure for `tar_runtime$file_info` (#1398). +* Fall back on vector aggregation without names (#1401, @guglicap). # targets 1.9.1 diff --git a/R/class_vector.R b/R/class_vector.R index 082bebff..a5b3596b 100644 --- a/R/class_vector.R +++ b/R/class_vector.R @@ -18,6 +18,23 @@ value_produce_slice.tar_vector <- function(value, index) { # nolint #' @export value_produce_aggregate.tar_vector <- function(value, objects) { # nolint + tar_vec_c(objects) +} + +tar_vec_c <- function(objects) { + tryCatch( + vec_c_spec(objects), + # Covered in tests/testthat/test-class_vector.R + # but might not register in covr. + # nocov start + error = function(condition) { + do.call(vctrs::vec_c, unname(objects)) + } + # nocov end + ) +} + +vec_c_spec <- function(objects) { objects$.name_spec <- "{outer}_{inner}" do.call(vctrs::vec_c, objects) } diff --git a/tests/testthat/test-class_vector.R b/tests/testthat/test-class_vector.R index 9d963c88..cee534b9 100644 --- a/tests/testthat/test-class_vector.R +++ b/tests/testthat/test-class_vector.R @@ -42,3 +42,12 @@ tar_test("vector$validate()", { x <- value_init(object = "abc", iteration = "vector") expect_silent(value_validate(x)) }) + +tar_test("tar_vec_c()", { + expect_equal(tar_vec_c(list(x = TRUE, y = FALSE)), c(x = TRUE, y = FALSE)) + x <- structure(TRUE, class = "custom") + y <- structure(FALSE, class = "custom") + c.custom <- function(...) structure(NextMethod(), class = "custom") + out <- tar_vec_c(list(x = x, y = y)) + expect_equal(as.logical(out), c(TRUE, FALSE)) +})