使用浏览按钮从Shiny UI(不仅仅是目录)获取文件路径而不上传文件

Nar*_*ati 9 r shiny

我需要在R中处理一个巨大的文件(> 500mb).因此,我不是在R环境中加载这么重的文件,而是以特定行数的块处理文件,最后得到聚合值.

我需要用户指定文件(使用某种浏览功能),以便我可以将文件路径提供给我的算法

fileConnection <-file( "../output/name.txt", open="w")
Run Code Online (Sandbox Code Playgroud)

有没有办法根据用户指定的地址从Shiny UI获取文件路径?我试过ShinyFiles包,但它只提供目录选择,而不是文件.

感谢你们!

SBi*_*sta 13

shinyFiles包中提供了此功能.看看这个最小的例子:

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)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!