diff --git a/NEWS.md b/NEWS.md index 0b9984c9..20a94149 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # tidytable 0.11.1 (in development) +#### Functionality improvements +* `pmap()` now preserves names (#809) + #### Bug fixes * Attempting to rename columns using `group_by()` now leads to an error (#799) * `pmap()` family works with data frame inputs (#803) diff --git a/R/purrr-pmap.R b/R/purrr-pmap.R index 2e068ca1..00298f45 100644 --- a/R/purrr-pmap.R +++ b/R/purrr-pmap.R @@ -5,11 +5,17 @@ pmap <- function(.l, .f, ...) { .f <- as_function(.f) args <- .args_recycle(.l) - do.call("mapply", c( + out <- do.call("mapply", c( FUN = list(quote(.f)), args, MoreArgs = quote(list(...)), SIMPLIFY = FALSE, USE.NAMES = FALSE )) + if (obj_is_list(args)) { + if (is_named(args[[1]])) { + names(out) <- names(args[[1]]) + } + } + out } #' @export diff --git a/tests/testthat/test-purrr.R b/tests/testthat/test-purrr.R index 66f1ab09..f70da530 100644 --- a/tests/testthat/test-purrr.R +++ b/tests/testthat/test-purrr.R @@ -63,8 +63,15 @@ test_that("pmap works", { expect_equal(.vec, c(2, 4, 6)) # Works on data.frame inputs, #803 df <- tidytable(a = 1:3, b = 4:6) - out <- pmap(df, ~ .x + .y) - expect_equal(out, list(5, 7, 9)) + res <- pmap(df, ~ .x + .y) + expect_equal(res, list(5, 7, 9)) + # Preserves names, #809 + l <- list( + vals1 = list(a = 1, b = 2), + vals2 = list(c = 3, d = 4) + ) + res <- pmap(l, ~ .x + .y) + expect_equal(names(res), c("a", "b")) })