[英]Password protecting an Excel file created using write.xlsx in R with openxlsx
[英]Clash when exporting excel file using “xlsx” and “openxlsx” packages in R
我有一个数据,其大小类似于下面的“ 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")
如果我运行上面的代码,几秒钟后,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)
}
如果我在10分钟后运行以上代码,则会返回有关内存不足的错误。 我用了
options(java.parameters = "-Xmx4000m")
增加要使用的内存,但仍然表示内存不足。
两者都适用于少量数据,但是当我尝试一次全部导出172张纸时,它们都无法正常工作。 我需要将所有172张纸都包含在一个excel文件中。
使用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.