tidyverse / vroom

Fast reading of delimited files
https://vroom.r-lib.org
Other
622 stars 60 forks source link

cryptic error messages when col_select has no valid columns #521

Open khusmann opened 10 months ago

khusmann commented 10 months ago

Hello! Was getting this cryptic error message in readr::read_delim and traced it to vroom. It occurs when col_select has no valid columns. MWE:

foo <- "a,b,c\n1,2,3\n"
vroom(I(foo), delim=",", col_select=c(d))

# Output:
# Error in UseMethod("collector_value") :                                                                                                                 
#  no applicable method for 'collector_value' applied to an object of class "c('collector_skip', 'collector')"

vroom(I(foo), delim=",", col_select=c(d, e, f))

# Output:
# Error in UseMethod("collector_value") :                                                                                                                 
#  no applicable method for 'collector_value' applied to an object of class "c('collector_skip', 'collector')"

Interestingly, if you read at least one valid column, you get a much nicer error:

vroom(I(foo), delim=",", col_select=c(a, d))
# Output:
# Error in `vroom_select()`:                                                                                                                              
# ! Can't subset columns that don't exist.
# ✖ Column `d` doesn't exist.

I tested on both vroom v 1.6.1 and 1.6.4 and got the same behavior.

Seems related to this issue reported in readr, but not quite. Empty selections using all_of() (as reported there) give nice errors as expected, so this appears to be a separate thing:

vroom(I(foo), delim=",", col_select=c(all_of("d")))
# Output:
# Error in `all_of()`:                                                                                                                                    
# ! Can't subset columns that don't exist.
# ✖ Column `d` doesn't exist.

Cheers!

lgaborini commented 2 months ago

Interestingly, I get the cryptic error with any_of() but not with all_of() (as above):

vroom(I(foo), delim=",", col_select=c(any_of("d")))
#> Error in UseMethod("collector_value"): no applicable method for 'collector_value' applied to an object of class "c('collector_skip', 'collector')"

On {vroom} 1.6.5.