From 25baba9bdb94f418869de27211ac168a7092b1a2 Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Thu, 27 Jun 2024 12:20:55 -0600 Subject: [PATCH 1/3] Preserve names from first input --- R/purrr-pmap.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 From b0ed6e168814f038f8824f393f860f3a8ab9eb35 Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Thu, 27 Jun 2024 12:22:34 -0600 Subject: [PATCH 2/3] Add names test --- tests/testthat/test-purrr.R | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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")) }) From bf38e69ea55cacbef1653a4f68f2be8286646128 Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Thu, 27 Jun 2024 12:22:44 -0600 Subject: [PATCH 3/3] News bullet --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) 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)