checkboxGroupInput-设置最小和最大选择数-滴答 [英] checkboxGroupInput - set minimum and maximum number of selections - ticks

查看:211
本文介绍了checkboxGroupInput-设置最小和最大选择数-滴答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是带有复选框组输入的示例代码:

Here is example code with check-box group input:

library(shiny)

server <- function(input, output) {
  output$Selected <- renderText({
    paste(input$SelecetedVars,collapse=",")
  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("SelecetedVars", "MyList:",
                         paste0("a",1:5), selected = "a1")
    ),
    mainPanel(textOutput("Selected"))
  )
)

shinyApp(ui = ui, server = server)

如上图所示,我们可以根据需要选择任意数量,在这种情况下,共5个中的4个。

As you you can see from image above we can select as many as we want, in this case 4 out of 5.

如何设置最小和最大刻度数?我需要检查最少1个选项,最多检查3个选项。即:防止取消勾选最后一个刻度,并在已经勾选 3个选项时防止勾选。

How can I set minimum and maximum number of ticks? I need minimum of 1 option checked and maximum of 3 options checked. i.e.: prevent unticking the last tick, and prevent ticking when 3 options were already ticked.

推荐答案

您可以执行以下操作:

library(shiny)

my_min <- 1
my_max <- 3

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("SelecetedVars", "MyList:",paste0("a",1:5), selected = "a1")
    ),
    mainPanel(textOutput("Selected"))
  )
)

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

  output$Selected <- renderText({
    paste(input$SelecetedVars,collapse=",")
  })

  observe({
    if(length(input$SelecetedVars) > my_max){
      updateCheckboxGroupInput(session, "SelecetedVars", selected= tail(input$SelecetedVars,my_max))
    }
    if(length(input$SelecetedVars) < my_min){
      updateCheckboxGroupInput(session, "SelecetedVars", selected= "a1")
    }
  })
}

shinyApp(ui = ui, server = server)

这篇关于checkboxGroupInput-设置最小和最大选择数-滴答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆