-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
rename_with + all_of fails with named character vector for .cols #6745
Comments
When renaming is allowed in combination with tidyselection, passing a named input to library(dplyr, warn.conflicts = FALSE)
mtcars <- as_tibble(mtcars)
v <- c(mp2 = "mpg")
select(mtcars, all_of(v))
#> # A tibble: 32 × 1
#> mp2
#> <dbl>
#> 1 21
#> 2 21
#> 3 22.8
#> 4 21.4
#> 5 18.7
#> 6 18.1
#> 7 14.3
#> 8 24.4
#> 9 22.8
#> 10 19.2
#> # … with 22 more rows But When tidy selection is used purely for selection, like it is here, we've started tightening up our checks to ensure that the user isn't trying to also do renaming in the tidy selection. At best this would just slip by silently, but at worst it can confuse the user if they thought renaming should occur, and it can change the internal result of I agree that this probably isn't the best error, we can probably add some context to it like we do with this: library(tidyselect)
library(rlang)
eval_select(expr(all_of(x)), mtcars)
#> Error:
#> ! Problem while evaluating `all_of(x)`.
#> Caused by error in `as_indices_impl()`:
#> ! object 'x' not found I'm imagining something like: #> Error in `rename_with()`:
#> ! Problem while evaluating `all_of(x)`.
#> ! Can't rename variables in this context. |
Closing in favor of r-lib/tidyselect#336 |
Thanks @DavisVaughan! Could the renaming be documented? I ran into a related issue again, and could not find a hint in the documentation of 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
sel <- c("mpg", "cyl")
mtcars %>% mutate(across(all_of(sel), ~.x * -1)) %>% summarise(mean(mpg), mean(cyl))
#> mean(mpg) mean(cyl)
#> 1 -20.09062 -6.1875
sel2 <- c("mpg", my = "cyl")
mtcars %>% mutate(across(all_of(sel2), ~.x * -1)) %>% summarise(mean(mpg), mean(cyl))
#> mean(mpg) mean(cyl)
#> 1 -20.09062 6.1875 Created on 2023-02-22 with reprex v2.0.2 |
There is an existing request to add some examples of named |
I was trying to rename variables quite deep in a function and ran into an odd error message for some input:
Can't rename variables in this context.
Apparently, that happens when the input toall_of
is named ... I would expect that to make no difference here; if it is to be discouraged for some reason then a clearer error message would be very helpful.Created on 2023-02-19 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: