【问题标题】:how to make the checkboxgroupinput color-coded in Shiny如何使复选框组输入在 Shiny 中进行颜色编码
【发布时间】:2017-06-08 09:53:24
【问题描述】:

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

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

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

“名称”是一个字符向量。

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

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

【问题讨论】:

  • 您应该尽量避免使用现有的 R 函数名称,例如 names 来存储您的内容。更喜欢使用my_names
  • 感谢提醒。这不是我的代码,我只是复制了一个更简单的代码作为示例。请您看一下我在您的答案下发布的问题吗?谢谢

标签: fonts colors shiny controls


【解决方案1】:

@HubertL

您的最终版本效果很好。但是,我想要更动态的东西——而不是永久地将颜色分配给一个选项,我更喜欢将前 N 个颜色分配给 N 个选择的项目。不幸的是,我自定义的代码没有按照我想要的方式工作。

例如,我有 6 种颜色,并且默认选择了所有六个变量,当我取消选中(二、三、四、五)中的任何一个时,我假设取消选中后的颜色会更新适当地。假设 ('blue','red','green','purple','lemon','brown') 表示 ('one','two','three' ,'四','五','六');当我取消选中“三”时,我想看到 ('blue','red','green','purple','lemon') 的 ('one','two','four','five' ,'six'),但实际颜色是('blue','red','purple','lemon','blue')

这是我用来测试的代码:

my_names

my_selected

my_colors

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

【讨论】:

  • 你不应该在答案中提出问题,而是提出一个新问题
  • @HubertL 抱歉,我不知道这条规则。这是新帖子:stackoverflow.com/questions/41886018/…
  • 请删除这个“答案”
【解决方案2】:

您可以为此使用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)

【讨论】:

  • 抱歉,我在我的一个应用程序中测试了此代码,但忘记更改(将对其进行编辑)。 #filespar 是我的 checkboxGroupInput 的 ID。在你的情况下是#variable。请指明checkboxGroupInput 的ID,否则我担心您的应用中的所有checkboxGroupInputs 都会在这些tag$styles 之后着色。
【解决方案3】:

您不能直接在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))
         })

【讨论】:

  • 非常感谢编码解释。非常有帮助。让我根据您的提示修改我的项目。
  • 谢谢,HubertL,你的方法很好用。现在,我有一个更高级的问题:如果我只想对选中的复选框进行颜色编码,这意味着,当我取消选中一个时,字体颜色会动态更改为默认值(黑色),反之亦然。我很努力,但没有找到任何交互方式来实现这一点。
  • 我不太符合你上次更新的逻辑。您在服务器会话中调用了函数“my_checkboxGroupInput”,但是 1)复选框显然是输入 2)输入变量(“my$selected
  • 当需要对 ui 进行更深入的控制时,在服务器部分构建 ui 是很常见的
  • 很高兴知道。我从来没有这样做过b4。谢谢,祝您有愉快的一天。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 2016-06-14
  • 2021-05-31
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多