简体   繁体   中英

Limit number selected items in multiInput() from shinyWidgets package

Let say I have below Shiny app -

library(shinyWidgets); library("shiny")
ui <- fluidPage(
  multiInput(
    inputId = "id", label = "Fruits :",
     choices = c("Banana", "Blueberry", "Cherry",
                "Coconut", "Grapefruit", "Kiwi",
                "Lemon", "Lime", "Mango", "Orange",
                "Papaya"),
    selected = "Banana", width = "400px",
     options = list(
       enable_search = FALSE,
       non_selected_header = "Choose between:",
      selected_header = "You have selected:"
    )
   ),
   verbatimTextOutput(outputId = "res")
 )

 server <- function(input, output, session) {
   output$res <- renderPrint({
     input$id
  })
 }

shinyApp(ui = ui, server = server)

Now I want to put an upper limit on the number of items user can chose of 3. Is there any way to achieve this with multiInput() ?

Yes, you can provide a limit by specifying it in the options list:

ui <- fluidPage(
  multiInput(
    inputId = "id", label = "Fruits :",
     choices = c("Banana", "Blueberry", "Cherry",
                "Coconut", "Grapefruit", "Kiwi",
                "Lemon", "Lime", "Mango", "Orange",
                "Papaya"),
    selected = "Banana", width = "400px",
     options = list(
       limit = 3,
       enable_search = FALSE,
       non_selected_header = "Choose between:",
      selected_header = "You have selected:"
    )
   ),
   verbatimTextOutput(outputId = "res")
 )

In the same situation, is there a way to allow selected apps in a shiny app to switch to the other side in the selected order? For example, "Banana", "Cherry", "Kiwi", "Grapefruit", "Orange", "Mango", "Papaya", "Lime" are the list of selected ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM