Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linked SelectInput controls in R Shiny - is it possible?

Tags:

r

shiny

Is it possible to have linked select input controls in R shiny. I currently have a dropdown to display the folders in a directory. I want a second dropdown to display the files in the folder selected. Is it possible?

Here is the ui.R:

shinyUI(pageWithSidebar(

  sidebarPanel(
    selectInput("Folder", "Folder:" ,  as.matrix(getFolders()),multiple = TRUE)
    selectInput("FileInFolder", "File in folder:" ) # can this dropdown be linked to one above???
  ),
  mainPanel(
))#end main
)

Here is the server.R

shinyServer(function(input, output) {}) 

Here is code to run it:

library(shiny)
runApp("C:/Users/me/Desktop/R Projects/FileFolder")

Here is my global.R file that populates the folders:

getFolders<-function()
{
  folders<-list.dirs("//nas/mypath/",full.names= FALSE,recursive = FALSE) 
  folders
}   
HERE is a fun ##How can I linkt his up to the getFolders so the UI is cascading?
getFilesInFolder<-function(Folder)
{
  files<-list.files(paste("//nas/mypath/",Folder,sep=""))
  files
}   

Any idea how to make this work so the UI is cascading. That is when you select a folder from the folder dropdown the Files Dropdown is updated??

Thank you.

like image 493
user3022875 Avatar asked Mar 31 '14 18:03

user3022875


1 Answers

Have a look to the updateSelectInput function.

Here is an example :

require(shiny)

datas <- data.frame(directory = c("a", "a", "a", "b", "b", "c"), file = sprintf("file%d", 1:6))

runApp(list(
  ui = basicPage(
    sidebarPanel(
      selectInput("directory", "Select a directory", choices = levels(datas$directory), selected = levels(datas$directory)[1]),
      tags$hr(),
      selectInput("files", "Select files", choices = datas$file[datas$directory == levels(datas$directory)[1]], multiple = TRUE)
    )
  ),
  server = function(input, output, session) {

    observe({
      directory <- input$directory

      updateSelectInput(session, "files", choices = datas$file[datas$directory == directory])
    })
  }
))
like image 54
Julien Navarre Avatar answered Sep 19 '22 00:09

Julien Navarre