Unable to save user's file selection

The bottom code is supposed to allow the user to select a file from their directory and save their selection in directory_path but for some reason, it doesn't work.

Thanks in advance for the help!

library(shiny)
library(shinyFiles)

ui <- fluidPage(
  shinyDirButton("directory", "Select Directory", "Please select a directory"),
  verbatimTextOutput("selected_directory")
)

server <- function(input, output, session) {
  # Initialize reactive values object to satore selected directory
  rv <- reactiveValues(directory_path = NULL)
  
  # Create reactive value for selected directory
  selected_directory <- eventReactive(input$directory, {
    chosen_dir <- shinyDirChoose(input, "directory", roots = c(home = '~'), filetypes = NULL)
    if (!is.null(chosen_dir)) {
      return(as.character(chosen_dir$value))
    }
  })
  
  # Update selected directory in reactive values object
  observe({
    rv$directory_path <- selected_directory()
  })
  
  # Display selected directory in output
  output$selected_directory <- renderText({
    paste0("You selected the following path: ", rv$directory_path)
  })


}

shinyApp(ui = ui, server = server)

There is no need for reactiveValues - shinyDirButton already provides you with according inputs (see input$directory$root and input$directory$path).

I just posted a related answer here.

Please check the following:

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

ui <- fluidPage(
splitLayout(
         shinyDirButton("directory", label = NULL, title = "Select directory", multiple = FALSE, icon = icon("folder"), viewtype = "detail", style = "height:40px;"),
         verbatimTextOutput("dir_out_output"),
         cellWidths = c("50px", "500px")
  )
)

server <- function(input, output, session) {
  roots <- c(wd = '.')
  
  shinyDirChoose(
    input,
    id = "directory",
    roots = roots,
    updateFreq = 0,
    session,
    defaultPath = "",
    defaultRoot = NULL,
    allowDirCreate = TRUE
  )

  output$dir_out_output <- renderPrint({
    if(all(c("root", "path") %in% names(input$directory))){
      selected_path <- do.call(path, c(roots[input$directory$root], input$directory$path))
      # selected_path <- path_abs(selected_path) # get the absolute path
      selected_path
    } else {
      "No directory selected"
    }
  })
}

shinyApp(ui, server)

This topic was automatically closed 21 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.