Last active
January 18, 2022 19:31
-
-
Save MayaGans/0c9dcaf0474b467796272a850f3aa9eb to your computer and use it in GitHub Desktop.
input hierarchy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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