user7373139
user7373139

Reputation:

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

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?

Upvotes: 2

Views: 3546

Answers (1)

Gregor de Cillia
Gregor de Cillia

Reputation: 7685

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.

Upvotes: 8

Related Questions