简体   繁体   English

openxlsx::write.xlsx 改写现有工作表 append

[英]openxlsx::write.xlsx overwriting existing worksheet instead append

The openxlsx::write.xlsx function is overwriting spreadsheet instead of adding another tab. openxlsx::write.xlsx function 正在覆盖电子表格而不是添加另一个选项卡。

I tried do follow some orientations of Stackoverflow, but without sucess.我尝试遵循 Stackoverflow 的一些方向,但没有成功。

dt.escrita <- format(Sys.time(), '%Y%m%d%H%M%S')

write.xlsx( tbl.messages
           ,file = paste('.\\2_Datasets\\messages_',dt.escrita,'.xlsx')
           ,sheetName = format(Sys.time(), '%d-%m-%y')
           ,append = FALSE)

write.xlsx( tbl.dic.dados
            ,file = paste('.\\2_Datasets\\messages_',dt.escrita,'.xlsx')
            ,sheetName = 'Dicionario_Dados'
            ,append = TRUE)

A spreadsheet with two tabs named: 30-07-19 and Dicionario_Dados.包含两个标签的电子表格:30-07-19 和 Dicionario_Dados。

Not sure if I understand correctly, you want to create one xlsx file with two tabs/sheets?不确定我是否理解正确,您想创建一个带有两个选项卡/工作表的 xlsx 文件吗? Then I usually first create the sheets and then write into each sheet seperatly (This is different from adding data to the same sheet by appending it).然后我通常首先创建工作表,然后单独写入每个工作表(这与通过附加将数据添加到同一张工作表不同)。

library("openxlsx")
mtcars1 <- mtcars %>% filter(cyl == 4)
mtcars2 <- mtcars %>% filter(cyl == 6)

wb <- createWorkbook()
addWorksheet(wb, "mtcars1")
addWorksheet(wb, "mtcars2")

writeData(wb, "mtcars1", mtcars1, startRow = 1, startCol = 1)
writeData(wb, "mtcars2", mtcars2, startRow = 1, startCol = 1)

saveWorkbook(wb, file = "excel_test.xlsx", overwrite = TRUE)

Update : Just wondering why I never used the append argument in openxlsx (which is my standard package for read/writing excel).更新:只是想知道为什么我从不使用openxlsxappend参数(这是我用于读/写 excel 的标准包)。 It seems like there is no such argument to neither of the three functions write.xlsx() , writeData() , writeDataTable() .三个函数write.xlsx()writeData()writeDataTable()似乎都没有这样的论点。 At least it's not in the documentation.至少它不在文档中。

The function does not seem to throw an error when called with unknown arguments, for example the call below has a non-existing somearg , but returns no error.该函数在使用未知参数调用时似乎不会引发错误,例如下面的调用具有不存在的somearg ,但不返回错误。

write.xlsx(mtcars2,
           file = "excel_test.xlsx",
           sheetName = "mtcars1",
           somearg = TRUE)

Update 2 To append data to an existing table you could read in the number of rows of the existing worksheet, add +1 and use this values as startRow:更新 2要将数据附加到现有表中,您可以读取现有工作表的行数,添加 +1 并将此值用作 startRow:

wb2 <- loadWorkbook(file = "excel_test.xlsx")

writeData(wb2,
          "mtcars1",
          mtcars2,
          colNames = FALSE,
          startRow = nrow(readWorkbook("excel_test.xlsx"))+1)
#Fixed the call to nrow, instead of ncol.

saveWorkbook(wb2, file = "excel_test.xlsx", overwrite = TRUE)

Write different dataframes to different sheets.将不同的数据帧写入不同的工作表。

library(xlsx)
write.xlsx(dataframe1, file="filename.xlsx", sheetName="sheet1")
write.xlsx(dataframe2, file="filename.xlsx", sheetName="sheet2", append=TRUE)

You can simply use a named list of dataframes:您可以简单地使用数据框的命名列表:

library(openxlsx)

df_lst <- setNames(list(tbl.messages, tbl.dic.dados), as.list(c(format(Sys.time(), '%d-%m-%y'), 'Dicionario_Dados')))

write.xlsx( df_lst, file = paste0('.\\2_Datasets\\messages_',dt.escrita,'.xlsx') )

The write.xlsx function of the xlsx package does have an append argument xlsx package 的 write.xlsx function 确实有一个 append 参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM