wb must be a Workbok

Does anyone know what this error is from? That's how the error displays also.

badRowCount <- nrow(badie)
wb <- createWorkbook()
if (badRowCount > 0) {
	write.csv(badie,'bad.csv',row.names=FALSE,na='')
	addWorksheet(wb,'bads')
	writeData(wb,'bads',badie)
}
> wb <- createWorkbook()
> if (nrow(badie) > 0) {
+ 	write.csv(badie,'bad.csv',row.names=FALSE,na='')
+ 	addWorksheet(wb,'bads')
+ 	writeData(wb,'bads',badie)
+ }
Error: wb must be a Workbok

I get a different error with the fundamental problem being the use of nrow(badie). If I use length(badie), the code runs.

library(openxlsx)
badie <- 10
wb <- createWorkbook()
if (nrow(badie) > 0) {
   write.csv(badie,'bad.csv',row.names=FALSE,na='')
   addWorksheet(wb,'bads')
   writeData(wb,'bads',badie)
 }
Error in if (nrow(badie) > 0) { : argument is of length zero
#This works
if (length(badie) > 0) {
   write.csv(badie,'bad.csv',row.names=FALSE,na='')
   addWorksheet(wb,'bads')
   writeData(wb,'bads',badie)
 }

I had to update my OP. badie is a dataframe.

I do not see any problem with your code and it runs for me if I make a data frame named badie. If you run the following code, does it work for you?

library(openxlsx)

badie <- data.frame(A=1:2)
wb <- createWorkbook()
if (nrow(badie) > 0) {
  write.csv(badie,'bad.csv',row.names=FALSE,na='')
  addWorksheet(wb,'bads')
  writeData(wb,'bads',badie)
}
sheets(wb)
#> [1] "bads"

Created on 2022-09-28 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.