Skip to content

fix: improve error message when expand/complete on grouping column (#1509)#1636

Closed
LeonidasZhak wants to merge 2 commits into
tidyverse:mainfrom
LeonidasZhak:fix/expand-complete-grouping-column-error-1509
Closed

fix: improve error message when expand/complete on grouping column (#1509)#1636
LeonidasZhak wants to merge 2 commits into
tidyverse:mainfrom
LeonidasZhak:fix/expand-complete-grouping-column-error-1509

Conversation

@LeonidasZhak

Copy link
Copy Markdown

Problem

When calling expand() or complete() on a grouped data frame with a grouping column, the error message was confusing:

bob <- tibble(x = c(1, 2, 3), y = c("abe", "bob", "abe"))
bob_grouped <- bob |> group_by(x)
expand(bob_grouped, x, y)
#> Error in `reframe()`:
#> ℹ In argument: `expand(data = pick(everything()), ..., .name_repair = .name_repair)`.
#> ℹ In group 1: `x = 1`.
#> Caused by error:
#> ! object 'x' not found

The error "object 'x' not found" is misleading because x exists in the data, but it's a grouping column that isn't available in the data mask used by pick(everything()).

Solution

Added explicit checks in expand.grouped_df() and complete.grouped_df() to detect when the user tries to expand/complete on a grouping column and provide a clear, actionable error message:

expand(bob_grouped, x, y)
#> Error:
#> ! Can't expand on a grouping column.
#> i Column "x" is a grouping variable.
#> i Use `dplyr::ungroup()` first, or expand on non-grouping columns.

Changes

  • R/expand.R: Added check for grouping columns in expand.grouped_df()
  • R/complete.R: Added check for grouping columns in complete.grouped_df()
  • tests/testthat/test-expand.R: Updated test to use snapshot testing
  • tests/testthat/test-complete.R: Updated test to use snapshot testing
  • tests/testthat/_snaps/expand.md: Added snapshot for new error message
  • tests/testthat/_snaps/complete.md: Added snapshot for new error message

Validation

Related Issues

Closes #1509

Filter out infinite values before computing range in full_seq.numeric().
This prevents the cryptic 'missing value where TRUE/FALSE needed' error
when input contains Inf or -Inf values.

Fixes tidyverse#1496
…idyverse#1509)

- Add clear error message when trying to expand or complete on a grouping column
- Error now says 'Can't expand/complete on a grouping column' instead of cryptic 'object not found'
- Suggests using ungroup() or working with non-grouping columns
- Updated tests to use snapshot testing for error messages
@LeonidasZhak

Copy link
Copy Markdown
Author

I am withdrawing this PR as part of a broader cleanup of an oversized automated contribution batch. I am sorry for the review noise and the pressure this may have put on maintainers. Going forward I will keep contributions to this project much more limited and higher-signal, ideally only one or two focused PRs at a time. Thank you for maintaining the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confusing error message when "expand" hits grouped data

1 participant