shinyFiles icon indicating copy to clipboard operation
shinyFiles copied to clipboard

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

Open sbihorel opened this issue 3 years ago • 0 comments

Hi,

Thank you for creating and sharing shinyFiles to the community. Really helpful! I recently posted a question to RStudio Community but got no takers... I'm hoping that you'll be able to provide some insights to the following problem.

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://community.rstudio.com/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)

sbihorel avatar Sep 13 '22 10:09 sbihorel