首页 > 解决方案 > 将来自 shinyFiles::shinyFileChoose 的文件路径传递给另一个函数

问题描述

相关在 Shiny 中显示选定的文件夹路径

无论如何,我无法接收文件路径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)

错误:参数 1(类型“列表”)不能由“猫”处理

标签: rshinyfile-browser

解决方案


因为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)

推荐阅读