Getting file path from Shiny UI (Not just director

2019-02-17 04:02发布

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条回答
再贱就再见
2楼-- · 2019-02-17 04:53

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!

查看更多
登录 后发表回答