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.
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])
})
}
))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With