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)
keithnewman commented 1 week ago

I think your situation could work with two changes:

Step 1: Install the development version of the package. Using remotes:

remotes::install_github("thomasp85/shinyFiles")

If you want to install the specific commit at the time of writing, it's

remotes::install_github("thomasp85/shinyFiles@da361359712bbcd74dd6a9505a88ec87aead13bc")

The patch in the development version of the branch was actually written on 5th July 2023 but hasn't been released yet. It's frustrating, because I also need the same patch for my work.

Step 2: Modify a line of your app code (removing brackets after roots):

observe({ shinyDirChoose(input, "dir1", roots = roots) })

This leaves you with the following app code:

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)