Skip to content

improve error message for non data.frame inputs #1567

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

Merged
merged 6 commits into from
Aug 15, 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# tidyr (development version)

* `pivot_wider_spec()` now throws a more informative error on non-data frame
inputs (@catalamarti, #1510).

* tidyr now requires dplyr >=1.1.0 (#1568, @catalamarti).

# tidyr 1.3.1
Expand Down
1 change: 1 addition & 0 deletions R/pivot-wide.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ pivot_wider_spec <- function(data,
unused_fn = NULL,
error_call = current_env()) {
check_dots_empty0(...)
check_data_frame(data, call = error_call)

spec <- check_pivot_spec(spec, call = error_call)
check_bool(id_expand, call = error_call)
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/pivot-wide.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# error if input is not a data.frame

Code
pivot_wider_spec(list(), spec)
Condition
Error in `pivot_wider_spec()`:
! `data` must be a data frame, not an empty list.

# error when overwriting existing column

Code
Expand Down
9 changes: 8 additions & 1 deletion tests/testthat/test-pivot-wide.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ test_that("implicit missings turn into explicit missings", {
expect_equal(pv$y, c(NA, 2))
})

test_that("error if input is not a data.frame", {
spec <- tibble(.name = "x", .value = "val", )
expect_snapshot(
pivot_wider_spec(list(), spec),
error = TRUE
)
})

test_that("error when overwriting existing column", {
df <- tibble(
a = c(1, 1),
Expand Down Expand Up @@ -811,4 +819,3 @@ test_that("`id_cols` compat behavior doesn't trigger if named `...` are supplied
pivot_wider(df, ids = id)
})
})

Loading