在R中使用shinyDirButton
选择多个文件夹并不是该函数的默认功能,因为shinyDirButton
主要是为了选择一个单一的文件夹路径设计的。然而,你可以通过一些额外的步骤来实现选择多个文件夹的功能。
shinyDirButton
是Shiny框架中的一个UI组件,用于让用户选择一个文件夹路径。Shiny是R的一个包,用于构建交互式的Web应用程序。
由于shinyDirButton
本身不支持选择多个文件夹,我们可以使用shinyFiles
包中的shinyFilesButton
来代替,因为它提供了更多的灵活性,包括选择多个文件或文件夹的能力。
library(shiny)
library(shinyFiles)
ui <- fluidPage(
shinyFilesButton("btn", "选择文件夹", "Please select a folder", multiple = TRUE),
verbatimTextOutput("selected_folders")
)
server <- function(input, output, session) {
volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
output$selected_folders <- renderPrint({
req(input$btn)
shinyFiles::parseFilePaths(volumes, input$btn)$datapath
})
}
shinyApp(ui, server)
shinyFilesButton
代替shinyDirButton
,并设置multiple = TRUE
以允许选择多个文件夹。volumes
变量来指定可用的文件系统卷。然后在renderPrint
中解析用户选择的文件夹路径并显示它们。shinyFiles
包:install.packages("shinyFiles")
。通过这种方式,你可以在R的Shiny应用程序中实现选择多个文件夹的功能。
领取专属 10元无门槛券
手把手带您无忧上云