Handling empty data frame with write.xlsx

Viewed 3266

How do I handle empty data frames when i'm using write.xlsx within a loop?

Below is what the loop looks like, where source("./Scripts/Analysis_details.R") is referencing the r file where the data frames are created.

library(xlsx) 
    for (A in unique(df_base$A)) {
          df<- df_base[df_base$A==A,]
          source("./Scripts/Analysis_details.R")
          output_file = paste("./Output/report_", A, '_', Sys.Date(), ".xlsx", sep='')
          write.xlsx(df1, file=output_file, sheetName="df1", append=TRUE, row.names=FALSE, showNA = FALSE)
          write.xlsx(df2, file=output_file, sheetName="df2", append=TRUE, row.names=FALSE, showNA = FALSE)
          write.xlsx(df3, file=output_file, sheetName="df3", append=TRUE, row.names=FALSE, showNA = FALSE)
          write.xlsx(df4, file=output_file, sheetName="df4", append=TRUE, row.names=FALSE, showNA = FALSE)}

The error that I'm getting is...

Error in mapply(setCellValue, cells[seq_len(nrow(cells)), colIndex[ic]],  : zero-length inputs cannot be mixed with those of non-zero length
1 Answers
Related