Search code examples
rshinyfile-browser

Pass a filepath from shinyFiles::shinyFileChoose to another function


RELATED: Display selected folder path in Shiny

I cannot receive the file path from shinyFileChoose to use it in another function in anyway. I have tried the following approach according to the manual and the related thread mentioned above, but I still get nothing...

I simply want the absolute file path to the file user chooses so that I can use it in my program later on (in several different functions).

ui <- fluidPage(

   titlePanel("File Browser"),

   sidebarLayout(
      sidebarPanel(

        shinyFilesButton('files', label = 'Select', title = 'Please select a 
                          file', multiple = FALSE),
        verbatimTextOutput("filechosen")
      ),

      mainPanel(
      )
   )
)


server <- function(input, output) {

   shinyFileChoose(input, 'files', root = c(root = '/home/guest/test_data'),          
   filetypes = c('', "xml", "txt"))

   file <- reactive(input$files)
   output$filechosen <- renderText({
     parseFilePaths(c(home = "/home/guest/test_data"), file())
   })

}   
shinyApp(ui = ui, server = server)

Error: argument 1 (type 'list') cannot be handled by 'cat'


Solution

  • because the parseFilePaths output is a 1 row dataframe, you should specific the column and change it to character, so it will be able to show in renderText

    Try :

    library(shinyFiles)
    ui <- fluidPage(
    
      titlePanel("File Browser"),
    
      sidebarLayout(
        sidebarPanel(
    
          shinyFilesButton('files', label = 'Select', title = 'Please select a 
                           file', multiple = FALSE),
          verbatimTextOutput("filechosen")
          ),
    
        mainPanel(
        )
      )
      )
    
    
    server <- function(input, output) {
    
      shinyFileChoose(input, 'files', root = c(root = '/home/guest/test_data'),
                      filetypes = c('', "xml", "txt"))
    
      file <- reactive(input$files)
      output$filechosen <- renderText({
    
        as.character(parseFilePaths(c(home = "/home/guest/test_data"),file())$datapath)
        # Either is fine
        # parseFilePaths(c(home = "/home/guest/test_data"),file())$datapath,stringAsFactors=F)
      })
    
    }   
    shinyApp(ui = ui, server = server)