Skip to content

Instantly share code, notes, and snippets.

@MayaGans
Last active January 18, 2022 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MayaGans/0c9dcaf0474b467796272a850f3aa9eb to your computer and use it in GitHub Desktop.
Save MayaGans/0c9dcaf0474b467796272a850f3aa9eb to your computer and use it in GitHub Desktop.
input hierarchy
library(shiny)
file_structure <- list(
LEVEL_1 = list(
LEVEL_2a = list(
LEVEL_3a1 = list(
LEVEL_4a1 = list()
),
LEVEL_3a2 = list(
LEVEL_4a3 = list()
),
LEVEL_3a3 = list(
LEVEL_4a3 = list()
)
),
LEVEL_2b = list(
LEVEL_3b1 = list(),
LEVEL_3b1 = list(),
LEVEL_3b2 = list()
)
)
)
ui <- fluidPage(
selectInput("level1", "Level 1", choices = NULL),
selectInput("level2", "Level 2", choices = NULL),
selectInput("level3", "Level 3", choices = NULL),
selectInput("level4", "Level 4", choices = NULL)
)
server <- function(input, output, session) {
# Create L1 using box_config - this doesnt change
observe({
updateSelectInput(session,
"level1",
choices = names(file_structure))
})
# L2 contingent on L1
L2 <- reactive({
req(input$level1)
file_structure[[1]]
})
observeEvent(L2(), {
updateSelectInput(session, "level2", choices = names(L2()))
})
# L3 contingent on L2
L3 <- reactive({
req(input$level1)
# browser()
if (input$level2 %in% names(L2())) {
L2()[[input$level2]]
} else {
return(list())
}
})
observeEvent(L3(), {
updateSelectInput(session, "level3", choices = names(L3()))
})
# L4 contingent on L3
L4 <- reactive({
req(input$level3)
if (input$level3 %in% names(L3())) {
return(L3()[[input$level3]])
} else {
return(list())
}
})
observeEvent(L4(), {
print(L4())
updateSelectInput(session, inputId = "level4", choices = names(L4()))
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment