fonts - 如何在 Shiny 中使 checkboxgroupinput 进行颜色编码

标签 fonts colors shiny controls

思想有点开放。我用谷歌做了一些研究,但没有成功。

我有一个 Shiny 的项目,其中我在侧面板中使用 checkboxGroupInput,如下所示:

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

其中“名称”是字符向量。

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

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

最佳答案

你不能直接在shiny中执行此操作,但你可以在浏览器检查器中分析shiny构建的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))
         })

关于fonts - 如何在 Shiny 中使 checkboxgroupinput 进行颜色编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41813960/

相关文章:

jquery - 是否可以使用 jquery、css 等更改图像部分的颜色

r - 使用 sprintf 打印换行符 - Shiny

python - 更改字体名称而不更改默认字体python

html - CSS:如何使字体图标和它下面的文字更接近?

css - 哪些网站提供公共(public)网络字体库链接?

java - 将背景更改为颜色后,Button 的 onClick 上的突出显示效果消失了

pdf - 使用 iTextSharp 检查 PDF 文档中的文本是否为粗体的方法有哪些

JavaFX StackedBarChart 自动在不同系列上重复使用相同的颜色 : how to avoid it?

r - 在 Shiny 的米中转换方差

css - R Shiny 中的响应式 CSS 属性