Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

fonts - how to make the checkboxgroupinput color-coded in Shiny

It's a bit open-minded. I did some research with google, but no hit at all.

I have a shiny project, in which I use a checkboxGroupInput in sidepanel, sth like this:

   checkboxGroupInput("variable", "Variable:",
                     choices = names ,selected = names
    )

where the 'names' is a character vector.

I include legend (different colors for different names) in a chart in main panel. Now I am thinking to change the text colors of the names (one color for each name) in sidepanel so that I can get rid of the legend in the chart.

Does anyone know how to do it? thanks so much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))
         })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...