Selectinput conditional formatting

I have found a few ways to apply conditional formatting based on the value of the choices. Like this example from Dean Attali:

appCSS <- 

"#color ~ .selectize-control.single .selectize-dropdown [data-value=blue] { color: blue }

 #color ~ .selectize-control.single .selectize-dropdown [data-value=red] { color: red }"

runApp(shinyApp(

  ui = fluidPage(

    tags$head(tags$style(HTML(appCSS))),

    selectInput("color", "Color", c("blue", "red"))

  ),

  server = function(input, output, session) {

  }

))

However I haven't found a good way to apply formatting based on a variable that is NOT choices. My question is: suppose I have 20000 unique choices that are either in group A or B:

tibble(choices = stringi::stri_rand_strings(20000, 5), group = stri_rand_strings(20000, 1, pattern = "[AB]"))

Is there an efficient way to make all choices in group A colored in blue in the selectInput and choices in group B be red?

adapted from:

library(shiny)
library(stringi)
t1 <- tibble(choices = stringi::stri_rand_strings(200, 5), 
             group = stri_rand_strings(200, 1, pattern = "[AB]")) %>%
  mutate(html=ifelse(group=='A',paste0("<span style='color:red';>",choices,"</span>"),
                     paste0("<span style='color:blue';>",choices,"</span>")
                     ))

items <- setNames(t1$choices, t1$html)


runApp(shinyApp(
  
  ui = fluidPage(
    
    
    selectizeInput("id", "Label", choices = items, 
                   options = list(render = I("
  {
    item: function(item, escape) { return '<div>' + item.label + '</div>'; },
    option: function(item, escape) { return '<div>' + item.label + '</div>'; }
  }"))) 
    
  ),
  
  server = function(input, output, session) {
    
  }
  
))
2 Likes

Nice, many thanks nirgrahamuk!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.