如何保存 ShinyFiles 目录输入的基本名称以传递给函数

how to save basename of ShinyFiles directory input to pass into a function

如何从 ShinyFiles 目录按钮中获取选定的目录名称?我查看了这个 post、,并让它在 verbatimTextOutput 中显示完整路径,但我只想要目录名称。我尝试打印到 message()print() 之类的控制台。它同时打印了一些奇怪的整数。我尝试了 isolate(reactiveValuesToList(base_name))dir()$path[[2]] 但我没有成功。

   1                                                            
   [1] 1                                                        
   attr(,"class")                                               
   [1] "integer"                "shinyActionButtonValue"         
   list("", "flowCyt_pdfs")home                                  
   $path                                                        
   $path[[1]]                                                    
   [1] ""                                                        

   $path[[2]]                                                    
   [1] "flowCyt_pdfs"                                            

   $root                                                         
   [1] "home"                                                   
   Warning: Error in $: $ operator is invalid for atomic vectors
library(shiny)
library(shinyFiles)


ui <- fluidPage(
  navbarPage(
    tabPanel("test",
    sidebarPanel(
      tags$h2("HEADER"),
      shinyDirButton("dir", "Input Directory", "")
    ),
    mainPanel(
      h4("DIRPATH OUTPUT"),
      verbatimTextOutput("dirpath_dply")
    )
    )
  )
)

server <- function(input, output) {
  home_dir <- "/home/dir/path"
  shinyDirChoose(
                 input,
                 "dir",
                 roots = c(home = home_dir),
                 filetypes = c('', "txt", "png", "pdf")
                 )
  dir <- reactive(input$dir)
  output$dirpath_dply <- renderText({
      parseDirPath(c(home=home_dir), dir())
  })

  observeEvent(ignoreNULL = TRUE,
               eventExpr = {
                   input$dir
              },
              handlerExpr = {
                  base_name <- unlist(dir())
                  message(base_name)
                  datapath <- file.path(home_dir,paste(unlist(dir()) ))

  })
}

shinyApp(ui, server)

我不确定您真正希望得到什么,但这是我的解释(我将 home_dir 更改为 ~ 以匹配任何 user/system)。

注意:您不需要提供 filetypes,因为 shinyDirChoose 用于选择目录,而不是文件。你的 UI 也有点奇怪。不建议在另一个 ...Page 中使用 ...Page,因为 ...Page 通常是最顶层的 UI 元素。

dir() 将提供选定的目录基本名称。我在使用值之前检查真实性。

server <- function(input, output) {
  home_dir <- "~"
  shinyDirChoose(
    input,
    "dir",
    roots = c(home = home_dir)
  )
  dir <- reactive(basename(parseDirPath(c(home=home_dir), input$dir)))
  
  output$dirpath_dply <- renderText({
    dir()
  })
  observe({
    if(isTruthy(dir()))
      message(dir())
  })
}