【问题标题】:Download excel file already existing in Shiny下载 Shiny 中已经存在的 excel 文件
【发布时间】:2020-02-16 22:09:13
【问题描述】:

我正在尝试下载我闪亮的应用程序中已经存在的 excel 文件。

我在server.R中尝试了以下代码

   output$downloadbutton <- downloadHandler(

     filename <- function() {
       paste("result_balance", "xlsx", sep=".")
     },
     content <- function(file) {
       #temp <- file.path(tempdir(), "report.Rmd")
       file.copy(file.path(getwd(),'www','result.xlsx'), file,overwrite = TRUE)
     },
     contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
   )

但是当用户点击下载按钮时,它会弹出一个下载,如下所示:

它会下载一个包含 xml/html 文件的文件夹。 奇怪的是,提议的名称是 downloadbutton,就像我的 id shiny 元素一样。

我已经知道这个R Shiny: Download existing file

Ubuntu 18.04 上的 R 版本 3.6.2

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    filenamecontent 是函数downloadHandler 的参数,因此您必须使用= 而不是&lt;-

     output$downloadbutton <- downloadHandler(
    
         filename = function() {
           paste("result_balance", "xlsx", sep=".")
         },
         content = function(file) {
           #temp <- file.path(tempdir(), "report.Rmd")
           file.copy(file.path(getwd(),'www','result.xlsx'), file,overwrite = TRUE)
         },
         contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
       )
    

    【讨论】:

    • 谢谢,我没注意到。但奇怪的是,在 windows 中下载我发布的代码是可以的,在 Linux 中我遇到了上述问题。
    【解决方案2】:

    在 windows 和 linux ( Ubuntu ) 上测试此功能有效:

     output$downloadbutton <- downloadHandler(
    
         filename = function() {
           paste("result_balance", "xlsx", sep=".")
         },
         content = function(file) {
    
           file.copy(file.path(getwd(),'www','result.xlsx'), file,overwrite = TRUE)
         },
         contentType = "text/xlsx"
       )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 2014-02-18
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      相关资源
      最近更新 更多