Force user to select at least n number of options from pickerInput (R shiny)?

666 Views Asked by At

Hi helpful R community,

Problem: I have a list of two different types of organizations in two different pickerInputs- list_1 and list_2. I want to force the user to select a minimum of 5 total from both lists (example they could select 3 organizations from list_1 and 2 organizations from list_2). When they select at least 5 organizations I want to renderText in the mainPanel that prints what they have selected. If they have not selected at least 5 organizations I would like the message to be "Please select a minimum of 5 organizations to proceed!"

Here is a reprex:

# LIBRARIES ----
library(shiny)
library(shinyWidgets)
library(glue)



# USER INTERFACE ----
ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel = sidebarPanel(
      width = 4,
      p("Peer Group Comparisons"),
      
      pickerInput(
        inputId  = "list_1",
        label    = "Organizations from List 1",
        choices  = c("a", "b", "c", "d"),
        options  = pickerOptions(
          actionsBox = TRUE,
          liveSearch = TRUE),
           multiple = TRUE 
      ),
      pickerInput(
        inputId  = "list_2",
        label    = "Organizations from List 2",
        choices  = c("e", "f", "g", "h", "x", "y", "z"),
        options  = pickerOptions(
          actionsBox = TRUE,
          liveSearch = TRUE),
        multiple = TRUE 
      )
      ),
      
     
      mainPanel = mainPanel(width = 8,
                            textOutput("results")
      )
      
    )
    
  )
  
  
  # SERVER ----
  server <- function(input, output, session) {
    
    output$list1_and_list2 <- reactive({
      glue(input$list1, input$list2)
    })
    
    output$results <- renderText(
      list1_and_list2() 
    )
  }
  
  shinyApp(ui, server)
1

There are 1 best solutions below

0
On BEST ANSWER
library(shiny)
library(shinyWidgets)
library(glue)



# USER INTERFACE ----
ui <- fluidPage(

sidebarLayout(
    sidebarPanel = sidebarPanel(
        width = 4,
        p("Peer Group Comparisons"),
        
        pickerInput(
            inputId  = "list_1",
            label    = "Organizations from List 1",
            choices  = c("a", "b", "c", "d"),
            options  = pickerOptions(
                actionsBox = TRUE,
                liveSearch = TRUE),
            multiple = TRUE 
        ),
        pickerInput(
            inputId  = "list_2",
            label    = "Organizations from List 2",
            choices  = c("e", "f", "g", "h", "x", "y", "z"),
            options  = pickerOptions(
                actionsBox = TRUE,
                liveSearch = TRUE),
            multiple = TRUE 
        )
    ),
    
    
    mainPanel = mainPanel(width = 8,
                          textOutput("results")
    )
    
)

)


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

output$results <- renderText(
          if(length(c(input$list_1, input$list_2))<5) {
         "Please select a minimum of 5 organizations to proceed!"
          } else {
            c(input$list_1, input$list_2)
          }
 
)
}

shinyApp(ui=ui, server=server)