Easy way to export multiple data.frame to multiple Excel worksheets

I am surprised to find that there is no easy way to export multiple data.frame to multiple worksheets of an Excel file? I tried xlsx package, seems it can only write to one sheet (override old sheet); I also tried WriteXLS package, but it gives me error all the time...

My code structure is like this: by design, for each iteration, the output dataframe (tempTable) and the sheetName (sn) got updated and exported into one tab.

for (i in 2 : ncol(code)){
...
tempTable <- ...
sn <- ...
WriteXLS("tempTable", ExcelFileName = "C:/R_code/../file.xlsx",
SheetNames = sn);
}

I can export to several cvs files, but there has to be an easy way to do that in Excel, right?

248281 次浏览

我不熟悉 WriteXLS软件包; 我通常使用 XLConnect:

library(XLConnect)
##
newWB <- loadWorkbook(
filename="F:/TempDir/tempwb.xlsx",
create=TRUE)
##
for(i in 1:10){
wsName <- paste0("newsheet",i)
createSheet(
newWB,
name=wsName)
##
writeWorksheet(
newWB,
data=data.frame(
X=1:10,
Dataframe=paste0("DF ",i)),
sheet=wsName,
header=TRUE,
rownames=NULL)
}
saveWorkbook(newWB)

这当然可以向量化,如上面@joran 提到的,但是为了快速生成动态工作表名称,我使用了 for循环来演示。

我在 loadWorkbook中使用了 create=TRUE参数,因为我创建了一个新的。Xlsx 文件,但是如果您的文件已经存在,那么您不必指定它,因为默认值是 FALSE

下面是创建的工作簿的一些截图:

enter image description here

enter image description here

enter image description here

可以使用 xlsx包对多个工作表进行写操作。您只需要为每个数据帧使用不同的 sheetName,并添加 append=TRUE:

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

另一种选择是在 R/xlsx 代码中执行所有操作,然后在最后保存工作簿,这种选择使您能够更好地控制格式设置和数据框架放置的位置。例如:

wb = createWorkbook()


sheet = createSheet(wb, "Sheet 1")


addDataFrame(dataframe1, sheet=sheet, startColumn=1, row.names=FALSE)
addDataFrame(dataframe2, sheet=sheet, startColumn=10, row.names=FALSE)


sheet = createSheet(wb, "Sheet 2")


addDataFrame(dataframe3, sheet=sheet, startColumn=1, row.names=FALSE)


saveWorkbook(wb, "My_File.xlsx")

如果您觉得有用的话,这里有一些有趣的辅助函数,它们使得使用 xlsx向电子表格添加格式化、元数据和其他特性变得更加容易: Http://www.sthda.com/english/wiki/r2excel-read-write-and-format-easily-excel-files-using-r-software

还可以使用 openxlsx 库将多个数据集导出到单个工作簿中的多个工作表。Openxlsx 相对于 xlsx 的优势在于 openxlsx 消除了对 Java 库的依赖。

Write a list of data.frames to individual worksheets using list names as worksheet names.

require(openxlsx)
list_of_datasets <- list("Name of DataSheet1" = dataframe1, "Name of Datasheet2" = dataframe2)
write.xlsx(list_of_datasets, file = "writeXLSX2.xlsx")

对我来说,WriteXLS提供了您正在寻找的功能。由于您没有指定它返回哪些错误,因此我向您展示一个示例:

例子

library(WriteXLS)
x <- list(sheet_a = data.frame(a=letters), sheet_b = data.frame(b = LETTERS))
WriteXLS(x, "test.xlsx", names(x))

解释

如果 x是:

  • a list of data frames, each one is written to a single sheet
  • (R 对象的)字符向量,每个对象写入一张纸
  • 另外,还可以看看帮助说明:

More on usage

?WriteXLS

图示:

`x`: A character vector or factor containing the names of one or
more R data frames; A character vector or factor containing
the name of a single list which contains one or more R data
frames; a single list object of one or more data frames; a
single data frame object.

解决方案

对于您的示例,您需要在循环期间收集一个列表中的所有 data.frame,并在循环结束后使用 WriteXLS

会议信息

  • R3.2.4
  • WriteXLS 4.0.0

由于数据量很小,R 有许多包和函数,可以根据您的需要使用它们。

虽然 write.xlsx、 write.xlsx2、 XLconnect 也可以完成这些工作,但是与 Openxlsx相比,它们有时候是 慢慢来

因此,如果您正在处理大型数据集并遇到了 Java 错误。我会建议有一个 “ openxlsx”的外观,这是真正令人敬畏的,并减少到1/12的时间。

我测试了所有这些,最后我对 openxlsx 功能的性能印象深刻。

下面是将多个数据集写入多个工作表的步骤。

 install.packages("openxlsx")
library("openxlsx")


start.time <- Sys.time()


# Creating large data frame
x <- as.data.frame(matrix(1:4000000,200000,20))
y <- as.data.frame(matrix(1:4000000,200000,20))
z <- as.data.frame(matrix(1:4000000,200000,20))


# Creating a workbook
wb <- createWorkbook("Example.xlsx")
Sys.setenv("R_ZIPCMD" = "C:/Rtools/bin/zip.exe") ## path to zip.exe

Sys.setenv (“ R _ ZIPCMD”= “ C:/Rtools/bin/zip.exe”)必须是静态的,因为它引用了 Rtools 的一些实用程序。

注意: 如果您的系统上没有安装 Rtools,请先安装它,以获得平稳的体验。以下连结供你参考: (选择合适的版本)

Https://cran.r-project.org/bin/windows/rtools/ 按照下面的链接检查选项(安装时需要选中所有复选框)

Https://cloud.githubusercontent.com/assets/7400673/12230758/99fb2202-b8a6-11e5-82e6-836159440831.png

    # Adding a worksheets : parameters for addWorksheet are 1. Workbook Name 2. Sheet Name


addWorksheet(wb, "Sheet 1")
addWorksheet(wb, "Sheet 2")
addWorksheet(wb, "Sheet 3")


# Writing data in to respetive sheets: parameters for writeData are 1. Workbook Name 2. Sheet index/ sheet name 3. dataframe name


writeData(wb, 1, x)


# incase you would like to write sheet with filter available for ease of access you can pass the parameter withFilter = TRUE in writeData function.
writeData(wb, 2, x = y, withFilter = TRUE)


## Similarly writeDataTable is another way for representing your data with table formatting:


writeDataTable(wb, 3, z)


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


end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

Openxlsx 包非常适合从 Excel 文件中读取和写入大量数据,并且在 Excel 中有很多自定义格式的选项。

有趣的事实是,我们在这里不必为 Java 堆内存而烦恼。

城里有个新图书馆,来自 rOpenSci: Writexl

便携式,轻量级的数据框架,以 xlsx 出口商的基础上 不需要 Java 或 Excel

我发现它比上面的建议(使用开发版本)更好更快:

library(writexl)
sheets <- list("sheet1Name" = sheet1, "sheet2Name" = sheet2) #assume sheet1 and sheet2 are data frames
write_xlsx(sheets, "path/to/location")

I do it in this way for openxlsx using following function

mywritexlsx<-function(fname="temp.xlsx",sheetname="Sheet1",data,
startCol = 1, startRow = 1, colNames = TRUE, rowNames = FALSE)
{
if(! file.exists(fname))
wb = createWorkbook()
else
wb <- loadWorkbook(file =fname)
sheet = addWorksheet(wb, sheetname)


writeData(wb,sheet,data,startCol = startCol, startRow = startRow,
colNames = colNames, rowNames = rowNames)
saveWorkbook(wb, fname,overwrite = TRUE)
}

这里有很多好的答案,但有些有点过时了。如果你想添加进一步的工作表到一个单一的文件,然后这是我发现的方法为我工作。为了清晰起见,下面是 openxlsx4.0版的工作流

# Create a blank workbook
OUT <- createWorkbook()


# Add some sheets to the workbook
addWorksheet(OUT, "Sheet 1 Name")
addWorksheet(OUT, "Sheet 2 Name")


# Write the data to the sheets
writeData(OUT, sheet = "Sheet 1 Name", x = dataframe1)
writeData(OUT, sheet = "Sheet 2 Name", x = dataframe2)


# Export the file
saveWorkbook(OUT, "My output file.xlsx")

剪辑

我现在已经尝试了一些其他的答案,事实上我真的很喜欢@Syed 的。它没有充分利用 openxlsx的所有功能,但是如果您想要一个快速而简单的导出方法,那么这可能是最直接的方法。

我一直这样,我所做的就是

WriteXLS::WriteXLS(
all.dataframes,
ExcelFileName = xl.filename,
AdjWidth = T,
AutoFilter = T,
FreezeRow = 1,
FreezeCol = 2,
BoldHeaderRow = T,
verbose = F,
na = '0'
)

所有的数据帧都来自这里

all.dataframes <- vector()
for (obj.iter in all.objects) {
obj.name <- obj.iter
obj.iter <- get(obj.iter)
if (class(obj.iter) == 'data.frame') {
all.dataframes <- c(all.dataframes, obj.name)
}

显然这里的套路会更好

一个适用的版本。

library(data.table)
library(xlsx)


path2txtlist <- your.list.of.txt.files
wb <- createWorkbook()
lapply(seq_along(path2txtlist), function (j) {
sheet <- createSheet(wb, paste("sheetname", j))
addDataFrame(fread(path2txtlist[j]), sheet=sheet, startColumn=1, row.names=FALSE)
})


saveWorkbook(wb, "My_File.xlsx")

我遇到了这个问题,我用这种方法解决了它:

library(openxlsx) # loads library and doesn't require Java installed


your_df_list <- c("df1", "df2", ..., "dfn")


for(name in your_df_list){
write.xlsx(x = get(name),
file = "your_spreadsheet_name.xlsx",
sheetName = name)
}

这样,如果有大量数据框架要写入 Excel,就不必手动创建很长的列表。

我经常使用打包的 里约热内卢来出口各种产品。使用 rio,您可以输入一个列表,命名每个选项卡并指定数据集。Rio 编译其他 in/out 包,并且为了导出到 Excel,使用 openxlsx。

library(rio)


filename <- "C:/R_code/../file.xlsx"


export(list(sn1 = tempTable1, sn2 = tempTable2, sn3 = tempTable3), filename)

tidy way of taking one dataframe and writing sheets by groups:

library(tidyverse)
library(xlsx)
mtcars %>%
mutate(cyl1 = cyl) %>%
group_by(cyl1) %>%
nest() %>%
ungroup() %>%
mutate(rn = row_number(),
app = rn != 1,
q = pmap(list(rn,data,app),~write.xlsx(..2,"test1.xlsx",as.character(..1),append = ..3)))