Use reactive object to set default path of shinyFiles::shinyDirChoose

Hi,

I would like to set the default path of a shinyFiles::shinyDirButton / shinyFiles::shinyDirChoose based upon the folder selected in another shinyFiles::shinyDirButton.

Building upon the example provided in https://forum.posit.co/t/shiny-directory-input/29160, I would like to set the default folder for my Upload button based upon the selection made in the Download button. Using the dirDown() reactive leads to error message.

Any suggestion would be greatly appreciated.

library(shiny)
library(shinyFiles)

ui <- fluidPage( # Application title
  mainPanel(
    shinyDirButton("dirDown", "Download directory", "Download"),
    verbatimTextOutput("dirDown", placeholder = TRUE) ,
    shinyDirButton("dirUp", "Upload directory", "Upload"),
  )
)

server <- function(input, output) {
  shinyDirChoose(
    input,
    'dirDown',
    roots = c(home = '~')
  )
  
  global <- reactiveValues(datapath = getwd())
  
  dirDown <- reactive(input$dirDown)
  
  output$dirDown <- renderText({
    global$datapath
  })
  
  observeEvent(
    ignoreNULL = TRUE,
    eventExpr = {
      input$dirDown
    },
    handlerExpr = {
      if (!"path" %in% names(dirDown())) return()
      home <- normalizePath("~")
      global$datapath <-
        file.path(home, paste(unlist(dirDown()$path[-1]), collapse = .Platform$file.sep))
    }
  )
  
  shinyDirChoose(
    input,
    'dirUp',
     #defaultPath = dirDown(),
    roots = c(home = '~')
  )
  
}

# Run the application
shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.