如何在Shiny中使checkboxgroupinput颜色编码 [英] how to make the checkboxgroupinput color-coded in Shiny

查看:686
本文介绍了如何在Shiny中使checkboxgroupinput颜色编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个开放的思想。我做了一些谷歌的研究,但没有一击。

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


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






其中names是一个字符向量。



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

有人知道该怎么做吗?非常感谢。

解决方案

你不能直接用闪亮的方式来做这个,但是你可以分析浏览器中闪亮的HTML inspector /

 < div id =variableclass =form-group shiny-input-checkboxgroup shiny-input-container > 
< label class =control-labelfor =variable>变量:< / label>
< div class =shiny-options-group>
< div class =checkbox>
< label>
< input type =checkboxname =variablevalue =1checked =checked/>
< span>一个< / span>
< / label>
< / div>
< div class =checkbox>
< label>
< input type =checkboxname =variablevalue =2checked =checked/>
< span>两个< / span>
< / label>
< / div>
< div class =checkbox>
< label>
< input type =checkboxname =variablevalue =3/>
< span>三< / span>
< / label>
< / div>
< / div>
< / div>

您可以使用 renderUI 重新创建它:

  my_checkboxGroupInput<  - 函数(变量,标签,选择,选定,颜色){
choices_names< - choices
if(length(names(choices))> 0)my_names < - names(options)
div(id = variable,class =form-group shiny-input-checkboxgroup shiny-input-容器闪亮边界输入,
HTML(paste0('< label class =control-labelfor =',variable,'>,label,'< / label>) ),
div(class =shiny-options-group,
HTML(paste0('< div class =checkboxstyle =color:',colors,'>
'< label>',
'< input type =checkboxname =',variable,
'value =',options,
' ',ifelse(选择%选中%,'checked ='选中'',''),
'/>',
'< span>,choices_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(输入,输出,会话){
输出$ my_cbgi< - renderUI(my_checkboxGroupInput(variable,Variable:,
choices = my_names,
selected = my_selected,
colors = my_colors))
}



编辑



为了在颜色中只显示选中的项目,需要稍微修改一下函数,并使用一个reactiveValue地图i输入$变量作为当前选择,如下所示:

  my_checkboxGroupInput<  -  function(variable,label,choices,selected,colors ){
my_names< - choices
if(length(names(choices))> 0)my_names < - names(options)
div(id = variable,class =form -group shiny-input-checkboxgroup shiny-input-container shiny-bound-input,
HTML(paste0('< label class =control-labelfor =',variable,'>' ,标签,'< / label>')),
div(class =shiny-options-group,
)(paste0('< div class =checkbox>
'< label>',
'< input type =checkboxname =',variable,
'value =',options,
' ',ifelse(%选择%,'checked','checked'',''),
'/>',
''< / label>',
'< / div>' ,'=','2'= 2,'3','=','=',' '= 3)
my_selected < - c(1,2)
my_colors< -c('blue','red','green')
shinyApp(ui = fluidPage (输入,输出,会话){
my < - reactiveValues(selected = my_selected)
observeEvent(input $ variable,{my $选择< - input $变量})
输出$ my_cbgi< - renderUI(my_checkboxGroupInput(variable,Variable:,
choices = my_names,
selected = my $ selected,
colors = my_colors))
})


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.

解决方案

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

这篇关于如何在Shiny中使checkboxgroupinput颜色编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆