readxl icon indicating copy to clipboard operation
readxl copied to clipboard

Number >1 converted to boolean TRUE without any warning

Open raffaem opened this issue 1 year ago • 1 comments

In the following example, readxl returns TRUE instead of 599, without any warnings nor errors.

This doesn't happen with xlsx, openxlsx or openxlsx2.

I understand this is related to readxl guessing the column type using the first 1000 rows, but it should return at least a warning if not an error if the data it encounters is incompatible with the column type it guessed.

test.xlsx

indf <- readxl::read_xlsx("test.xlsx")
indf %>% filter(project_id==1001) %>% pull("budget")
# returns TRUE
# should return 599

raffaem avatar Aug 23 '24 09:08 raffaem

Yes, I agree. I was recently bitten by this problem - took about a day to figure out!

An alternate solution would be return a character vector rather than a logical vector. In this way, the full information can be captured, i.e. "599" which can be converted to numeric), or the calling program is more likely to return an error when doing arithmetic operations on a character column.

Carl Schwarz

cschwarz-stat-sfu-ca avatar Feb 04 '25 02:02 cschwarz-stat-sfu-ca