使用R Shiny从XLConnect下载Excel文件 [英] Downloading Excel File from XLConnect with R Shiny
问题描述
在ui.R中有一个不起眼的行:
downloadButton('downloadData','下载')
在server.R中有一个处理程序:
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)#在文件$中写入数字1:3 b $ b saveWorkbook(wb)
}
)
我没有问题下载.csv,并且使用XLConnect创建excel文件没有问题。但是当我运行上面的代码时,我的Chrome浏览器中出现以下错误:
IllegalArgumentException(Java):文件扩展名file1b683b9323bc不支持
!只允许* .xls和* .xlsx!
据我看到,XLConnect无法写入临时文件。 / p>
有没有人有解决方案或解决方法?
一个选项是将文件保存在特定位置,然后创建一个指向它的下载链接。但是,这不是很闪亮,因为多个用户会造成havok。
许多谢谢
Marcus
尝试使用内容(...)
它适用于我...
content = function(file){
fname& xlsx,sep =。)
wb< - loadWorkbook(fname,create = TRUE)
createSheet(wb,name =Sheet1)
writeWorksheet(wb,c 1:3),sheet =Sheet1)#在文件$ b中写入数字1:3 $ b saveWorkbook(wb)
file.rename(fname,file)
}
问题是文件
是一个随机生成的临时文件,没有扩展名,而 saveWorkbook(...)
需要 .xlsx
扩展名。所以这只是将 .xlsx
追加到文件
,并将其用于所有XLConnect操作,然后将最终文件重命名为原始名称(例如,剥离扩展名)。
Has anyone tried using the download handler in R Shiny to download a freshly created Excel file with XLConnect?
In the ui.R there is the unremarkable line:
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:
IllegalArgumentException (Java): File extension "file1b683b9323bc" not supported! Only *.xls and *.xlsx are allowed!
As far as I can see, XLConnect cannot write to a temporary file.
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.
Many Thanks
Marcus
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).
这篇关于使用R Shiny从XLConnect下载Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!