【问题标题】:shiny checkboxgroupinput dynamic background color control闪亮的复选框组输入动态背景颜色控制
【发布时间】:2017-01-27 01:56:49
【问题描述】:

我正在设计一个完全动态的用户界面,目的是用闪亮的方式进行演示。我的清单上有几个步骤,我正在一步一步地工作。

  1. 自定义由函数'checkboxGroupInput'生成的多选框的背景颜色
  2. make the checkboxes more dynamic -- when the background color will be on/off when select/de-select one checkbox

我在另一篇文章中找到了解决方案,并且效果很好。 (how to make the checkboxgroupinput color-coded in Shiny) 这是我得到的代码:

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

现在,我想要更动态的东西——而不是永久地将颜色分配给一个选项,我更喜欢将前 N 个颜色分配给 N 个选择的项目。不幸的是,我自定义的代码没有按我想要的方式工作。

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

这是我用来测试的代码:

my_names <- c('one','two','three','four','five','six')
my_selected <- c('one','two','three','four','five','six')
my_colors <-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)]))
      })

【问题讨论】:

  • @HubertL 完成!谢谢。
  • 我无法想象你为什么要这样做

标签: colors background shiny


【解决方案1】:

这是该函数的更新版本,它将为您提供预期的结果。它使用observeEvent 的ignoreNULL 参数来跟踪最后一个复选框的取消选中。我必须添加一个变量来忽略第一个取消选择所有初始选择的调用:

my_checkboxGroupInput <- function(variable, label, choices, selected, colors){
    choices_names <- choices
    if(length(names(choices))>0) choices_names <- names(choices)
    my_colors <- rep("black", length(choices))
    is_selected <- choices %in% selected
    my_colors[is_selected] <- colors[1:sum(is_selected)]
    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:', my_colors, '">',
                         '<label>',
                         '<input type="checkbox" name="', variable, 
                         '" value="', choices, 
                         '"', ifelse(is_selected, 'checked="checked"', ''), 
                         '/>',
                         '<span>', choices_names,'</span>',
                         '</label>',
                         '</div>', collapse = " "))
        )
    )
  }


my_names <- c('one','two','three','four','five','six')
my_selected <- c('one','two','three','four','five','six')
my_colors <-c('blue','red','green','purple','lemon','brown')

shinyApp(ui=fluidPage(uiOutput("my_cbgi")),

         server = function(input, output, session) {
           my <- reactiveValues(selected=my_selected, firt_call_ignore=TRUE)
           output$my_cbgi <- renderUI(my_checkboxGroupInput("variable", "Variable:",
                                                            choices = my_names, 
                                                            selected=my$selected,
                                                            colors=my_colors ))
           observeEvent(input$variable,{
             if(my$firt_call_ignore)
               my$firt_call_ignore=FALSE
             else
               my$selected <- input$variable
             }, ignoreNULL = FALSE)
         })

【讨论】:

  • 嘿,我回来了。相信我,在我来这里问之前,我总是自己努力。当我取消选择所有复选框时,最后一个无法将字体颜色动态更改为黑色(默认)。比如我取消勾选('one','two','three','five','six'),现在('four')的字体颜色为蓝色;然后,如果我取消检查('四'),字体颜色应该是黑色,但它保持蓝色,直到至少有一个项目被重新检查。我以多种方式尝试了条件语句,但没有一个有效。 :(
  • 一开始我尝试了多次,但我没有足够的声誉将您的宝贵答案标记为“有用”。 :(
  • 哦,Shxt....我从来没有注意到有一个复选标记来接受。我很抱歉。 :(
  • 当然。你真是个热心的专家。顺便说一句,你能快速更新一下我的最新问题吗?
  • 再次感谢。虽然我一次又一次地感谢你。
猜你喜欢
  • 2018-07-14
  • 2021-08-29
  • 2016-05-03
  • 2016-02-18
  • 2014-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
相关资源
最近更新 更多