Skip to content

Conversation

DavisVaughan
Copy link
Member

@DavisVaughan DavisVaughan commented Jan 5, 2023

Closes #1425 (Unless we decide otherwise)
Related to #1367

I'd say that this resolves #1425 by giving a much better error message.

It also resolves this comment #1367 (comment), again with a much better error message.

It does not resolve the original issue presented in #1367 about using some automatic separator for unnamed elements. I think that is related but can be thought about separately. I've kept that one open.

In general this improves errors for unpack(), unnest(), unnest_wider(), and the separate_wider_*() family.


There are two types of name duplication issues that can occur when unpacking, which I have called:

  • inner / inner duplication (unpacking multiple columns that have duplicate inner names)
  • outer / inner duplication (unpacking 1 column that duplicates an outer name)

This PR attempts to give better error messages for both of those cases when names_sep = NULL and names_repair = "check_unique", i.e. the user has not attempted to solve these issues themselves yet.

I tried not to mention "unpacking" in the error because these bubble up from other functions.

library(tidyr)

# inner / inner
df <- tibble(
  x = list(tibble(a = 1)),
  y = list(tibble(a = 2))
)
unnest(df, c(x, y))
#> Error in `unpack()` at tidyr/R/unnest.R:181:2:
#> ! Can't duplicate names across the modified columns.
#> ✖ These names are duplicated:
#>   ℹ `a`, across `x` and `y`.
#> ℹ Use `names_sep` to disambiguate using the column name.
#> ℹ Or use `names_repair` to specify a repair strategy.

# outer / inner
df <- tibble(
  x = 1, 
  y = list(tibble(x = 2))
)
unnest(df, y)
#> Error in `unpack()` at tidyr/R/unnest.R:181:2:
#> ! Can't duplicate names between the modified columns and the original data.
#> ✖ These names are duplicated:
#>   ℹ `x`, from `y`.
#> ℹ Use `names_sep` to disambiguate using the column name.
#> ℹ Or use `names_repair` to specify a repair strategy.

You would get more indented bullets if there are additional problems (see tests)

@DavisVaughan DavisVaughan requested a review from hadley January 5, 2023 22:29
@DavisVaughan DavisVaughan force-pushed the feature/unpack-column-duplication-error branch from 88a6661 to 4f45e24 Compare January 9, 2023 15:20
@DavisVaughan DavisVaughan merged commit a958cd7 into tidyverse:main Jan 9, 2023
@DavisVaughan DavisVaughan deleted the feature/unpack-column-duplication-error branch January 9, 2023 15:33
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.

separate_wider_regex(), names_sep, and too_few
2 participants