Fix: How do i implement shiny selectize inputs that update mutually?

 To implement Shiny selectize inputs that update mutually in R Shiny, you can use the `updateSelectizeInput` function in combination with reactive values. This allows you to create two or more selectize inputs that can update each other based on user selections. Here's an example of how to achieve this:


```R

library(shiny)


ui <- fluidPage(

  selectizeInput("select1", "Select Item 1", choices = NULL),

  selectizeInput("select2", "Select Item 2", choices = NULL)

)


server <- function(input, output, session) {

  # Initialize reactive values to store the choices for select1 and select2

  choices1 <- reactiveVal(NULL)

  choices2 <- reactiveVal(NULL)


  # Update the choices for select2 when select1 is changed

  observeEvent(input$select1, {

    # Simulate new choices for select2 based on the selected value in select1

    selected_value <- input$select1

    new_choices <- switch(selected_value,

      A = c("A1", "A2", "A3"),

      B = c("B1", "B2", "B3"),

      C = c("C1", "C2", "C3")

    )

    choices2(new_choices)


    # Update the choices in the UI using updateSelectizeInput

    updateSelectizeInput(session, "select2", choices = new_choices)

  })


  # Update the choices for select1 when select2 is changed

  observeEvent(input$select2, {

    # Simulate new choices for select1 based on the selected value in select2

    selected_value <- input$select2

    new_choices <- switch(selected_value,

      A1 = "A",

      A2 = "A",

      A3 = "A",

      B1 = "B",

      B2 = "B",

      B3 = "B",

      C1 = "C",

      C2 = "C",

      C3 = "C"

    )

    choices1(new_choices)


    # Update the choices in the UI using updateSelectizeInput

    updateSelectizeInput(session, "select1", choices = new_choices)

  })


  # Initialize choices for select1 and select2

  observe({

    initial_choices <- c("A", "B", "C")

    choices1(initial_choices)

    choices2(NULL)


    # Update the choices in the UI using updateSelectizeInput

    updateSelectizeInput(session, "select1", choices = initial_choices)

  })

}


shinyApp(ui, server)

```


In this example:


- Two selectize inputs, `select1` and `select2`, are created in the UI.


- The `choices1` and `choices2` reactive values are used to store the choices for `select1` and `select2`, respectively.


- When the user selects a value in `select1`, the choices for `select2` are updated based on the selected value. This is achieved using `observeEvent` and `updateSelectizeInput`.


- When the user selects a value in `select2`, the choices for `select1` are updated based on the selected value.


- The initial choices are set for `select1` and `select2` using `updateSelectizeInput` when the app starts.


You can adapt this example to your specific use case and data. The key is to use reactive values and `updateSelectizeInput` to update the choices based on user selections.

Comments

Popular posts from this blog

bad character U+002D '-' in my helm template

GitLab pipeline stopped working with invalid yaml error

How do I add a printer in OpenSUSE which is being shared by a CUPS print server?