Shiny directory input?

Hi,

Update: This was posted in the RStudio IDE category, but maybe i'll find more info if it is posted in the Shiny category?.

tldr;
I am looking for a way to input a directory path in a shiny app. I have recently realized how I can use choose.dir in Windows, but is there a platform independent solution?

I have written an addin that makes it possible to add or remove projects to the Rstudio Most Recently Used Project list as desired (you can see it here).
All of my own projects are held in sub directories of the ~/R directory, and so are easy easy to find.
Since I wrote the functions for only my own use, I had no need to search any other locations, but it seems not everyone sets up their projects the same way I do :grin:

As far as I can tell there is no shiny::dirInput (or equivalent), so what might be the best, platform independent, way to allow people to specify the directory in which to look for projects?

I think you could use shinyFiles to achieve this, similar to this SO post https://stackoverflow.com/questions/48034976/shinyfiles-folder-selection-display-the-default-folder

I had to modify the example slightly to get it working:

library(shiny)
library(shinyFiles)

ui <- fluidPage( # Application title
  mainPanel(
    shinyDirButton("dir", "Input directory", "Upload"),
    verbatimTextOutput("dir", placeholder = TRUE)  
  ))

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

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

Thanks so much for this, it's great. I have just added your code and with a few adjustment I think it will work a treat.

Thanks,

J

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