xlsx
xlsx copied to clipboard
xlsx::write.xlsx() crashes on tibble with 2+ rows and row.names = F parameter
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
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)
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)
Is it a bug in xlsx::write.xlsx() or I can do something with it?
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
Thanks for the great reprex y'all! This definitely seems like a pretty straightforward fix!


