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

Provide clear error message when col_select includes no columns #1419

Closed
LukasWallrich opened this issue Aug 21, 2022 · 3 comments
Closed

Provide clear error message when col_select includes no columns #1419

LukasWallrich opened this issue Aug 21, 2022 · 3 comments

Comments

@LukasWallrich
Copy link

When col_select contains no columns, it would be great to get a clear error message (or possibly just an empty dataframe) ... in any case, the current cryptic error message made debugging rather hard when this happened in a real context.

library(readr)
temp <- tempfile()
write_csv(iris, temp)
read_csv(temp, col_select = all_of(""))
#> Error in UseMethod("collector_value"): no applicable method for 'collector_value' applied to an object of class "c('collector_skip', 'collector')"

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

@hadley
Copy link
Member

hadley commented Jul 31, 2023

Looks like we now get nice messages for free thanks to tidyselect improvements:

library(readr)
read_csv(readr_example("mtcars.csv"), col_select = all_of("x"))
#> Error in `all_of()`:
#> ! Can't subset columns that don't exist.
#> ✖ Column `x` doesn't exist.

Created on 2023-07-31 with reprex v2.0.2

@hadley hadley closed this as completed Jul 31, 2023
@LukasWallrich
Copy link
Author

In the case I struggled with (where the selection is empty) the error message is not quite as nice as it is not clear what the subscript is - but certainly much better, and probably good enough.

library(readr)
read_csv(readr_example("mtcars.csv"), col_select = all_of(""))
#> Error in `all_of()`:
#> ! Can't subset columns.
#> ✖ Subscript can't contain the empty string.
#> ✖ It has an empty string at location 1.
#> Backtrace:
#>      ▆
#>   1. ├─readr::read_csv(readr_example("mtcars.csv"), col_select = all_of(""))
#>   2. │ └─vroom::vroom(...)
#>   3. │   └─vroom:::vroom_(...)
#>   4. ├─vroom (local) `<fn>`(`<col_spec>`, 11L, `<chr>`, all_of(""), "unique")
#>   5. │ ├─names(spec$cols) %in% ...
#>   6. │ └─tidyselect::vars_select(names(spec$cols), !!col_select, .strict = FALSE)
#>   7. │   └─tidyselect:::eval_select_impl(...)
#>   8. │     ├─tidyselect:::with_subscript_errors(...)
#>   9. │     │ └─rlang::try_fetch(...)
#>  10. │     │   └─base::withCallingHandlers(...)
#>  11. │     └─tidyselect:::vars_select_eval(...)
#>  12. │       └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#>  13. │         └─tidyselect:::eval_c(expr, data_mask, context_mask)
#>  14. │           └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#>  15. │             └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#>  16. │               └─tidyselect:::eval_context(expr, context_mask, call = error_call)
#>  17. │                 ├─tidyselect:::with_chained_errors(...)
#>  18. │                 │ └─rlang::try_fetch(...)
#>  19. │                 │   ├─base::tryCatch(...)
#>  20. │                 │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  21. │                 │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  22. │                 │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  23. │                 │   └─base::withCallingHandlers(...)
#>  24. │                 └─rlang::eval_tidy(as_quosure(expr, env), context_mask)
#>  25. ├─tidyselect::all_of("")
#>  26. │ └─tidyselect:::as_indices_impl(x, vars = vars, strict = TRUE)
#>  27. │   └─tidyselect:::chr_as_locations(x, vars, call = call, arg = arg)
#>  28. │     └─vctrs::vec_as_location(...)
#>  29. └─vctrs (local) `<fn>`()
#>  30.   └─vctrs:::stop_subscript_empty(i = i, call = call)
#>  31.     └─rlang::cnd_signal(...)

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

@hadley
Copy link
Member

hadley commented Aug 3, 2023

It says all_of() in the error, which should hopefully get you to the point pretty quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants