'Don't change R Shiny's selectInput's value unless necessary
Set Option A to E
Set Option B to 2
Option A changes even though I want it to stay where it is unless changed by the user or set to an option that is no longer available (ie if it's set to C and Option B is set to 3).
How is this done?
library("shiny")
library("bslib")
library("shinyWidgets")
ui <- bootstrapPage(
# https://bootswatch.com/journal/
theme = bs_theme(version = 5, "font_scale" = 1.0),
div(class = "container-fluid",
div(class = "row",
div(class="col-4",
selectInput(
inputId = "opt_a",
label = "Option A:",
choices = LETTERS[1:5],
selected = "A",
multiple = FALSE,
selectize = TRUE,
width = "120px",
size = NULL
),
),
div(class="col-4",
selectInput(
inputId = "opt_b",
label = "Option B:",
choices = 1:5,
selected = 1,
multiple = FALSE,
selectize = TRUE,
width = "120px",
size = NULL
),
),
)
)
)
# If Option B == 3, then Option A has no C
server <- function(input, output, session) {
observeEvent(input$opt_b, {
# DO THIS
if(input$opt_b == 3){
freezeReactiveValue(input, "opt_a")
updateSelectInput(
session = session,
inputId = "opt_a",
choices = c("A", "B", "D", "E"),
selected = "A"
)
} else {
freezeReactiveValue(input, "opt_a")
updateSelectInput(
session = session,
inputId = "opt_a",
choices = LETTERS[1:5]
)
}
})
}
shinyApp(ui, server)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
