【问题标题】:Change shinyWidget's switchInput default colors in Shiny app在 Shiny 应用程序中更改 shinyWidget 的 switchInput 默认颜色
【发布时间】:2021-05-07 20:58:05
【问题描述】:

如何更改 SwitchInput 元素的颜色(来自 ShinyWidgets 包)?

【问题讨论】:

    标签: r shiny bootstrap-switch shinywidgets


    【解决方案1】:

    首先,在您的switchInput() 函数中,您必须通过更改onStatusoffStatus 参数来指定“开”和“关”状态:

    switchInput(
                           inputId = "switch",
                           label = "Label", 
                           labelWidth = "120px", 
                           onLabel = "ON",
                           offLabel = "OFF",
                           onStatus = "danger",
                           offStatus = "info"
                         ),
    
    

    然后,在您的 UI.r 文件或 Shiny 应用的 UI 部分中,添加以下 CSS 标记:

    #switchInput color while on
                tags$head(tags$style(HTML('.bootstrap-switch .bootstrap-switch-handle-off.bootstrap-switch-danger,
                                           .bootstrap-switch .bootstrap-switch-handle-on.bootstrap-switch-danger {
                                            background: green;
                                            color: white;
                                            }'))),
                
    #switchInput color while off
                tags$head(tags$style(HTML('.bootstrap-switch .bootstrap-switch-handle-off.bootstrap-switch-info,
                                           .bootstrap-switch .bootstrap-switch-handle-on.bootstrap-switch-info {
                                            background: red;
                                            color: black;
                                            }'))),
    

    使用background设置开关的背景色,color设置文字颜色。

    【讨论】:

    • 此代码是否还会更改其他对象的“信息”和“危险”颜色,还是仅更改开关?
    • @Jamie 仅适用于开关,因为颜色变化是通过匹配开关的 CSS 类来完成的
    猜你喜欢
    • 2021-05-09
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    相关资源
    最近更新 更多