xlsx icon indicating copy to clipboard operation
xlsx copied to clipboard

Named colClasses doesn't work

Open Stikus opened this issue 3 years ago • 2 comments

According to documentation: https://www.rdocumentation.org/packages/xlsx/versions/0.6.5/topics/read.xlsx image

Here is my simple test xlsx file: image

  • Without colClasses:
> META <- xlsx::read.xlsx('1.xlsx', sheetIndex = 1)
> str(META)
'data.frame':	1 obs. of  3 variables:
 $ A: chr "ABC"
 $ B: Date, format: "1970-01-01"
 $ C: num 2000
  • With all classes described:
> META <- xlsx::read.xlsx('1.xlsx', sheetIndex = 1, colClasses = c(A = "character", B = "Date", C = 'character'))
> str(META)
'data.frame':	1 obs. of  3 variables:
 $ A: chr "ABC"
 $ B: Date, format: "1970-01-01"
 $ C: chr "0000"
  • Looks like all good, but if you remove first class F = "character":
> META <- xlsx::read.xlsx('1.xlsx', sheetIndex = 1, colClasses = c(B = "Date", C = 'character'))
> str(META)
'data.frame':	1 obs. of  3 variables:
 $ A: Date, format: NA
 $ B: chr "0"
 $ C: num 2000
Warning message:
In as.POSIXlt.Date(x) : NAs introduced by coercion

colClasses doesn't respect names - only position in vector.

Stikus avatar Jul 05 '22 14:07 Stikus

Thanks for the great reprex! This should make it much easier to isolate and fix (although I haven't looked to see how complex a fix is).

Just for future reference on other projects, the reprex package can provide even more simple "copy and use" examples. It's a bit tricky with the xlsx package, but the way we do it in our tests is often to write out the file in question to a temp file and then import it.

colearendt avatar Jul 13 '22 10:07 colearendt

@colearendt Hello, I see that xlsx package doesn't receive any updated for long time, do you have any fix for this problem?

Stikus avatar Feb 16 '24 11:02 Stikus