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

Names attribute in cols for pivot_longer results in "Can't rename variables in this context." #1481

Closed
JamesHWade opened this issue Feb 2, 2023 · 3 comments
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@JamesHWade
Copy link
Contributor

After upgrading to v1.3 I noticed, pivot_longer fails when I pass a vector with names attributes to the cols argument.
Relevant issues: #1104 and #1160. Looks like #1405 is the breaking change?

I discovered this when trying to create custom tidymodels::recipes steps. Would it be best practice to use as.character(cols) for anything passed to pivot_longer? This may not be enough context, but here is what I was working on if more context might help.

Here is a reprex:

library(tidyr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(modeldata)
data(meats)

meats_small <- meats |> select(x_001, x_002, x_003, water, protein)

measures <- c("x_001", "x_002", "x_003")
measures_with_names <- measures
names(measures_with_names) <- measures

meats_small |> 
  pivot_longer(
    cols = all_of(measures),
    names_to = "measure",
    values_to = "response"
  )
#> # A tibble: 645 × 4
#>    water protein measure response
#>    <dbl>   <dbl> <chr>      <dbl>
#>  1  60.5    16.7 x_001       2.62
#>  2  60.5    16.7 x_002       2.62
#>  3  60.5    16.7 x_003       2.62
#>  4  46      13.5 x_001       2.83
#>  5  46      13.5 x_002       2.84
#>  6  46      13.5 x_003       2.84
#>  7  71      20.5 x_001       2.58
#>  8  71      20.5 x_002       2.58
#>  9  71      20.5 x_003       2.59
#> 10  72.8    20.7 x_001       2.82
#> # … with 635 more rows

meats_small |> 
  pivot_longer(
    cols = all_of(measures_with_names),
    names_to = "measure",
    values_to = "response"
  )
#> Error in `pivot_longer()`:
#> ! Can't rename variables in this context.

#> Backtrace:
#>      ▆
#>   1. ├─tidyr::pivot_longer(...)
#>   2. └─tidyr:::pivot_longer.data.frame(...)
#>   3.   └─tidyr::build_longer_spec(...)
#>   4.     └─tidyselect::eval_select(...)
#>   5.       └─tidyselect:::eval_select_impl(...)
#>   6.         ├─tidyselect:::with_subscript_errors(...)
#>   7.         │ └─rlang::try_fetch(...)
#>   8.         │   └─base::withCallingHandlers(...)
#>   9.         └─tidyselect:::vars_select_eval(...)
#>  10.           └─tidyselect:::ensure_named(...)
#>  11.             └─cli::cli_abort(...)
#>  12.               └─rlang::abort(...)

Created on 2023-02-02 with reprex v2.0.2

@jimmyrisk
Copy link

was there any fix to this? I'm teaching a stat learning course. I have tidyr v1.2.1 and code is working fine, some new students have 1.3.0 and are getting the can't rename variables in this context error.

@DavisVaughan
Copy link
Member

Just unname the vector you supplied to all_of() using unname()

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

I think this is a case where the error message should be improved in tidyselect r-lib/tidyselect#336

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

No branches or pull requests

4 participants