Skip to content

Commit 9966c04

Browse files
catalamartihadleyDavisVaughan
authored
Improve pivot_wider_spec() error for non data.frame inputs (#1567)
* improve error message for non data.frame inputs fixes #1510 * use magritrr pipe * test only list case and remove dbplyr * Simplify test a little more & move to better location * use check_data_frame * NEWS bullet --------- Co-authored-by: Hadley Wickham <[email protected]> Co-authored-by: Davis Vaughan <[email protected]>
1 parent 5e2eddc commit 9966c04

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# tidyr (development version)
22

3+
* `pivot_wider_spec()` now throws a more informative error on non-data frame
4+
inputs (@catalamarti, #1510).
5+
36
* tidyr now requires dplyr >=1.1.0 (#1568, @catalamarti).
47

58
# tidyr 1.3.1

R/pivot-wide.R

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ pivot_wider_spec <- function(data,
298298
unused_fn = NULL,
299299
error_call = current_env()) {
300300
check_dots_empty0(...)
301+
check_data_frame(data, call = error_call)
301302

302303
spec <- check_pivot_spec(spec, call = error_call)
303304
check_bool(id_expand, call = error_call)

tests/testthat/_snaps/pivot-wide.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# error if input is not a data.frame
2+
3+
Code
4+
pivot_wider_spec(list(), spec)
5+
Condition
6+
Error in `pivot_wider_spec()`:
7+
! `data` must be a data frame, not an empty list.
8+
19
# error when overwriting existing column
210

311
Code

tests/testthat/test-pivot-wide.R

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ test_that("implicit missings turn into explicit missings", {
2323
expect_equal(pv$y, c(NA, 2))
2424
})
2525

26+
test_that("error if input is not a data.frame", {
27+
spec <- tibble(.name = "x", .value = "val", )
28+
expect_snapshot(
29+
pivot_wider_spec(list(), spec),
30+
error = TRUE
31+
)
32+
})
33+
2634
test_that("error when overwriting existing column", {
2735
df <- tibble(
2836
a = c(1, 1),
@@ -811,4 +819,3 @@ test_that("`id_cols` compat behavior doesn't trigger if named `...` are supplied
811819
pivot_wider(df, ids = id)
812820
})
813821
})
814-

0 commit comments

Comments
 (0)