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

Error on named tidyselections in pivot_longer() and pivot_wider() #1104

Closed
mgirlich opened this issue Mar 8, 2021 · 3 comments · Fixed by #1405
Closed

Error on named tidyselections in pivot_longer() and pivot_wider() #1104

mgirlich opened this issue Mar 8, 2021 · 3 comments · Fixed by #1405
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@mgirlich
Copy link
Contributor

mgirlich commented Mar 8, 2021

As the cols argument supports the tidy-select syntax I thought I could use it to rename a column. But this didn't work as expected and when using a named vector in cols we get:

  • a unclear error message when using a data frame
  • a better error message when using a data frame
  • a clearly unwanted result when the name actually exists as a column

I think it would be nice to allow renaming via cols

library(tidyr)

# unclear error message for data frame
data.frame(x = 1, y = 2) %>% 
  pivot_longer(c(z = y))
#> Error in `[.data.frame`(data, cols): undefined columns selected

# better for tibble
tibble(x = 1, y = 2) %>% 
  pivot_longer(c(z = y))
#> Error: Can't subset columns that don't exist.
#> x Column `z` doesn't exist.

# when the name actually exists we get something quite unwanted
data.frame(x = 1, y = 2, z = 3) %>% 
  pivot_longer(c(z = y))
#> # A tibble: 1 x 4
#>       x     y name  value
#>   <dbl> <dbl> <chr> <dbl>
#> 1     1     2 z         3

Created on 2021-03-08 by the reprex package (v1.0.0)

@hadley
Copy link
Member

hadley commented Aug 23, 2021

Hmmmm, I'm not sure — I feel like that might be overloading pivot_longer() with slightly too much functionality.

@hadley hadley added bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes" labels Aug 23, 2021
@DavisVaughan
Copy link
Member

In #1160 we discussed just setting allow_rename = FALSE in the tidyselect call to disable this early on, but #1160 (comment) reminds us of a tidyselect bug preventing this from working well

@DavisVaughan DavisVaughan changed the title pivot_longer(): named vector in cols Error on named tidyselections in pivot_longer() and pivot_wider() Nov 23, 2021
@DavisVaughan
Copy link
Member

DavisVaughan commented Nov 23, 2021

We are going to wait until we get a tidyselect release that will fix this, and then we can apply allow_rename = FALSE everywhere all at once. Needed in:

  • pivot_longer() + spec helpers
  • pivot_wider() + spec helpers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants