-
Notifications
You must be signed in to change notification settings - Fork 421
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
Use tidyselect::eval_select(allow_positional = FALSE)
for id_cols
when possible
#1541
Comments
In my case, I could specify
|
Minimal reprex library(tidyr)
# Set a seed for consistency
set_seed = 312
df <- data.frame(
site = c(rep((1:3), each = 2)),
time = rep(c("day", "night"), 3),
temp = rnorm(6, rep(c(20, 10), each = 1), 4),
day_of_exp = c(rep(1, 2), rep(11, 2), rep(21, 2))
)
# Columns 2 (time) and 3 (temp) are removed before evaluating the tidyselection
# for `id_cols`
df %>% pivot_wider(
id_cols = c(1, 4),
names_from = time,
values_from = temp
)
#> Error in `rethrow_id_cols_oob()`:
#> ! `i` must be a single string, not an integer vector.
#> ℹ This is an internal error that was detected in the tidyr package.
#> Please report it at <https://github.com/tidyverse/tidyr/issues> with a reprex
#> (<https://tidyverse.org/help/>) and the full backtrace. Comes from:
But I'd argue that using positions to perform the selection here is actually not a great thing to do. You should really prefer selecting with names. Ideally I think we'd keep the current behavior but prevent selecting by position through We've decided to wait for |
id_cols
tidyselect::eval_select(allow_positional = FALSE)
for id_cols
when possible
@DavisVaughan the reason I want to use positions is that it seems more parsimonious and for the lazy coder like me. However, using indices are more risky than specifying names. |
In going through some old code that I hadn't touched in a while, I found that
pivot_wider
was not working and was giving me the following error:Digging deeper, I think I've sorted out the overarching timing of the issue, and it is due to changes made between
tidyr
1.1.4 and 1.2.0 that changes the behavior, and between versions 1.2.1 and 1.3.0 where the error message changed to the above. I've included a reprex below that shows behavior in ver 1.1.4 and 1.3.1:Created on 2024-02-15 with reprex v2.1.0
A workaround is to use the column names in the
id_cols
argument; however, given older versions of thepivot_wider
function allowed the use of column numbers and there is an increased potential for error when entering names, it would be nice ifpivot_wider
functioned like it did intidyr
1.1.4 and before. I would also advocate for a return to the more descriptive error message when choosing variables that don't exist if that is possible, and including a descriptive warning/error if one attempts to choose a variable that is included in thenames_from
orvalues_from
arguments.If the removal of straightforward selection by column number is a feature and not a bug, I would request an update for the help and vignette to make this even clearer.
The text was updated successfully, but these errors were encountered: