如何使复选框组输入用闪亮的

时间:2017-01-23 19:18:15

标签: fonts colors shiny controls

有点心胸开阔。我和谷歌做了一些研究,但没有任何打击。

我有一个闪亮的项目,我在sidepanel中使用了checkboxGroupInput,就像这样:

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

其中'names'是一个字符向量。

我在主面板的图表中包含图例(不同名称的不同颜色)。现在我想在侧面板中更改名称的文本颜色(每个名称一种颜色),以便我可以摆脱图表中的图例。

有谁知道怎么做?非常感谢。

3 个答案:

答案 0 :(得分:2)

你不能直接用闪亮的方式做到这一点,但你可以在浏览器检查器中分析由flash构建的HTML /

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

然后您可以使用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))
    }
  )

修改

为了仅显示所选项目的颜色,您需要稍微修改该函数并使用reactiveValue将input $ variable映射为当前选择,如下所示:

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

答案 1 :(得分:1)

您可以使用tags$style

lapply(1:length(names), function(x) {
   n <- length(names)
   col <- gplots::col2hex(rainbow(n)[x])
   css_col <- paste0("#variable div.checkbox:nth-child(",x,
                     ") span{color: ", col,";}")
   tags$style(type="text/css", css_col)
}),
checkboxGroupInput("variable", "Variable:",
                   choices = names, selected = names)

答案 2 :(得分:0)

@HubertL

您的最终版本效果很好。然而,我想要更动态的东西 - 而不是永久地为选择分配颜色,我更喜欢为N选择的项目分配前N种颜色。不幸的是,我定制的代码不能按我想要的方式工作。

例如,我有6种颜色,并且默认情况下选择了所有六个变量,当我取消选中(二,三,四,五)中的任何一个时,我认为取消选中后的颜色会更新正常。让我们说('blue','red','green','purple','lemon','brown') ('one','two','three' , '四', '5', '6');当我取消'三'时,我想看('蓝','红','绿','紫','柠檬')('一','二','四','五' ,'六'),但实际颜色是('蓝色','红色','紫色','柠檬','蓝色')

这是我用于测试的代码:

  

my_names&lt; - c('one','two','three','four','five','six')

     

my_selected&lt; - c('one','two','three','four','five','six')

     

my_colors&lt; -c('blue','red','green','purple','lemon','brown')

     

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[1:length(my$selected)]))
     })