2017-09-29 12 views
1

ループ内でwrite.xlsxを使用しているときに空のデータフレームを処理するにはどうすればよいですか?write.xlsxで空のデータフレームを処理する

以下は、ソース( "./ Scripts/Analysis_details.R")がデータフレームが作成されたrファイルを参照しているループの外観です。私は取得していますエラーがある

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)} 

...

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

答えて

2

私は私のスクリプトに変更write.xlsx()機能を置くことによってこの問題を回避することができました。データフレームにゼロ行がある場合、.write_block()はスキップされ、ファイルは列名だけで保存されます。オリジナルの.write_block()関数もスクリプトにコピーする必要があります。

write.xlsx.custom <- function(x, file, sheetName="Sheet1", 
         col.names=TRUE, row.names=TRUE, append=FALSE, showNA=TRUE) 
{ 
    if (!is.data.frame(x)) 
     x <- data.frame(x) # just because the error message is too ugly 

    iOffset <- jOffset <- 0 
    if (col.names) 
     iOffset <- 1 
    if (row.names) 
     jOffset <- 1 

    if (append && file.exists(file)){ 
     wb <- loadWorkbook(file) 
    } else { 
     ext <- gsub(".*\\.(.*)$", "\\1", basename(file)) 
     wb <- createWorkbook(type=ext) 
    } 
    sheet <- createSheet(wb, sheetName) 

    noRows <- nrow(x) + iOffset 
    noCols <- ncol(x) + jOffset 
    if (col.names){ 
     rows <- createRow(sheet, 1)     # create top row 
     cells <- createCell(rows, colIndex=1:noCols) # create cells 
     mapply(setCellValue, cells[1,(1+jOffset):noCols], colnames(x)) 
    } 
    if (row.names)    # add rownames to data x     
     x <- cbind(rownames=rownames(x), x) 

    if(nrow(x) > 0) { 
     colIndex <- seq_len(ncol(x)) 
     rowIndex <- seq_len(nrow(x)) + iOffset 

     .write_block(wb, sheet, x, rowIndex, colIndex, showNA) 
    } 
    saveWorkbook(wb, file) 

    invisible() 
} 

私はhttps://github.com/cran/xlsx/blob/master/R/write.xlsx.R

で本来の機能を発見しました
関連する問題