R Shiny radioButtons 如何改变某些选项的颜色?

lar*_*ara 2 shiny

ui <- fluidPage(
         radioButtons("dist", "Distribution type:",
                       c("Normal" = "norm",
                        "Uniform" = "unif",
                        "Log-normal" = "lnorm",
                        "Exponential" = "exp")))

server <- function(input, output) {}
Run Code Online (Sandbox Code Playgroud)

我希望“Normal”和“Uniform”的字体颜色与其他选项的字体颜色不同。假设前两个选项的颜色应该是红色。

谁能做到这一点?

Gre*_*lia 6

只需查看 中的示例?radioButtons。这将为您提供有关如何将HTML标签应用于选项的说明。

总而言之,您将不得不使用参数choiceNameschoiceValues

  • choiceNames定义单选按钮的 ui。您可以使用HTML标签 ( tags$strong, tags$code, ... and HTML("some html string"))
  • choiceValues是服务器将通过input$dist.

下面是一个例子:

library(shiny)

shinyApp(
  fluidPage(
    radioButtons(
      inputId = "dist",
      label = "Distribution type:",
      choiceNames = list(
        HTML("<font color='red'>Normal</font>"), 
        tags$span(style = "color:red", "Uniform"), 
        "Log-normal", "Exponential"
      ),
      choiceValues = c("norm", "unif", "lnorm", "exp")
    )
  ),
  server = function(...) {}
)
Run Code Online (Sandbox Code Playgroud)

另外,看看shinyWidgets::shinyWidgetsGallery()你是否需要灵感来设计你的radioButtons。