着色checkboxGroupInput选项

时间:2017-07-06 20:41:45

标签: html r checkbox shiny

在我的Shiny UI中我有

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

我希望它能让选择(文本)A和B染成红色,但C和D不是。我尝试过HTML,但后来在UI中出现了像#34; attribs&#34;和#34;孩子&#34;出现了。

提前致谢

2 个答案:

答案 0 :(得分:5)

由于shiny_1.0.1checkboxGroupInputchoiceNameschoiceValues个参数,用于传递任意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)

答案 1 :(得分:0)

伟大的维克多,我改善了您的回答,为它添加了多种变化。

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)