Skip to content

Commit

Permalink
Merge pull request #811 from markfairbanks/pmap-names
Browse files Browse the repository at this point in the history
Preserve names in `pmap()`
  • Loading branch information
markfairbanks authored Jun 27, 2024
2 parents e10cf3b + bf38e69 commit a1c5568
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 7 additions & 1 deletion R/purrr-pmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions tests/testthat/test-purrr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})


0 comments on commit a1c5568

Please sign in to comment.