0

How can I obtain just the selected directory name from a ShinyFiles directory button? I looked at this post, another SO question, and got it to show the full path in a verbatimTextOutput, but I just want the directory name. I tried printing out to console like message() and print(). It has some weird integers getting printed at the same time. I tried isolate(reactiveValuesToList(base_name)), dir()$path[[2]] but I"m not successful.

   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)
Spencer Trinh
  • 743
  • 12
  • 31

1 Answers1

0

I'm unsure of what you really expect to get but here is my interpretation (I changed home_dir to ~ in order to match any user/system).

NB: You don't need to provide filetypes as shinyDirChoose is for selecting directories, not files. Your UI is also a bit weird. Using a ...Page in another ...Page is not recommended as ...Page is usually the topmost UI element.

dir() will provide selected directory basename. I check for truthiness before using value.

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())
  })
}
Billy34
  • 1,777
  • 11
  • 11
  • cool, thanks @Billy34. It seems to work. I am new to R Shiny so not sure what I'm doing. Why did the other SO solution have so much more code that you gutted out? What was the use of: `eventExpr`, `handleExpr`, `unlist()`, `observeEvent`.? Also, now that I have the basename, do I just put my function within `observe({})`? after the `message(dir())`? – Spencer Trinh Jan 16 '21 at 05:24
  • `eventExpr` and `handleExpr` are just the names of the parameters of `observeEvent`. `observeEvent` is basically an `observe` where you define the reactive values you are listening to (`eventExpr`). observe has only a `handleExpr`. For where to put your function it depends of what you want to do with the result. – Billy34 Jan 17 '21 at 11:10
  • i simply want to use the `dir()` value to pass into a http post request after clicking the `shinyDirChoose` button adn selecting the directory – Spencer Trinh Jan 18 '21 at 17:07
  • Yes and what you want to do with the result of the POST request :-) Maybe move to a private chat or ask a new question. – Billy34 Jan 18 '21 at 18:39
  • how can i direct message you? – Spencer Trinh Jan 19 '21 at 21:58