checkboxGroupInput - 设置最小和最大选择数 - 刻度

zx8*_*754 6 checkbox r shiny

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

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

正如您从上图中可以看到的,我们可以根据需要选择多个,在这种情况下,可以选择4个中的4个.

如何设置最小和最大刻度数?我需要选中最少1个选项并选中最多3个选项.即:防止取消勾选的最后一跳,防止滴答当3个选项已经选中.

Por*_*hop 7

你可以这样做:

rm(list = ls())
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)
Run Code Online (Sandbox Code Playgroud)