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

Confusing error message when "expand" hits grouped data #1509

Open
bholtdwyer opened this issue Jul 12, 2023 · 2 comments
Open

Confusing error message when "expand" hits grouped data #1509

bholtdwyer opened this issue Jul 12, 2023 · 2 comments
Labels
reprex needs a minimal reproducible example

Comments

@bholtdwyer
Copy link

When you apply "expand" to data that has been grouped, a somewhat confusing error message suggests incorrectly that the variable being expanded on does not exist. A more helpful error message would say "you cannot expand on a variable by which the data is grouped. Run 'groups()' on your tibble to find variable groupings, or apply ungroup() first".

> bob = tibble(x=c(1,2,3), y=c("abe","bob","abe")) 
> bob = bob %>% group_by(x) 
> expand(bob, 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 
Run `rlang::last_trace()` to see where the error occurred.
>expand(ungroup(bob),x,y)
@bholtdwyer
Copy link
Author

This also happens when you try to complete data that have been grouped:

> ## Example for bug report ###############################################
> df <- tibble(
+   group = c(1:2, 1, 2),
+   item_id = c(1:2, 2, 3),
+   item_name = c("a", "a", "b", "b"),
+   value1 = c(1, NA, 3, 4),
+   value2 = 4:7
+ )
> df = df %>% group_by(group)
> df %>% complete(item_id, item_name)
# A tibble: 8 × 5
# Groups:   group [2]
  group item_id item_name value1 value2
  <dbl>   <dbl> <chr>      <dbl>  <int>
1     1       1 a              1      4
2     1       1 b             NA     NA
3     1       2 a             NA     NA
4     1       2 b              3      6
5     2       2 a             NA      5
6     2       2 b             NA     NA
7     2       3 a             NA     NA
8     2       3 b              4      7
> df = df %>% group_by(item_id, item_name)
> df %>% complete(item_id, item_name)
Error in `reframe()`:
ℹ In argument: `complete(data = pick(everything()), ..., fill = fill, explicit =
  explicit)`.
ℹ In group 1: `item_id = 1`, `item_name = "a"`.
Caused by error:
! object 'item_id' not found
Run `rlang::last_trace()` to see where the error occurred.
> df <- tibble(
+   group = c(1:2, 1, 2),
+   item_id = c(1:2, 2, 3),
+   item_name = c("a", "a", "b", "b"),
+   value1 = c(1, NA, 3, 4),
+   value2 = 4:7
+ )
> df = df %>% group_by(item_id, item_name)
> 
> df %>% complete(item_id, item_name)
Error in `reframe()`:
ℹ In argument: `complete(data = pick(everything()), ..., fill = fill, explicit =
  explicit)`.
ℹ In group 1: `item_id = 1`, `item_name = "a"`.
Caused by error:
! object 'item_id' not found
Run `rlang::last_trace()` to see where the error occurred.
> df %>% ungroup() %>% complete(item_id, item_name)
# A tibble: 6 × 5
  item_id item_name group value1 value2
    <dbl> <chr>     <dbl>  <dbl>  <int>
1       1 a             1      1      4
2       1 b            NA     NA     NA
3       2 a             2     NA      5
4       2 b             1      3      6
5       3 a            NA     NA     NA
6       3 b             2      4      7

@hadley
Copy link
Member

hadley commented Nov 1, 2023

Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session.

@hadley hadley added the reprex needs a minimal reproducible example label Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reprex needs a minimal reproducible example
Projects
None yet
Development

No branches or pull requests

2 participants