Can we isloate inside If function

In the below application, when we select second element in the Group By filter (Say Country), a render UI is popped up with default as 'USA'. When the user selects second country (sat UK) and selects Gender in the Group By filter, the second filter now becomes only USA (not USA and UK) . Wanted to check if we can still keep USA and UK in the second filter?

ui <- fluidPage(
  selectInput("groupby", "Group By", choices = c("Month","Country","Gender", "Site ID", "Treatment Arm"),multiple = TRUE,selected = c("Month")),
  uiOutput("coun")
)

server <- function(input, output, session) {
  observe({
    if(length(input$groupby) > 1){
      output$coun <- renderUI({selectInput("country", "Country",selected = "USA", choices = c("USA","UK","EUR"),multiple = TRUE)})
    } else {
      output$coun <- renderUI({})
    }
  })
}

shinyApp(ui, server)

this code does not perform the behaviour you describe.
It errors due to a lack of raw_data

My bad. I edited the code . Please refer

library(shiny)

ui <- fluidPage(
  selectInput("groupby", "Group By", choices = c("Month", 
                                                 "Country", 
                                                 "Gender", 
                                                 "Site ID", 
                                                 "Treatment Arm"),
              multiple = TRUE, selected = c("Month")),
  uiOutput("coun")
)

server <- function(input, output, session) {
  
  cntry <- reactiveVal("USA")
  observe({
    if (length(input$groupby) > 1) {
      output$coun <- renderUI({
        selectInput("country", "Country", selected = cntry(),
                    choices = c("USA", "UK", "EUR"), 
                    multiple = TRUE)
      })
    } else {
      output$coun <- renderUI({})
    }
  })

observeEvent(input$country,
             cntry(input$country))
}

shinyApp(ui, server)

Thanks a lot. It works :slight_smile:

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.