tibble icon indicating copy to clipboard operation
tibble copied to clipboard

.name_repair in `as_tibble()` doesn't check the name for rownames coerced to a column

Open smped opened this issue 2 years ago • 3 comments

When providing an obviously dumb name for a new column containing rownames, .name_repair doesn't appear to check the name of the new column against existing columns

as_tibble(data.frame(a = 1:5), rownames = "a", .name_repair = "unique")
# A tibble: 5 × 2
  a         a
  <chr> <int>
1 1         1
2 2         2
3 3         3
4 4         4
5 5         5

To my understanding, this should spit the following message and output as the names

vctrs::vec_as_names(c("a", "a"), repair = "unique")
New names:
* a -> a...1
* a -> a...2
[1] "a...1" "a...2"

smped avatar Mar 11 '22 14:03 smped

Thanks, good catch. For code that works before and after we fix that, would as_tibble(rownames_to_column(...)) work?

tibble::rownames_to_column(data.frame(a = 1:3), var = "a")
#> Error:
#> ! Column name `a` must not be duplicated.
#> Caused by error in `stop_vctrs()`:
#> ! Names must be unique.
#> x These names are duplicated:
#>   * "a" at locations 1 and 2.

Created on 2022-03-16 by the reprex package (v2.0.1)

krlmlr avatar Mar 16 '22 22:03 krlmlr

Yeah. That works nicely! Thanks for the follow up.

smped avatar Mar 22 '22 11:03 smped

Let's leave it "as is", because name repair can e.g. also be a function that returns a value independent of its input, as seen in the inspectdf package. It's easy to work around anyway.

Happy to document this glitch.

krlmlr avatar Apr 26 '22 09:04 krlmlr