Skip to content

trybik/shiny-directory-input

 
 

Repository files navigation

directoryInput

A widget for interactive selection of directories for R Shiny Applications

directory widget

Application

Provides an input for users to select directories via an interactive, and os native dialog, rather than having to type in paths in a textInput().

NOTE: This is intended to only be used with locally run shiny applications. It will not work on server deployed applications because it uses OS shell calls to present a directory choosing dialog. There is currently no way (that I'm aware) of securely presenting a client side directory selection dialog from a hosted web application.

Run the demo

Install the shiny R-package from CRAN:

install.packages('shiny')

Clone this git repository, navigate to where the ui.R and server.R files are located and run:

shiny::runApp()

Use the widget

  1. Copy the folder www/js to your app
  2. Copy directoryInput.R to your app
  3. In global.R (create it if it doesn't already exist) add the line:
source('directoryInput.R')

In ui.R:

  • Add the widget
directoryInput('directory', label = 'select a directory')
  • Add the widget with a default value
directoryInput('directory', label = 'select a directory', value = '~')

In server.R:

  • Observe when a user clicks the ... button to select a new directory
observeEvent(
  ignoreNULL = TRUE,
  eventExpr = {
    input$directory
  },
  handlerExpr = {
    if (input$directory > 0) {
      # condition prevents handler execution on initial app launch
      
      # launch the directory selection dialog with initial path read from the widget
      path = choose.dir(default = readDirectoryInput(session, 'directory'))
      
      # update the widget value
      updateDirectoryInput(session, 'directory', value = path)
    }
  }
)

About

An shiny input widget for selecting directories

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 78.3%
  • JavaScript 16.4%
  • Batchfile 5.3%