Limit number selected items in multiInput() from shinyWidgets package

橙三吉。 提交于 2019-12-19 11:59:10

问题


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()?


回答1:


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")
 )


来源:https://stackoverflow.com/questions/57102753/limit-number-selected-items-in-multiinput-from-shinywidgets-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!