首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当RShiny switchInput在uiOutput中时,它的样式如何?

当RShiny switchInput在uiOutput中时,它的样式如何?
EN

Stack Overflow用户
提问于 2022-06-13 17:24:07
回答 1查看 38关注 0票数 0

我以前曾在UI中使用以下样式标记来为来自ShinyWidgets的ShinyWidgets样式设置样式:

代码语言:javascript
复制
#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: #eef4fa;
                                        color: black;
                                        }'))),
  
  #switchInput color while off
  tags$head(tags$style(HTML('.bootstrap-switch .bootstrap-switch-handle-off.bootstrap-switch-success,
                                       .bootstrap-switch .bootstrap-switch-handle-on.bootstrap-switch-success {
                                        background: #fdf1f1;
                                        color: black;
                                        }'))),

但是,现在我已经将开关呈现为uiOutput,然后是renderUI,这些标记似乎没有被识别。

当开关是通过uiOutput生成的,而不是直接放到ui中时,我该如何设计开关的开、关版本呢?

代码语言:javascript
复制
library(shiny)
library(shinyjs)
library(bslib)
library(shinyBS)
library(shinyWidgets)

ui <- fluidPage(
  
  #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: #eef4fa;
                                        color: black;
                                        }'))),
  
  #switchInput color while off
  tags$head(tags$style(HTML('.bootstrap-switch .bootstrap-switch-handle-off.bootstrap-switch-success,
                                       .bootstrap-switch .bootstrap-switch-handle-on.bootstrap-switch-success {
                                        background: #fdf1f1;
                                        color: black;
                                        }'))),

  
  fluidRow(
    column(2,
           offset = 5,
           align = "center",
           uiOutput('log_axis_output')
    )),
    

)

server <- function(input, output) {
  
  output$log_axis_output <- renderUI({
    
    switchInput(
      inputId = "log_axis1",
      offLabel = "Log X\naxis?",
      onLabel = "Normal\nX axis",
      value = FALSE,
      offStatus = "success", 
      onStatus = "danger",
      disabled = FALSE)
    
  })

}

# Run the application 
shinyApp(ui = ui, server = server)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-13 21:07:46

这不是R码的问题。这是CSS代码的问题所在。

您的CSS选择器没有足够的优先级作为原始样式选择器。

若要修复,请更改为:

代码语言:javascript
复制
    tags$head(tags$style(HTML('.bootstrap-switch .bootstrap-switch-handle-off.bootstrap-switch-danger,
                                       .bootstrap-switch .bootstrap-switch-container .bootstrap-switch-handle-on.bootstrap-switch-danger {
                                        background: #eef4fa;
                                        color: black;
                                        }'))),
    
    #switchInput color while off
    tags$head(tags$style(HTML('.bootstrap-switch .bootstrap-switch-handle-off.bootstrap-switch-success,
                                       .bootstrap-switch .bootstrap-switch-container .bootstrap-switch-handle-off.bootstrap-switch-success {
                                        background: #fdf1f1;
                                        color: black;
                                        }'))),

我在中间增加了.bootstrap-switch-container以提高特异性水平。了解有关如何计算CSS特异性的更多信息。

另一种方法是使用!important,但通常不推荐使用它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72606798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档