简体   繁体   中英

css - RShiny widgets: How to color the radioButtons() and checkboxGroupInput()?

I'm using many different checkboxGroupInput() and radioButtons() in my RShiny app, one code snippet looks like this:

checkboxGroupInput(inputId = "maDays", label = "Select Trading Days", 
                                      choices = c("Monday", "Tuesday", "Wednesday", "Thursday",
                                                  "Friday", "Saturday", "Sunday"),
                                      selected = c("Monday", "Tuesday", "Wednesday", "Thursday",
                                                   "Friday"), 
                                      inline = TRUE),
radioButtons(inputId = "maTimeGran", label = "Select Time Granularity", 
                                choices = c("Hourly" = "hour", "Daily" = "day", 
                                            "Weekly" = "week"),
                                selected = "day")

and gives:

在此处输入图像描述 在此处输入图像描述

Now my question is: How can I change the coloring of all my selected radioButtons (button, not text) and checkmarks (not text) with a css -tag? My preferred color would be #007d3c .

I was able to adapt the approach from https://dev.to/kallmanation/styling-a-radio-button-with-only-css-4llc to get this to work. (The checkboxes have a solid fill color instead of checkmarks when selected.)

Here's the code:

library(shiny)

ui <- fluidPage(
    tags$head(
        tags$style(HTML("
                    label > input[type='radio'] {
                        opacity: 0;
                        position: absolute;
                    }
                    label > input[type='radio'] + *::before {
                        content: '';
                        margin: 4px 0 0;
                        width: 13px;
                        height: 13px;
                        position: absolute;
                        margin-left: -20px;
                        border-radius: 50%;
                        border-style: solid;
                        border-width: 0.1rem;
                        border-color: #007d3c;
                    }
                    label > input[type='radio']:checked + *::before {
                        background: radial-gradient(white 0%, white 30%, #007d3c 30%, #007d3c);
                                border-color: #007d3c;
                    }
                    label > input[type='checkbox'] {
                        opacity: 0;
                        position: absolute;
                    }
                    label > input[type='checkbox'] + *::before {
                      content: '';
                      position: absolute;
                      margin: 4px 0 0;
                      margin-left: -20px;
                      align: center;
                      width: 13px;
                      height: 13px;
                      margin-right: 1rem;
                      border-radius: 0%;
                      border-style: solid;
                      border-width: 0.1rem;
                      border-color: #007d3c;
                    }
                    label > input[type='checkbox']:checked + *::before {
                      content: '';
                      width: 13px;
                      height: 13px;
                      background-color: #007d3c;
                    }
                  "))
    ),
    checkboxGroupInput(inputId = "maDays", label = "Select Trading Days", 
                       choices = c("Monday", "Tuesday", "Wednesday", "Thursday",
                                   "Friday", "Saturday", "Sunday"),
                       selected = c("Monday", "Tuesday", "Wednesday", "Thursday",
                                    "Friday"), 
                       inline = TRUE),
    radioButtons(inputId = "maTimeGran", label = "Select Time Granularity", 
                 choices = c("Hourly" = "hour", "Daily" = "day", 
                             "Weekly" = "week"),
                 selected = "day")
)

server <- function(input, output) {
    ## put server code here
}

shinyApp(ui = ui, server = server)

Here's my original answer, which styles the labels for checked elements, in case this is useful to anyone else:

library(shiny)

ui <- fluidPage(
    tags$head(
        tags$style(HTML("
                  input:checked + span {
                    color: #007d3c;
                  }
                  "))
    ),
    checkboxGroupInput(inputId = "maDays", label = "Select Trading Days", 
                       choices = c("Monday", "Tuesday", "Wednesday", "Thursday",
                                   "Friday", "Saturday", "Sunday"),
                       selected = c("Monday", "Tuesday", "Wednesday", "Thursday",
                                    "Friday"), 
                       inline = TRUE),
    radioButtons(inputId = "maTimeGran", label = "Select Time Granularity", 
                 choices = c("Hourly" = "hour", "Daily" = "day", 
                             "Weekly" = "week"),
                 selected = "day")
)

server <- function(input, output) {
  ## put server code here
}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM