|
| 1 | + |
| 2 | +library(shiny) |
| 3 | +library(shinyWidgets) |
| 4 | + |
| 5 | +# data |
| 6 | +cities <- data.frame( |
| 7 | + continent = c("America", "America", "America", "Africa", |
| 8 | + "Africa", "Africa", "Africa", "Africa", |
| 9 | + "Europe", "Europe", "Europe", "Antarctica"), |
| 10 | + country = c("Canada", "Canada", "USA", "Tunisia", "Tunisia", |
| 11 | + "Tunisia", "Algeria", "Algeria", "Italy", "Germany", "Spain", NA), |
| 12 | + city = c("Trois-Rivières", "Québec", "San Francisco", "Tunis", |
| 13 | + "Monastir", "Sousse", "Alger", "Oran", "Rome", "Berlin", "Madrid", NA), |
| 14 | + stringsAsFactors = FALSE |
| 15 | +) |
| 16 | + |
| 17 | +# app |
| 18 | +ui <- fluidPage( |
| 19 | + theme = bslib::bs_theme(version = 5, preset = "bootstrap"), |
| 20 | + tags$h2("updateQuercusInput() example"), |
| 21 | + fluidRow( |
| 22 | + column( |
| 23 | + width = 6, |
| 24 | + quercusInput( |
| 25 | + inputId = "ID1", |
| 26 | + label = "Select cities:", |
| 27 | + choices = create_tree(cities), |
| 28 | + multiSelectEnabled = TRUE, |
| 29 | + initiallyExpanded = TRUE, |
| 30 | + returnValue = "text" |
| 31 | + ), |
| 32 | + verbatimTextOutput("res1") |
| 33 | + ), |
| 34 | + column( |
| 35 | + width = 6, |
| 36 | + textInput( |
| 37 | + inputId = "label", |
| 38 | + label = "Update label:", |
| 39 | + value = "Select cities:" |
| 40 | + ), |
| 41 | + checkboxGroupInput( |
| 42 | + inputId = "val_country", |
| 43 | + label = "Select countries:", |
| 44 | + choices = unique(cities$country), |
| 45 | + inline = TRUE |
| 46 | + ), |
| 47 | + checkboxGroupInput( |
| 48 | + inputId = "val_city", |
| 49 | + label = "Select cities:", |
| 50 | + choices = unique(cities$city), |
| 51 | + inline = TRUE |
| 52 | + ), |
| 53 | + actionButton("clear", "Clear selected"), |
| 54 | + actionButton("update", "Update choices"), |
| 55 | + actionButton("back", "Back to first choices") |
| 56 | + ) |
| 57 | + ) |
| 58 | +) |
| 59 | + |
| 60 | +server <- function(input, output, session) { |
| 61 | + |
| 62 | + output$res1 <- renderPrint(input$ID1) |
| 63 | + |
| 64 | + observe( |
| 65 | + updateTreeInput(inputId = "ID1", label = input$label) |
| 66 | + ) |
| 67 | + |
| 68 | + observe( |
| 69 | + updateQuercusInput(inputId = "ID1", selected = input$val_country) |
| 70 | + ) |
| 71 | + |
| 72 | + observe( |
| 73 | + updateQuercusInput(inputId = "ID1", selected = input$val_city) |
| 74 | + ) |
| 75 | + |
| 76 | + observeEvent(input$clear, { |
| 77 | + updateQuercusInput(inputId = "ID1", selected = character(0)) |
| 78 | + updateCheckboxGroupInput(inputId = "val_country", selected = character(0)) |
| 79 | + updateCheckboxGroupInput(inputId = "val_city", selected = character(0)) |
| 80 | + }) |
| 81 | + |
| 82 | + observeEvent(input$update, { |
| 83 | + cities <- data.frame( |
| 84 | + continent = c("Asia", "Asia", "Asia", "Australia", |
| 85 | + "Australia", "Australia", "Australia", "Australia", |
| 86 | + "South America", "South America", "South America", "Arctic"), |
| 87 | + country = c("Japan", "Japan", "China", "Australia", "Australia", |
| 88 | + "Australia", "New Zealand", "New Zealand", |
| 89 | + "Brazil", "Argentina", "Chile", NA), |
| 90 | + city = c("Tokyo", "Kyoto", "Beijing", "Sydney", |
| 91 | + "Melbourne", "Perth", "Auckland", "Wellington", |
| 92 | + "São Paulo", "Buenos Aires", "Santiago", NA), |
| 93 | + stringsAsFactors = FALSE |
| 94 | + ) |
| 95 | + updateQuercusInput(inputId = "ID1", choices = create_tree(cities)) |
| 96 | + }) |
| 97 | + |
| 98 | + observeEvent(input$back, { |
| 99 | + updateQuercusInput(inputId = "ID1", choices = create_tree(cities)) |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +if (interactive()) |
| 104 | + shinyApp(ui, server) |
0 commit comments