@Julien (Julien) posted a new topic, subject "Selectinput conditional formatting". It read:

   I have found a few ways to apply conditional formatting based on the value of the `choices`. Like this example from Dean Attali:
    [code] 
        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) {
        
          }
        
        ))
        
    [/code]
    
    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:
    [code] 
        tibble(choices = stringi8b7c:f320:99b9:690f:4595:cd17:293a:c069stri_rand_strings(20000, 5), group = stri_rand_strings(20000, 1, pattern = "[AB]"))
        
    [/code]
    
    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?

Then @nirgrahamuk (Nir A Graham) said

    adapted from:  
    
    
    stackoverflow.com
    
    ####  Shiny selectizeInput with value and label fields
    
    **javascript, r, shiny, selectize.js**
    
    answered by  St&#233;phane Laurent  on 08:11AM - 05 Feb 19 UTC
    [code] 
        library(shiny)
        library(stringi)
        t1 <- tibble(choices = stringi8b7c:f320:99b9:690f:4595:cd17:293a:c069stri_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) {
            
          }
          
        ))
    [/code]

After which, @Julien (Julien) added

    Nice, many thanks nirgrahamuk!!

Then @system (system) said

    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.


{'source': 'discourse', 'url': 'https://community.rstudio.com/t/selectinput-conditional-formatting/81563', 'title': 'Selectinput conditional formatting', 'views': 596, 'has_accepted_answer': True, 'word_count': 272, 'like_count': 1, 'reply_count': 1, 'participant_count': 2, 'posts': 4, 'tags': '', 'author': '@Julien (Julien)', 'participants': '@Julien (Julien), @nirgrahamuk (Nir A Graham), @system (system)', 'image_url': [None], 'created_at': '2020-09-22T09:44:09.904Z', 'last_posted_at': '2020-09-29T11:50:24.707Z'}