Skip to content

Commit

Permalink
Merge pull request #804 from markfairbanks/pmap-data_frame
Browse files Browse the repository at this point in the history
`pmap()` works on data frame inputs
  • Loading branch information
markfairbanks authored Mar 9, 2024
2 parents 7c51151 + e608dc0 commit 3ad9130
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Bug fixes
* Attempting to rename columns using `group_by()` now leads to an error (#799)
* `pmap()` family works with data frame inputs (#803)

# tidytable 0.11.0

Expand Down
9 changes: 4 additions & 5 deletions R/purrr-pmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ pmap_vec <- function(.l, .f, ..., .ptype = NULL) {
}

.args_recycle <- function(args) {
lengths <- map_int(args, length)
n <- max(lengths)
args <- as.list(args)
sizes <- list_sizes(args)
n <- max(sizes)

stopifnot(all(lengths == 1L | lengths == n))
to_recycle <- lengths == 1L
args[to_recycle] <- map(args[to_recycle], function(x) rep.int(x, n))
args <- map(args, vec_recycle, n)

args
}
4 changes: 4 additions & 0 deletions tests/testthat/test-purrr.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ test_that("pmap works", {
expect_equal(.df, tidytable(x = 1:3, y = 1:3))
.vec <- pmap_vec(list(1:3, 1:3), ~ .x + .y)
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))
})


0 comments on commit 3ad9130

Please sign in to comment.