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

fix: list_transpose() takes into account all elements for the template #1136

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 14 additions & 5 deletions R/list-transpose.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#' @param x A list of vectors to transpose.
#' @param template A "template" that describes the output list. Can either be
#' a character vector (where elements are extracted by name), or an integer
#' vector (where elements are extracted by position). Defaults to the names
#' of the first element of `x`, or if they're not present, the integer
#' indices.
#' vector (where elements are extracted by position). Defaults to the union
#' of the names of the elements of `x`, or if they're not present, the
#' union of the integer indices.
#' @param simplify Should the result be [simplified][list_simplify]?
#' * `TRUE`: simplify or die trying.
#' * `NA`: simplify if possible.
Expand Down Expand Up @@ -74,8 +74,17 @@ list_transpose <- function(x,

if (length(x) == 0) {
template <- integer()
} else {
template <- template %||% vec_index(x[[1]])
} else if (is.null(template)) {
indexes <- map(x, vec_index)
try_fetch(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withCallingHandlers should be adequate here.

template <- reduce(indexes, vec_set_union),
vctrs_error_ptype2 = function(e) {
cli::cli_abort(
"Can't combine named and unnamed vectors.",
arg = template
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs call still?

)
}
)
}

if (!is.character(template) && !is.numeric(template)) {
Expand Down
6 changes: 3 additions & 3 deletions man/list_transpose.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/list-transpose.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@
Error in `list_transpose()`:
! `template` must be a character or numeric vector, not a function.

# fail mixing named and unnamed vectors

Code
list_transpose(list(x = list(a = 1, b = 2), y = list(3, 4)))
Condition
Error in `handlers[[1L]]()`:
! Can't combine named and unnamed vectors.

9 changes: 8 additions & 1 deletion tests/testthat/test-list-transpose.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_that("can use character template", {
# Default:
expect_equal(
list_transpose(x, default = NA),
list(a = c(1, NA), b = c(2, 3))
list(a = c(1, NA), b = c(2, 3), c = c(NA, 4))
)

# Change order
Expand Down Expand Up @@ -130,3 +130,10 @@ test_that("validates inputs", {
list_transpose(list(1), template = mean)
})
})

test_that("fail mixing named and unnamed vectors", {
x <- list(list(a = 1, b = 2), list(a = 3, b = 4))
expect_snapshot(error = TRUE, {
list_transpose(list(x = list(a = 1, b = 2), y = list(3, 4)))
})
})
Loading