You can not do this directly in shiny, BUT you can analyse the HTML built by shiny in the browser inspector/
<div id="variable" class="form-group shiny-input-checkboxgroup shiny-input-container">
<label class="control-label" for="variable">Variable:</label>
<div class="shiny-options-group">
<div class="checkbox">
<label>
<input type="checkbox" name="variable" value="1" checked="checked"/>
<span>one</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="variable" value="2" checked="checked"/>
<span>two</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="variable" value="3"/>
<span>three</span>
</label>
</div>
</div>
</div>
you can then recreate it using renderUI
:
my_checkboxGroupInput <- function(variable, label, choices, selected, colors){
choices_names <- choices
if(length(names(choices))>0) my_names <- names(choices)
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" style="color:', colors,'">',
'<label>',
'<input type="checkbox" name="', variable,
'" value="', choices,
'"', ifelse(choices %in% selected, 'checked="checked"', ''),
'/>',
'<span>', choices_names,'</span>',
'</label>',
'</div>', collapse = " "))
)
)
}
library(shiny)
my_names <- c('one'=1,'two'=2,'three'=3)
my_selected <- c(1,2)
my_colors <-c('blue','red','green')
shinyApp(
ui=fluidPage(uiOutput("my_cbgi")),
server = function(input, output, session) {
output$my_cbgi <- renderUI(my_checkboxGroupInput("variable", "Variable:",
choices = my_names,
selected=my_selected,
colors=my_colors))
}
)
Edit
In order to display in color only selected items, you need to slightly modify the function and use a reactiveValue to map input$variable as the current selection, like this:
my_checkboxGroupInput <- function(variable, label,choices, selected, colors){
my_names <- choices
if(length(names(choices))>0) my_names <- names(choices)
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>',
'<input type="checkbox" name="', variable,
'" value="', choices,
'"', ifelse(choices %in% selected, 'checked="checked"', ''),
'/>',
'<span ', ifelse(choices %in% selected, paste0('style="color:', colors,'"'),''), '>',my_names,'</span>',
'</label>',
'</div>', collapse = " "))
)
)
}
my_names <- c('one'=1,'two'=2,'three'=3)
my_selected <- c(1,2)
my_colors <-c('blue','red','green')
shinyApp(ui=fluidPage(uiOutput("my_cbgi")),
server = function(input, output, session) {
my <- reactiveValues(selected=my_selected)
observeEvent(input$variable,{my$selected <- input$variable})
output$my_cbgi <- renderUI(my_checkboxGroupInput("variable", "Variable:",
choices = my_names,
selected=my$selected,
colors=my_colors))
})