thomasp85 / shinyFiles

A shiny extension for server side file access
196 stars 47 forks source link

Adding a user selected folder directly accessible in shinyDirChoose #194

Open gver84 opened 4 months ago

gver84 commented 4 months ago

Dear, I want to add an accessible folder to a user-selected directory in shinyDirChoose by modifying roots. It seems it works to modify roots, but next if I want to select a new directory via the 'custom' volume selection and click on sub-directories, for example, it crashes with the error message "Error in: path must not have missing values". Thank you for your help !

library(shiny)
library(shinyFiles)
library(fs)

ui <- fluidPage(
  titlePanel("Add Volume to Roots in Shiny"),
  shinyDirButton("dir1", "Select Directory 1", "Please select a directory"),
  verbatimTextOutput("roots_info")
)

server <- function(input, output, session) {

  # Initial roots definition
  roots <- reactiveVal(c(
    home = fs::path_home(),
    output = paste0(fs::path_home(), "/Eclipse")
  ))

  # Show the current roots information
  output$roots_info <- renderPrint({ roots() })

  # Initialize shinyDirChoose for the directories
  observe({ shinyDirChoose(input, "dir1", roots = roots()) })

  # # Handle the directory selection
   observeEvent(input$dir1, {
     dir1 <- parseDirPath(roots(), input$dir1)
     if (!is.null(dir1) && !anyNA(dir1) && length(dir1) > 0) {
       new_roots <- roots()
       new_roots["custom"] <- dir1
       roots(new_roots)    
     }
   })

}

shinyApp(ui, server)