-->

Getting file path from Shiny UI (Not just director

2019-02-17 04:13发布

问题:

I need to deal with a huge file (>500mb) in R. So instead of loading such heavy file in R environment, I process the file in chunks of specific number of rows and finally get the aggregate values.

I need user to specify the file (using some kind of browse functionality) so that I can feed the file path to my algorithm

fileConnection <-file( "../output/name.txt", open="w")

Is there any way to get only file path from Shiny UI based on the address specified by user? I tried ShinyFiles package, but it gives only directory to choose, not file.

Thank you guys!

回答1:

This functionality is available in the shinyFiles package. Have a look at this minimal example:

library(shiny)
library(shinyFiles)


  ui <- fluidPage(
    shinyFilesButton("Btn_GetFile", "Choose a file" ,
                                    title = "Please select a file:", multiple = FALSE,
                          buttonType = "default", class = NULL),

              textOutput("txt_file")     
                   )


  server <- function(input,output,session){

    volumes = getVolumes()
    observe({  
    shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)

    if(!is.null(input$Btn_GetFile)){
      # browser()
      file_selected<-parseFilePaths(volumes, input$Btn_GetFile)
      output$txt_file <- renderText(as.character(file_selected$datapath))
    }
  })
  }
  shinyApp(ui = ui, server = server)

Hope this helps!