Is this correct practice to reinitialise a UI with the current values of a reactive entity?

I currently have the following problem: I have a UI component containing a selectInput.

 binaryAnalysisInputUI <- function(id, selection_table) {
   ns <- NS(id)
   return(
     fluidRow(
       column(12,
         h4("Options"),
         selectInput(
           ns("outcomeSelect"),
           "Outcome",
           choices = c())
       )
     )
   )
 
 }
 
 binaryAnalysisInputServer <- function(input, output, session,
                                       selection_table ) {
   binary_names <- SelectionTable$SelectionTable_getNamesForType(
     selection_table,
     "binary")
 
   observeEvent({ binary_names() }, {
     updateSelectInput(session, "outcomeSelect", choices=binary_names())
   })
   return(reactive({input$outcomeSelect}))
}

This UI component is rendered conditionally in a containing UI element server, like this:

  output$analysisInputUi <- renderUI({
    if (input$typeSelect == "binary") {
      return(binaryAnalysisInput$binaryAnalysisInputUI(session$ns("binaryAnalysisInputUi")))
    } else if (input$typeSelect == "continuous") { 
  return(continuousAnalysisInput$continuousAnalysisInputUI(session$ns("continuousAnalysisInputUi")))
    }

Now the problem is that when I switch between the two, the accumulated visual state is of course thrown away. The select input will be empty. I expected this, and compensated by passing the appropriate value at the UI, then compute the choice from it in an isolated context.

binaryAnalysisInputUI <- function(id, selection_table) {
  ns <- NS(id)
  binary_names <- isolate(SelectionTable$SelectionTable_getNamesForType(
    selection_table,
    "binary"))
  return(
    fluidRow(
      column(12,
        h4("Options"),
        selectInput(
          ns("outcomeSelect"),
          "Outcome",
          choices = binary_names())
      )
    )
  )
}

This post is to verify my question: is this the expected way to deal with conditional rendering in shiny, and if yes, how is initialisation against reactive values generally performed?

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.