将文件路径从 shinyFiles::shinyFileChoose 传递到另一个函数

Pass a filepath from shinyFiles::shinyFileChoose to another function

相关

我无法从 shinyFileChoose 接收文件路径以在其他函数中使用它。我已经根据手册和上面提到的相关线程尝试了以下方法,但我仍然一无所获...

我只是想要用户选择的文件的绝对文件路径,以便我以后可以在我的程序中使用它(在几个不同的函数中)。

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'

因为 parseFilePaths 输出是第 1 行 dataframe,您应该指定列并将其更改为 character,这样它将能够显示在 renderText

尝试:

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)