【问题标题】:Get the path for a folder chosen by user in shiny获取用户在闪亮中选择的文件夹的路径
【发布时间】:2019-06-07 11:59:19
【问题描述】:

我希望用户能够选择一个文件夹(稍后我将检索此路径)。

目前这适用于选择文件而不是文件夹。我无法解释为什么。并且该目录中确实有文件(在 windows 和 mac 上测试过)。 有什么想法吗?

library(shiny)
library(shinyFiles)


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

  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))
    }
  })

  observe({  
    if(!is.null(input$Btn_Folder)){
      # browser()
      shinyDirChoose(input, 'folder', roots=volumes)
      dir <- reactive(input$folder)
      output$dir <- renderText(as.character(dir()))
    }
  })


}
shinyApp(ui = ui, server = server)

【问题讨论】:

标签: r shiny


【解决方案1】:

那是因为你在这里写了Btn_Folder 而不是folder

  observe({  
    if(!is.null(input$Btn_Folder)){
      shinyDirChoose(input, 'folder', roots=volumes)
      dir <- reactive(input$folder)
      output$dir <- renderText(as.character(dir()))
    }
  })

替换为:

  observe({  
    if(!is.null(input$folder)){
      shinyDirChoose(input, 'folder', roots=volumes)
      dir <- reactive(input$folder)
      output$dir <- renderText(as.character(dir()))
    }
  })

附带说明,您不需要在观察者内部定义此反应导体,只需执行以下操作:

  observe({  
    if(!is.null(input$folder)){
      shinyDirChoose(input, 'folder', roots=volumes)
      output$dir <- renderText(as.character(input$folder))
    }
  })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多