我写了这个函数,它使用xlsx
包将一个或多个数据帧写入.xlsx文件。当给出相同的输入时(3个数据帧:23个变量的6185个盲,17个中的4个和3个中的2个),它会抛出大多数的错误,但不会全部当时。
有人能告诉我如何优化我的代码,更优雅地获得相同的结果,或者尽管有错误仍然继续吗?
这是控制台输出:
Running: WriteToFile()
WriteToFile:1
WriteToFile:4
Error in: Example Report
java.lang.OutOfMemoryError: GC overhead limit exceededReturning from: Example Report
这是函数:
WriteToFile <- function() {
# Write a data frame(s) out to .xlsx file
if(debug > 2) {message("WriteToFile:1")}
# If the file already exists today, then delete it
if(file.exists(paste0(report.name, '(', today(), ')', ".xlsx"))) {
if(debug > 2) {message("WriteToFile:2")}
writeLines(paste0("File '", report.name, '(', today(), ')', ".xlsx", "' already exists and will be replaced."))
flush.console()
file.remove(paste0(report.name, '(', today(), ')', ".xlsx"))
}
# Get tab names that were generated in ProcessOutputs()
tabs <- ls(pattern=paste0("\\.data$"), name=.GlobalEnv)
# If no tabs, send fail-mail
if(length(tabs) == 0) {
if(debug > 2) {message("WriteToFile:3")}
SendPerlMail(fail.mail=TRUE, fail.msg="No data for tabs in WriteToFile()")
return(-1)
}
# Write first tab to first sheet, then write any remaining tabs to additional sheets
else {
if(debug > 2) {message("WriteToFile:4")}
write.xlsx2(x=get(tabs[1]), file=paste0(report.name, ' (', today(), ')', ".xlsx"),
sheetName=substr(x=tabs[1], start=0, stop=nchar(tabs[1])-5), col.names=TRUE, row.names=FALSE, append=FALSE)
if(length(tabs) > 1) {
if(debug > 2) {message("WriteToFile:5")}
for(t in mget(tabs[2:length(tabs)])) {
write.xlsx2(x=t, file=paste0(report.name, ' (', today(), ')', ".xlsx"),
sheetName=substr(x=substitute(t), start=0, stop=nchar(t)-5), col.names=TRUE, row.names=FALSE, append=TRUE)
}
}
}
email.file <<- paste0(getwd(), "/", report.name, ' (', today(), ')', ".xlsx")
return(1)
}
函数中引用的外部变量是:
options(java.parameters = "-Xms2048m -Xmx4096m", "-XX:-UseGCOverheadLimit")
(我设置了这些Java参数,因为它们适用于另一篇文章中有相同GC错误的人)debug
= 3 report.name
=&#34;示例报告&#34; Example Query 1.data
=数据框架6185x23 Example Query 2.data
=数据框架为4x17 Example Query 3.data
=数据框2x3 有时会将WriteToFile:5
输出到控制台,有时它会成功而不会抛出错误。非常感谢任何帮助 - 我一直试图弄清楚为什么现在这几个小时都无法正常工作。