我正在使用R包Shiny来开发我自己的Web应用程序。
我有一个下载按钮,允许我将数据导出到Excel文件中。在excel文件中,有4张纸,每张纸都有一个数据框。
例如,在sheet1中有dataTab1,在sheet2中有dataTab2,在表3中有dataTab3,在sheet4中有dataTab4。
为此,我在闪亮的server.R。
中使用函数downloadHeader()我使用了两种方法来做到这一点。
output$downloadTab <- downloadHandler(
filename = "dataxls.xlsx",
content = function(file) {
#creation of the workbook
dataxls=createWorkbook(file)
#creation of the sheets
dataTabs1=createSheet(wb=dataxls,sheetName="Compartiments-simulation_sans_changement")
dataTabs2=createSheet(wb=dataxls,sheetName="Esperance-simulation_sans_changement")
dataTabs3=createSheet(wb=dataxls,sheetName="Compartiments-simulation_avec_changement")
dataTabs4=createSheet(wb=dataxls,sheetName="Esperance-simulation_avec_changement")
#add the dataframes to the sheets
writeWorksheet(dataxls, dataTab1, sheet = "Compartiments-simulation_sans_changement")
writeWorksheet(dataxls, dataTab2, sheet = "Esperance-simulation_sans_changement")
writeWorksheet(dataxls, dataTab3, sheet = "Compartiments-simulation_avec_changement")
writeWorksheet(dataxls, dataTab4, sheet = "Esperance-simulation_avec_changement")
saveWorkbook(dataxls,file)
}
)
问题是我有以下错误: createWorkbook(文件)出错: 未知格式C:\ Users \ Baramova \ AppData \ Local \ Temp \ Rtmpmyqyeh \ fileafc6d2b5998.xlsx
我尝试使用以下示例修复它:
content = function(file) {
fname = paste(file,"xlsx",sep=".")
wb = loadWorkbook(fname, create = TRUE)
createSheet(wb, name = "Sheet1")
writeWorksheet(wb, c(1:3), sheet = "Sheet1")
saveWorkbook(wb)
file.rename(fname,file)
}
但它对我不起作用。也许我省略了什么。 你有什么主意吗?
输出$ downloadTab&lt; - downloadHandler(
filename ="Tab.xls",
content = function(fname) {
tmpdir <- tempdir()
db <- paste(tmpdir,fname,sep="/")
channel <- odbcConnectExcel(xls.file = db,readOnly=FALSE)
sqlSave(channel, Compartiments_simulation_sans_changement, tablename = "Compartiments_simulation_sans_changement")
sqlSave(channel, Esperance_simulation_sans_changement, tablename = "Esperance_simulation_sans_changement")
sqlSave(channel, Compartiments_simulation_avec_changement, tablename = "Compartiments_simulation_avec_changement")
sqlSave(channel, Esperance_simulation_avec_changement, tablename = "Esperance_simulation_avec_changement")
odbcClose(channel)
},
contentType="application/csv"
)
问题是即使我通过输入以下命令调用RODBC包,也无法识别odbcConnectExcel()函数: 库(RODBC)
你知道为什么会这样吗?
答案 0 :(得分:2)
我找到了解决问题的方法。
首先,您必须调用XLConnect和RODBC软件包。 之后,您可以使用以下示例:
output$downloadTab <- downloadHandler(
filename ="Tab.xls",
content = function(file) {
write.csv(Compartiments_simulation_sans_changement, file="Compartiments-simulation_sans_changement.csv")
write.csv(Esperance_simulation_sans_changement, file="Esperance-simulation_sans_changement.csv")
write.csv(Compartiments_simulation_avec_changement, file="Compartiments-simulation_avec_changement.csv")
write.csv(Esperance_simulation_avec_changement, file="Esperance-simulation_avec_changement.csv")
channel <- odbcConnectExcel(xls.file = file,readOnly=FALSE)
sqlSave(channel, Compartiments_simulation_sans_changement, tablename = "Compartiments_simulation_sans_changement")
sqlSave(channel, Esperance_simulation_sans_changement, tablename = "Esperance_simulation_sans_changement")
sqlSave(channel, Compartiments_simulation_avec_changement, tablename = "Compartiments_simulation_avec_changement")
sqlSave(channel, Esperance_simulation_avec_changement, tablename = "Esperance_simulation_avec_changement")
odbcClose(channel)
},
contentType="application/xls"
)