Home > Enterprise >  updateSelectInput() called in Server not refreshing UI in Shiny
updateSelectInput() called in Server not refreshing UI in Shiny

Time:08-25

I have a simple UI, and I want to populate the selectInput with the results of a query.

ui <- fluidPage(
  theme = bs_theme(version = 4, bootswatch = "minty"),
  headerPanel("Adverse Event Fuzzy Search Tool"),
  
  fluidRow(
    column(3, selectInput("ingredients", label = "Select on or more Active Ingredients:", choices = NULL, multi=TRUE, 
                          selectize = TRUE)),
  
    column(3, textInput("search_term", "AE Search Term:")),
    
    column(3, sliderInput("search_tolerance", label = "Search Tolerance, lower means more accuracy:",
                          min = 0, max = 0.7, value = 0.2, step = 0.05)),
    
    column(3, actionButton("do_search", "Perform Search"))
  )
  ,fluidRow(
    reactableOutput("search_results")
  )
  
  
)

In my Server I am creating reactives which call functions which query Oracle and return a dataframe. The functions work, as I was previously calling these functions in the root of app.R.

But my selectInput is not getting updated. I had thought the reactive would get initialised on session start.

server <- function(input, output, session) {
  
  # retrieve master data set for session
  cases_df <- reactive({
    return(get_adverse_events_from_db())
  })
  
  ingredients_df <- reactive({
    df <- get_list_of_actives_from_db()
    message(paste("Length of actives: ", nrow(df)))
    
    updateSelectInput(session, 
                      "ingredients",
                      label = "Select on or more Active Ingredients:",
                      choices = df$PRIMARY_SUSPECT_KEY_INGREDIENT,
                      selected = NULL 
    )
    
    return(df)
    
  })

}

CodePudding user response:

reactives are used to return a value, observeEvents for their side effect like updating the UI. Try:

server <- function(input, output, session) {
  
  # retrieve master data set for session
  cases_df <- reactive({
    return(get_adverse_events_from_db())
  })
  
  ingredients_df <- reactive({
    df <- get_list_of_actives_from_db()
    message(paste("Length of actives: ", nrow(df)))
    
    return(df)
    
  })
  
  observeEvent(ingredients_df(), {
    updateSelectInput(session, 
                      "ingredients",
                      label = "Select on or more Active Ingredients:",
                      choices = ingredients_df()$PRIMARY_SUSPECT_KEY_INGREDIENT,
                      selected = NULL 
    )
  })
  
}
  • Related