checkboxGroupInput - set minimum and maximum number of selections - ticks

后端 未结 2 2005
情歌与酒
情歌与酒 2021-02-14 13:41

Here is example code with check-box group input:

library(shiny)

server <- function(input, output) {
  output$Selected <- renderText({
    paste(input$Sele         


        
2条回答
  •  我在风中等你
    2021-02-14 14:13

    You can use a little JavaScript to do it:

    ## In a file named 'js4checkbox.js' in your app folder :
    $(document).ready(function(){
      $('input[name=SelecetedVars]').on('click', function(event){
        if($('input[name=SelecetedVars]:checked').length > 3){
          $(this).prop('checked', false);
        }
      });
      $('input[name=SelecetedVars]').on('click', function(event){
        if($('input[name=SelecetedVars]:checked').length == 0){
          $(this).prop('checked', true);
        }
      });
    });
    

    And in your ui add:

    fluidPage(
      includeScript(path = "js4checkbox.js"),
      ...
    )
    

    I don't know why but it doesn't work well in the RStudio Viewer so open it in your browser.

    For the JavaScript code see this post

提交回复
热议问题