Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pmap() works on data frame inputs #804

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
})


Loading