Home > Back-end >  Don't change R Shiny's selectInput's value unless necessary
Don't change R Shiny's selectInput's value unless necessary

Time:02-28

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(, 
              selectInput(
                inputId = "opt_a",
                label = "Option A:",
                choices = LETTERS[1:5],
                selected = "A",
                multiple = FALSE,
                selectize = TRUE,
                width = "120px",
                size = NULL
              ),
          ),
          div(, 
              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)

CodePudding user response:

Perhaps you are looking for this

server <- function(input, output, session) {
  
  observeEvent(input$opt_b, {
    # DO THIS
    if(input$opt_b == 3){
      #freezeReactiveValue(input, "opt_a")
      choices <- c("A", "B", "D", "E")
      if (sum(choices %in% input$opt_a)>0) sel <- input$opt_a else sel <- choices[1]
      updateSelectInput(
        session = session,
        inputId = "opt_a",
        choices = choices,
        selected = sel
      ) 
    } else {  
      #freezeReactiveValue(input, "opt_a")
      sel <- input$opt_a
      updateSelectInput(
        session = session,
        inputId = "opt_a",
        choices = LETTERS[1:5], 
        selected = sel
      )
    }
  })
}
  • Related