Illya
Illya

Reputation: 263

Using shinyFiles: a shiny app where the user selects a file for download, then selects where to copy

The below code succesfully allows the user to browse and select a file. However I am unable to write the syntax for the download of this file.

library(shiny)
library(shinyFiles)


ui <- fluidPage( 

  shinyFilesButton('files', label='File select', title='Please select a file', multiple=T) ,

  verbatimTextOutput('rawInputValue'),

  verbatimTextOutput('filepaths') ,

  downloadButton("downloadFiles", "Download Files")

)

server <- function(input, output) {

  roots =  c(wd = 'H:/')

  shinyFileChoose(input, 'files', 
                  roots =  roots, 
                  filetypes=c('', 'txt' , 'gz' , 'md5' , 'pdf' , 'fasta' , 'fastq' , 'aln'))

  output$rawInputValue <- renderPrint({str(input$files)})

  filepathsObject <- renderPrint({parseFilePaths(roots, input$files)})

  output$filepaths <- filepathsObject

  output$downloadFiles <- downloadHandler(

    filename = 'filepathsObject' ,

    content = function(file) {
      file.copy(filepathsObject, file)
    }
  )

}

shinyApp(ui = ui , server = server)

Upvotes: 1

Views: 1461

Answers (1)

Geovany
Geovany

Reputation: 5687

To download the file with downloadHandler you need to provide the full name including the path as a string, but your variable filepathsObject is an object of class "shiny.render.function.

The parseFilePaths function will return a list with all the information needed, but you need convert it to character. Below is you code modified to download the previously uploaded file.

Please note that the download button doesn't works well on the RStudio viewer, so launch the app in a browser if you want to have the original file name as default.

library(shiny)
library(shinyFiles)


ui <- fluidPage( 
  shinyFilesButton('files', label='File select', title='Please select a file', multiple=T) ,
  verbatimTextOutput('rawInputValue'),
  verbatimTextOutput('filepaths') ,
  downloadButton("downloadFiles", "Download Files")
)

server <- function(input, output) {

  roots =  c(wd = 'H:/')

  shinyFileChoose(input, 'files', 
                  roots =  roots, 
                  filetypes=c('', 'txt' , 'gz' , 'md5' , 'pdf' , 'fasta' , 'fastq' , 'aln'))

  output$rawInputValue <- renderPrint({str(input$files)})

  output$filepaths <- renderPrint({parseFilePaths(roots, input$files)})

  output$downloadFiles <- downloadHandler(
    filename = function() {
      as.character(parseFilePaths(roots, input$files)$name)
    },
    content = function(file) {
      fullName <- as.character(parseFilePaths(roots, input$files)$datapath)
      file.copy(fullName, file)
    }
  )
}

shinyApp(ui = ui , server = server)

Upvotes: 1

Related Questions