【问题标题】:download binary file from shiny server从闪亮的服务器下载二进制文件
【发布时间】:2020-11-20 17:18:51
【问题描述】:

我的闪亮应用程序的用户刚刚创建了一个存储在闪亮服务器上的二进制文件。 它不是文本文件也不是 zip,而是生物信息学数据文件 (bam)。 该文件位于闪亮的应用树内的已知路径 => Uploads/data_filtered.bam

我想让用户使用下载按钮下载它。

如何修改 downloadHandler 块以将文件复制到本地客户端?

目前我还没有找到任何解决方案,也不想将 bam 打包成 zip 以节省用户下载后解压的时间。

感谢任何可以完成这项工作的代码

【问题讨论】:

    标签: r shiny download


    【解决方案1】:

    您可以使用addResourcePath 为您的文件提供闪亮的服务

    library(shiny)
    
    ui <- fluidPage(htmlOutput("link"))
    
    server <- function(input, output, session) {
      addResourcePath("res", "Uploads")
      output$link = renderUI(HTML('<A HREF="res/data_filtered.bam">Download</A>'))
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 非常感谢,这不是我的意思(我的 bam 数据是在会话期间由闪亮创建的),但对于共享例如演示二进制文件也非常有用
    【解决方案2】:

    我找到了正确的函数(file.copy),经过反复试验,我想出了如何使用它

      # get info from input file uploaded by the user
      BamFile <- reactive({
        file <- input$BAM
        req(file)
        fpath <- file$datapath
        fext <- tools::file_ext(fpath)
        validate(need(fext %in% c("bam", "BAM"), "Upload must be a .bam or .BAM file"))
        res <-
          list(
            upath = file$datapath,
            fname = basename(file$name),
            fext = fext
          )
        return(res)
      })
      
    ....
    
      # user wants to download the filtered bam file created by the shiny app
      # the file was created in uploads='Uploads/' under the name 'data_filtered.bam'
      output$downloadBam <- downloadHandler(
        filename = function() {
          gsub(".bam", "_filtered.bam", BamFile()$fname)
        },
        content = function(file) {
          serverfile <- paste0(uploads,"/data_filtered.bam")
          file.copy(serverfile, file)
        }
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 2012-07-02
      • 2020-01-26
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多