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 message for select() when column doesn't exist #6200

Closed
mine-cetinkaya-rundel opened this issue Feb 27, 2022 · 5 comments
Closed

Error message for select() when column doesn't exist #6200

mine-cetinkaya-rundel opened this issue Feb 27, 2022 · 5 comments
Labels
columns ↔️ Operations on columns: mutate(), select(), rename(), relocate() feature a feature request or enhancement

Comments

@mine-cetinkaya-rundel
Copy link
Member

See below for a reprex for selecting a column that doesn't exist:

library(dplyr)

df <- tibble(x = 1:2, y = 3:4)
df
#> # A tibble: 2 × 2
#>       x     y
#>   <int> <int>
#> 1     1     3
#> 2     2     4
df %>% select(z)
#> Error in `stop_subscript()`:
#> ! Can't subset columns that don't exist.
#> x Column `z` doesn't exist.

Created on 2022-02-27 by the reprex package (v2.0.1)

I think the error should say Can't select columns that don't exist since subset is not the verb used in the code, and it actually means filter() in base R.

@hadley hadley added feature a feature request or enhancement columns ↔️ Operations on columns: mutate(), select(), rename(), relocate() labels Apr 15, 2022
@hadley
Copy link
Member

hadley commented Jul 21, 2022

It now says

library(dplyr, warn.conflicts = FALSE)

df <- tibble(x = 1:2, y = 3:4)
df %>% select(z)
#> Error in `select()`:
#> ! Can't subset columns that don't exist.
#> ✖ Column `z` doesn't exist.

Created on 2022-07-21 by the reprex package (v2.0.1)

Which I think is about as good as we can get, without customising the underlying vctrs error which would be a lot of work, for relatively little gain, IMO.

@hadley hadley closed this as completed Jul 21, 2022
@DavisVaughan
Copy link
Member

DavisVaughan commented Jul 21, 2022

This might not be that hard actually. tidyselect is already renaming it to subset and columns for us:

tidyselect:::with_subscript_errors
#> function (expr, type = "select", call = caller_env()) 
#> {
#>     tryCatch(expr, vctrs_error_subscript = function(cnd) {
#>         cnd$subscript_action <- subscript_action(type)
#>         cnd$subscript_elt <- "column"
#>         cnd_signal(cnd)
#>     })
#> }
#> <bytecode: 0x7f7dbd5df4f0>
#> <environment: namespace:tidyselect>
tidyselect:::subscript_action
#> function (type) 
#> {
#>     switch(validate_type(type), select = "subset", rename = "rename", 
#>         pull = "extract")
#> }
#> <bytecode: 0x7f7ddc4e69e0>
#> <environment: namespace:tidyselect>

So maybe tidyselect should say select = "select" here rather than select = "subset"? @lionel- what do you think?

We'd have to make "select" a valid option in vctrs.

library(dplyr, warn.conflicts = FALSE)

df <- tibble(x = 1:2, y = 3:4)

withCallingHandlers(
  select(df, z),
  vctrs_error_subscript_oob = function(cnd) {
    cnd$subscript_action <- "select"
    rlang::cnd_signal(cnd)
  }
)
#> Error in `cnd_subscript_action()`:
#> ! Internal error: `cnd$subscript_action` must be one of `subset`, `extract`, `assign`, `rename`, `remove`, or `negate`.

Alternatively we can also catch and rethrow the error in select() and make it say "select" too, if we want to keep it as "subset" in tidyselect.

@lionel-
Copy link
Member

lionel- commented Jul 23, 2022

That sounds good. We'll need a change in both vctrs and tidyselect, I've opened issues.

@hadley
Copy link
Member

hadley commented Jul 23, 2022

FWIW I stand by my original comment that this is more trouble than its worth. Having to change three packages in concert to modify a single word in an error message that is already pretty good doesn't feel like a great payoff to me.

@DavisVaughan
Copy link
Member

Fine by me

@hadley hadley closed this as completed Jul 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
columns ↔️ Operations on columns: mutate(), select(), rename(), relocate() feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants