Has anyone tried using the download handler in R Shiny to download a freshly created Excel file with XLConnect?
有没有人尝试过使用R bright中的下载处理程序来下载使用XLConnect新创建的Excel文件?
In the ui.R there is the unremarkable line:
在ui中。这里有一句平淡无奇的话:
downloadButton('downloadData', 'Download')
In the server.R there is the handler:
在服务器。有一个处理器:
output$downloadData <- downloadHandler(
filename = function() { "output.xlsx" },
content = function(file){
wb <- loadWorkbook(file, create = TRUE)
createSheet(wb, name = "Sheet1")
writeWorksheet(wb, c(1:3), sheet = "Sheet1") # writes numbers 1:3 in file
saveWorkbook(wb)
}
)
I have no problem downloading a .csv and no problem creating the excel file with XLConnect. But when I run the code as above I get the following error in my Chrome browser:
下载.csv并使用XLConnect创建excel文件没有问题。但是当我运行代码时,我在我的Chrome浏览器中有以下错误:
IllegalArgumentException (Java): File extension "file1b683b9323bc" not supported! Only *.xls and *.xlsx are allowed!
不支持文件扩展名“file1b683b9323bc”!只*。xls和*。xlsx允许!
As far as I can see, XLConnect cannot write to a temporary file.
在我看来,XLConnect不能写入临时文件。
Has anyone got a solution or workaround?
有人找到解决办法了吗?
One option would be to save the file in a specific location and then creating a download link pointing to it. However, this is not very Shiny-esque as multiple users would cause havok.
一种选择是将文件保存在特定的位置,然后创建指向该文件的下载链接。然而,这并不是很光鲜,因为多个用户会导致havok。
Many Thanks
非常感谢
Marcus
马库斯
1 个解决方案
#1
12
Try using this for the content(...)
function; it works for me...
试着用它来做内容(…)函数;它适合我…
content = function(file){
fname <- paste(file,"xlsx",sep=".")
wb <- loadWorkbook(fname, create = TRUE)
createSheet(wb, name = "Sheet1")
writeWorksheet(wb, c(1:3), sheet = "Sheet1") # writes numbers 1:3 in file
saveWorkbook(wb)
file.rename(fname,file)
}
The problem is that file
is a randomly generated temp file, without an extension, whereas saveWorkbook(...)
requires the .xlsx
extension. So this just appends .xlsx
to file
and uses that for all the XLConnect manipulations, then renames the final file to the original name (e.g., strips off the extension).
问题是,文件是一个随机生成的临时文件,没有扩展,而saveWorkbook(…)需要.xlsx扩展。因此,它只是将.xlsx附加到file中,并将其用于所有的XLConnect操作,然后将最终文件重命名为原始名称(例如,删除扩展名)。
#1
12
Try using this for the content(...)
function; it works for me...
试着用它来做内容(…)函数;它适合我…
content = function(file){
fname <- paste(file,"xlsx",sep=".")
wb <- loadWorkbook(fname, create = TRUE)
createSheet(wb, name = "Sheet1")
writeWorksheet(wb, c(1:3), sheet = "Sheet1") # writes numbers 1:3 in file
saveWorkbook(wb)
file.rename(fname,file)
}
The problem is that file
is a randomly generated temp file, without an extension, whereas saveWorkbook(...)
requires the .xlsx
extension. So this just appends .xlsx
to file
and uses that for all the XLConnect manipulations, then renames the final file to the original name (e.g., strips off the extension).
问题是,文件是一个随机生成的临时文件,没有扩展,而saveWorkbook(…)需要.xlsx扩展。因此,它只是将.xlsx附加到file中,并将其用于所有的XLConnect操作,然后将最终文件重命名为原始名称(例如,删除扩展名)。