简体   繁体   English

在R中使用“ xlsx”和“ openxlsx”包导出Excel文件时发生冲突

[英]Clash when exporting excel file using “xlsx” and “openxlsx” packages in R

I have a data which has a size similar to "a" below 我有一个数据,其大小类似于下面的“ a”

library(openxlsx)
a <- list()
names(a) <- paste("sheet", seq_along(fulldata), sep="_")  ### name for each sheet
for (i in 1:172) {
a[[i]] <- matrix(i,30,60)
}

write.xlsx(a, "a.xlsx")

If I run the code above, few seconds later, R closes automatically. 如果我运行上面的代码,几秒钟后,R将自动关闭。

library(xlsx)
options(java.parameters = "-Xmx4000m")
a <- list()
for (i in 1:172) {
a[[i]] <- matrix(i,30,60)
}
n <- paste("sheet", seq_along(fulldata), sep="_") ### name for each sheet

for (i in 1:172) {
write.xlsx(a[[i]], "c.xlsx", sheetName=n[[i]], append=TRUE)
}

If I run the code above, after 10 minutes, it returns an error about lack of memory. 如果我在10分钟后运行以上代码,则会返回有关内存不足的错误。 I used 我用了

options(java.parameters = "-Xmx4000m") 

to increase the memory to be used but still, it says lack of memory. 增加要使用的内存,但仍然表示内存不足。
Both of them works fine with small data but it doens't work when I try to export 172 sheets all at once. 两者都适用于少量数据,但是当我尝试一次全部导出172张纸时,它们都无法正常工作。 I need all the 172 sheets to be included in one excel file. 我需要将所有172张纸都包含在一个excel文件中。

Creating the sheets using lapply may help alleviate the memory issue. 使用lapply创建工作表可能有助于减轻内存问题。

library(xlsx)

# Create the list of matrices
a <- list()
for (i in 1:172) {
  a[[i]] <- matrix(i,30,60)
}

# Set names for the matrices
names(a) <- seq_along(a)

# Create a workbook object
wb <- createWorkbook()

# Add each matrix to it's own worksheet inside of the workbook
lapply(seq_along(a), function(matrices, matrix.names, i){
                       ws <- createSheet(wb, matrix.names[[i]])
                       addDataFrame(matrices[[i]], ws)
                     }, matrices = a, matrix.names = names(a))

# Set the file path to save the workbook to
(output.path <- file.path(tempdir(), "output.xlsx"))

# Save the workbook
saveWorkbook(wb, output.path)

暂无
暂无

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

相关问题 使用 openxlsx 在 R 中使用 write.xlsx 创建的 Excel 文件的密码保护 - Password protecting an Excel file created using write.xlsx in R with openxlsx 使用 openxlsx 导出 R 数据框时出错(“zipr 中的错误”) - Error when exporting R data frame using openxlsx ("Error in zipr") openxlsx无法读取R中的.xlsx文件 - openxlsx not able to read from .xlsx file in R 在 R 中使用 openxlsx 时避免 Excel 隐式交集运算符 - avoid Excel implicit intersection operator when using openxlsx in R 使用 xlsx package 从 r 导出到 excel 时如何格式化千位分隔符? - How to format thousands separator when exporting from r to excel using xlsx package? 使用 R 包 OFFICER 和 RVG 将 plot 从 R 导出到 pptx 文件时无法指定幻灯片大小 - Unable to specify slide size when exporting a plot from R to a pptx file using R packages OFFICER and RVG 使用 openxlsx 将 Excel 数据导入 R:文件中的错误(con,“r”):无效的“描述”参数 - Import Excel data into R using openxlsx: Error in file(con, "r") : invalid 'description' argument R使用openxlsx编写excel时as.vector(x,“ character”)中的错误 - R Error in as.vector(x, “character”) when using openxlsx to write excel 使用 R 和 Openxlsx 在单个 Excel 文件中输出数据框列表作为工作表 - Use R and Openxlsx to output a list of dataframes as worksheets in a single Excel file R / openxlsx /在Excel文件中查找第一个非空单元格 - R / openxlsx / Finding the first non-empty cell in Excel file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM