R Shiny radioButtons how to change the colors of some of the choices?

梦想与她 提交于 2021-01-24 12:29:59

问题


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

server <- function(input, output) {}

I would like the font colors of "Normal" and "Uniform" to be different than those of the other choices. Let's say the color of the first two choices should be red.

Anyone who can achieve that?


回答1:


Just look at the examples in ?radioButtons. This will give you instructions on how to apply HTML tags to choices.

To summarize, you will have to use the arguments choiceNames and choiceValues.

  • choiceNames defines the ui of your radiobuttons. You can use HTML tags (tags$strong, tags$code, ... and HTML("some html string"))
  • choiceValues are the values that will be recieved by the server via input$dist.

Here is an example:

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(...) {}
)

Also, take a look at shinyWidgets::shinyWidgetsGallery() if you need inspiration for the styling of your radioButtons.



来源:https://stackoverflow.com/questions/49616376/r-shiny-radiobuttons-how-to-change-the-colors-of-some-of-the-choices

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!