[英]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.