xlsx icon indicating copy to clipboard operation
xlsx copied to clipboard

xlsx::write.xlsx() crashes on tibble with 2+ rows and row.names = F parameter

Open asd-tm opened this issue 1 year ago • 2 comments

The issue has been reported at StackOverflow. When I call xlsx::write.xlsx() on a tibble with 2+ rows it crashes:

df <- structure(list(`region name` = c("Reg 1", "Reg 2"), 
                     `value` = c(1.38755717255717, 1.26297588082289)), 
                row.names = c(NA, -2L), 
                class = c("tbl_df", "tbl", "data.frame")) 

xlsx::write.xlsx(df, file = "myfilename.xlsx", row.names = F)

and returns:

Error in if (is.na(value)) { : the condition has length > 1

1st option If I call the same script setting default row.names = T it executes properly but adds an unneeded column with row number.

xlsx::write.xlsx(df, file = "myfilename.xlsx", 
    row.names = T) # I can skip the parameter

enter image description here

2nd option If I subset only one row from the tibble the script executes entirely as expected.

xlsx::write.xlsx(df[1, ], file = "myfilename.xlsx", row.names = F)

enter image description here

3rd option If I convert data to data frame it executes without an error but modifies column names due to data.frame restrictions:

xlsx::write.xlsx(data.frame(df), file = "myfilename.xlsx", row.names = F)

enter image description here

Is it a bug in xlsx::write.xlsx() or I can do something with it?

asd-tm avatar Jul 03 '24 06:07 asd-tm

Just ran into this myself and it turns out converting to data frame first with as.data.frame() fixes the issue. This should be an easy one for the maintainers to fix.

See #120

bwu62 avatar Jul 12 '24 22:07 bwu62

Thanks for the great reprex y'all! This definitely seems like a pretty straightforward fix!

colearendt avatar Aug 27 '24 17:08 colearendt