lundi 24 juin 2019

Collapsible checkboxGroupInput in shiny

I have created a set of checkbox using renderUI and checkboxGroupInput. Here the result: enter image description here

What I would like to obtain now is something like this:

enter image description here

Where only the top results are shown with the possibility to expand the checkbox list.

Any suggestion on how to obtain this?

The code for the checkbox is the following:

Server.R:

    my_checkboxGroupInput <- function(variable, label,choices, selected, colors,perc){
    my_names <- choices
    log_funct("my_names",my_names,   verbose=T)
    if(length(names(choices))>0) my_names <- names(choices)
    log_funct("names(my_names)",my_names,   verbose=T)
    log_funct("choices",choices,   verbose=T)
     log_funct("selected",selected,   verbose=T)
    div(id=variable,class="form-group shiny-input-checkboxgroup shiny-input-container shiny-bound-input",
        HTML(paste0('<label class="control-label" for="',variable,'">',label,'</label>')),
        div( class="shiny-options-group",
             HTML(paste0('<div class="checkbox">',
                         '<label style="width: 100%">',
                         '<input type="checkbox" name="', variable, 
                         '" value="', choices, 
                         '"', ifelse(choices %in% selected, 'checked="checked"', ''), 
                         '/>',
                         #'<span ', ifelse(choices %in% selected, paste0('style=" background-color:',colors ,'; display: inline-block; white-space: nowrap; width: ',perc, '%;"'),''), '>',my_names,'</span>',
                         '<span ', paste0('style=" background-color:',colors ,'; display: inline-block; white-space: nowrap; width: ',perc, '%;"'),'>',my_names,'</span>',
                         '</label>',
                         '</div>', collapse = " "))
        )
    )
  }

  output$checkbox_cond <- renderUI({

    my_checkboxGroupInput("variable", "Variable:",choices = cond_plot()$Var1, 
                          selected=c(as.character(cond_plot()$Var1)[1],as.character(cond_plot()$Var1)[2]),
                          colors=c('#4e71ba'),
                          perc= cond_plot()$perc)
  })

The code is a modified version of the one in: how to make the checkboxgroupinput color-coded in Shiny




Aucun commentaire:

Enregistrer un commentaire