我在R中使用包'xlsx'来创建和保存包含多个工作表的Excel文件。您可以在下面看到我如何创建和保存文件。但是,它大部分时间都不起作用。它给了我以下错误:
.jcall错误(" RJavaTools"," Ljava / lang / Object;"," invokeMethod", cl,:
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:未能 save:保存包时发生错误:该部分 /docProps/app.xml无法使用marshaller保存在流中 org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@4bb4de6a
library(gridExtra) ; library(grid) ; library(tidyr) ; library(reshape2) ; library(plyr) ; library(lubridate) ; library(xlsx) ; require(rJava)
var<-0
var[1]<-'HAA5'
var[2]<-'SWTR'
var[3]<-'TTHM'
data1<-read.csv('AllData.csv')
analys10<-createWorkbook(type="xlsx")
for (i in 1:3) {
IM<-as.character(var[i])
sheet10=createSheet(analys10, IM)
sub<-data1[data1$ANALYTE_NAME==IDs[i],]
subdata<-sub[,1:19]
subdata<-unique(subdata)
subs<-subdata[!is.na(subdata$MCL_VALUE),]
n<-nrow(subs)
for (j in 1:n) {
if (as.character(subs$MCL_UOM[j])==as.character(subs$RESULT_UOM[j]))
subs$mag[j]<-(subs$RESULT[j]-subs$MCL_VALUE[j])/subs$MCL_VALUE[j]
}
subs$analysis10<-ifelse(subs$mag >10, 'Yes','no')
summary10<- ddply(subs, c("WATER_SYSTEM_NAME","analysis10",'year'), summarise, N= length(analysis10))
sum10<-summary10[summary10$analysis10 %in% 'Yes',]
table10<-reshape(sum10, timevar="year", idvar=c("WATER_SYSTEM_NAME","analysis10"), direction="wide" )
table10$analysis10<- NULL
table10<-table10[,order(colnames(table10),decreasing=TRUE)]
addDataFrame(table10, sheet=sheet10, startColumn=1, row.names=FALSE)
}
saveWorkbook(analys10, "analys10.xlsx")
我花了最后一天来弄清问题是什么,但到目前为止我没有机会。有什么建议? 我添加了整个代码和一个示例数据集。希望它会起作用!
PWS_ID REGULATING_AGENCY WATER_SYSTEM_NAME CITY Counties_Served Owner_Type ANALYTE_NAME PWS_Type Primary_Source POPULATION Quarter MCL_VALUE MCL_UOM XMOD RESULT RESULT_UOM VIOL_BEGIN_DATE Size_toggle year VIOL_END_DATE ADDRESS
210001 DIS LAKE ARN AL Pr HAA5 Comm Surface water 2650 2017 Q4 0.06 MG/L NA 0.0758 MG/L 10/1/2017 Smaller 2017 12/31/2017 P.O. Bo
210001 DIS LAKE ARN AL Pr HAA5 Comm Surface water 2650 2016 Q4 0.06 MG/L NA 0.07 MG/L 10/1/2016 Smaller 2016 12/31/2016 P.O. B
210001 DIS ALPINE ARN AL Pr HAA5 Comm Surface water 2650 2017 Q1 0.06 MG/L NA 0.095 MG/L 1/1/2017 Smaller 2017 3/31/2017 P.O. B
210001 DIS ALPINE ARNOLD AL Pr HAA5 Comm Surface water 2650 2017 Q3 0.06 MG/L NA 0.086 MG/L 7/1/2017 Smaller 2017 9/30/2017 P.O. B
210001 DIS ALPINE ARNOLD AL Pr HAA5 Comm Surface water 2650 2012 Q3 0.06 MG/L NA 0.0658 MG/L 7/1/2012 Smaller 2012 9/30/2012 P.O. B
210001 DIS ALPINE ARNOLD AL Pr HAA5 Comm Surface water 2650 2016 Q3 0.06 MG/L NA 0.062 MG/L 7/1/2016 Smaller 2016 9/30/2016 P.O. B
210001 DIS ALPINE ARNOLD AL Pr HAA5 Comm Surface water 2650 2017 Q2 0.06 MG/L NA 0.096 MG/L 4/1/2017 Smaller 2017 6/30/2017 P.O. B
300037 DIS JACKSON IONE AL Pr HAA5 Comm Surface water 2000 2012 Q4 NA NA NA 12/1/2012 Smaller 2012 12/31/2012 P.O. B
300037 DIS JACKSON IONE AL Pr HAA5 Comm Surface water 2000 2012 Q4 NA NA NA 11/1/2012 Smaller 2012 11/30/2012 P.O. B
300037 DIS JACKSON IONE AL Pr HAA5 Comm Surface water 2000 2012 Q4 NA NA NA 12/1/2012 Smaller 2012 12/31/2012 P.O. B
300037 DIS JACKSON IONE AL Pr HAA5 Comm Surface water 2000 2012 Q4 NA NA NA 12/1/2012 Smaller 2012 12/31/2012 P.O. B
300037 DIS JACKSON IONE AM Pr HAA5 Comm Surface water 2000 2012 Q3 NA NA NA 8/1/2012 Smaller 2012 8/31/2012 P.O. B
300037 DIS JACKSON IONE AM Pr HAA5 Comm Surface water 2000 2012 Q3 NA NA NA 8/1/2012 Smaller 2012 8/31/2012 P.O. B
300037 DIS JACKSON IONE AM Pr HAA5 Comm Surface water 2000 2012 Q4 NA NA NA 11/1/2012 Smaller 2012 11/30/2012 P.O. B
310002 DIS JACKSON JACKSON AM Pr TTHM Comm Surface water 6420 2017 Q1 0.08 MG/L NA 0.084 MG/L 1/1/2017 Smaller 2017 3/31/2017 P.O. B
310004 DIS JACKSON JACKSON AM Pr TTHM Comm Surface water 1060 2017 Q2 0.08 MG/L NA 0.081 MG/L 4/1/2017 Smaller 2017 6/30/2017 P.O. B
310005 DIS JACKSON JACKSON AM Pr HAA5 Comm Surface water 846 2014 Q3 0.08 MG/L NA 0.0815 MG/L 7/1/2014 Smaller 2014 9/30/2014 P.O. B
310011 DIS JACKSON JACKSON AM Pr SWTR Comm Surface water 838 2016 Q3 0.06 MG/L NA 0.065 MG/L 7/1/2016 Smaller 2016 9/30/2016 P.O. B
310011 DIS JACKSON JACKSON AM Pr SWTR Comm Surface water 838 2017 Q1 0.06 MG/L NA 0.068 MG/L 1/1/2017 Smaller 2017 3/31/2017 P.O. B
310011 DIS JACKSON JACKSON AM Pr SWTR Comm Surface water 838 2016 Q2 0.06 MG/L NA 0.069 MG/L 4/1/2016 Smaller 2016 6/30/2016 P.O. B
310012 DIS JACKSON JACKSON AM Pr TTHM Comm Surface water 18544 2016 Q4 0.08 MG/L NA 0.094 MG/L 10/1/2016 Larger 2016 12/31/2016 P.O. B
400073 DIS JACKSON JACKSON BUT Pr SWTR Comm Ground water 66 2017 Q3 10 MG/L NA 11 MG/L 7/1/2017 Smaller 2017 7/31/2017 P.O. B
510012 DIS JACKSON JACKSON BUT Pr SWTR Comm Surface water 180 2016 Q2 0.06 MG/L NA 0.067 MG/L 4/1/2016 Smaller 2016 6/30/2016 P.O. B
600008 DIS JACKSON JACKSON BUT Pr SWTR Comm Ground water 381 2012 Q1 10 UG/L NA 26.1 UG/L 1/1/2012 Smaller 2012 3/31/2012 P.O. B
600008 DIS JACKSON JACKSON BUT Pr SWTR Comm Ground water 381 2014 Q4 10 UG/L NA 23.6 UG/L 10/1/2014 Smaller 2014 12/31/2014 P.O. B
600008 DIS JACKSON JACKSON BUT Pr SWTR Comm Ground water 381 2015 Q4 NA NA 24 UG/L 10/1/2015 Smaller 2015 12/31/2015 P.O. B
600008 DIS JACKSON JACKSON BUT Pr SWTR Comm Ground water 381 2013 Q2 10 UG/L NA 24.6 UG/L 4/1/2013 Smaller 2013 6/30/2013 P.O. B
答案 0 :(得分:0)
我无法告诉你的代码在哪里破解,但这是一个有效的例子。我已使用内置的mtcars
数据框进行说明:
library(xlsx)
var = c('HAAS','SWTR','TTHM')
analys10 <- createWorkbook(type="xlsx")
for (i in var) {
sheet = createSheet(analys10, i)
addDataFrame(mtcars, sheet=sheet, startColumn=1, row.names=FALSE)
}
saveWorkbook(analys10, "analys10.xlsx")
在您的实际代码中,我假设您每次循环都会添加不同的数据框,因此您可能会因为设置或选择每个数据框而出现问题。数据框,但没有其他信息很难分辨。