Skip to content Skip to sidebar Skip to footer

Coloring The Checkboxgroupinput Choices

In my Shiny UI I have ui <- checkboxGroupInput('my_cbgi', 'Choose Something', c('A', 'B', 'C', 'D')) And I would like it so that the choices (the text) A and B are colored red,

Solution 1:

Since shiny_1.0.1, checkboxGroupInput have a choiceNames and choiceValues arguments for passing arbitrary UI to display to the user, check this example :

library("shiny")

ui <- fluidPage(
  checkboxGroupInput(
    inputId = "my_cbgi",
    label = "Choose Something", 
    choiceNames = list(
      tags$span("A", style = "color: red;"),
      tags$span("B", style = "color: red;"), 
      tags$span("C", style = "color: blue;"), 
      tags$span("D", style = "font-weight: bold;")
    ),
    choiceValues = c("A", "B", "C", "D")
  )
)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

Solution 2:

Great Victorp, I improved your answer adding a varying behaviour to it.

library("shiny")

my.options <- c('A', 'B', 'C')
my.colors <- c('red', 'green', 'blue')

my.fun <- function() {
  res <- list()
  for (o in my.options) {
    res[[length(res)+1]] <- tags$span(o, 
                                      style = paste0('color: ', my.colors[which(my.options == o)],';'))
  }
  res
}

ui <- fluidPage(
  checkboxGroupInput(inputId = "myId", label = "Options", 
                     choiceNames = my.fun(),
                     choiceValues = my.colors
  )
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)

Post a Comment for "Coloring The Checkboxgroupinput Choices"