【问题标题】:Coloring the checkboxGroupInput choices为 checkboxGroupInput 选项着色
【发布时间】:2017-12-11 00:38:52
【问题描述】:

在我闪亮的 UI 中

ui <- checkboxGroupInput("my_cbgi", "Choose Something", c("A", "B", "C", "D"))

我希望选项(文本)A 和 B 是红色的,但 C 和 D 不是。我尝试了 HTML,但随后在 UI 中出现了“attribs”和“children”等奇怪的框。

提前致谢

【问题讨论】:

标签: html r checkbox shiny


【解决方案1】:

由于shiny_1.0.1checkboxGroupInput 有一个choiceNameschoiceValues 参数用于传递任意UI 以显示给用户,请查看以下示例:

library("shiny")

ui <- fluidPage(
  checkboxGroupInput(
    inputId = "my_cbgi",
    label = "Choose Something", 
    choiceNames = list(
      tags$span("A", style = "color: red;"),
      tags$span("B", style = "color: red;"), 
      tags$span("C", style = "color: blue;"), 
      tags$span("D", style = "font-weight: bold;")
    ),
    choiceValues = c("A", "B", "C", "D")
  )
)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

【讨论】:

    【解决方案2】:

    伟大的维克多,我改进了你的答案,添加了不同的行为。

    library("shiny")
    
    my.options <- c('A', 'B', 'C')
    my.colors <- c('red', 'green', 'blue')
    
    my.fun <- function() {
      res <- list()
      for (o in my.options) {
        res[[length(res)+1]] <- tags$span(o, 
                                          style = paste0('color: ', my.colors[which(my.options == o)],';'))
      }
      res
    }
    
    ui <- fluidPage(
      checkboxGroupInput(inputId = "myId", label = "Options", 
                         choiceNames = my.fun(),
                         choiceValues = my.colors
      )
    )
    server <- function(input, output) {}
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多