【问题标题】:change colour of selectizeInput options in R Shiny在 R Shiny 中更改 selectizeInput 选项的颜色
【发布时间】:2020-08-03 23:36:26
【问题描述】:

我想在我的 Shiny 应用程序中更改 selectizeInput 菜单中每个单独选项的颜色。在下面的示例代码中,我可以将所有菜单选项的颜色更改为蓝色,但是如何为每个单独的选项更改它?例如使“a”红色,“b”蓝色,“c”绿色等。

非常感谢!

shinyApp(
  ui = 
    shinyUI(fluidPage(
      tags$head(
        tags$style(HTML("
     .item {
       background: #2196f3 !important;
       color: white !important;
     }
     .selectize-dropdown-content .active {
       background: #2196f3 !important;
       color: white !important;
     }
  "))
      ),
      sidebarLayout(
        sidebarPanel(
          selectizeInput("select", label=NULL,
                         choices=c("a", "b", "c", "d"),
                         selected = c("a", "b", "c", "d"),
                         multiple=TRUE, options=list(placeholder="Wybierz"))),
        mainPanel())
    )
    ),
  server = function(input, output){}
)
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats4    parallel  stats     graphics  grDevices utils     datasets  methods  
[9] base     

other attached packages:
 [1] rsconnect_0.8.16     shinythemes_1.1.2    dplyr_0.8.5          shiny_1.4.0.2       
 [5] BiocParallel_1.20.1  MLInterfaces_1.66.5  cluster_2.1.0        annotate_1.64.0     
 [9] XML_3.99-0.3         AnnotationDbi_1.48.0 IRanges_2.20.2       MSnbase_2.12.0      
[13] ProtGenerics_1.18.0  S4Vectors_0.24.4     mzR_2.20.0           Rcpp_1.0.4.6        
[17] Biobase_2.46.0       BiocGenerics_0.32.0 

【问题讨论】:

    标签: css r shiny shinyapps


    【解决方案1】:

    你可以这样做:

    .option[data-value=a] {
      background: red !important;
      color: white !important;
    }
    .option[data-value=b] {
      background: green !important;
      color: white !important;
    }
    .option[data-value=c] {
      background: blue !important;
      color: white !important;
    }
    .option[data-value=d] {
      background: magenta !important;
      color: white !important;
    }
    

    【讨论】:

    • 感谢您的回复。我不熟悉css。您能否编辑您的回复以包括我关于如何准确放置它的完整示例?我添加了.item[data-value=a] 来更改每个项目的颜色,但即使我包含.selectize-dropdown-content[data-value=a],下拉菜单中的颜色也不会改变。谢谢。
    • @LisaBreckels html 代码中没有 item 类。你只需要用我的 CSS 替换你的 CSS。
    • 感谢您的帮助。我不确定为什么我需要在下面的代码中调用 item 来为菜单和下拉菜单着色?我已在下面明确包含详细信息,因为这解决了我的问题。
    • @LisaBreckels 好的。无需重复代码,您可以执行.option[data-value=a], .item[data-value=a] { ......
    【解决方案2】:

    用上面建议的代码替换我的代码会改变下拉菜单的颜色,但不是菜单中的各个项目:

    
    shinyApp(
      ui = 
        shinyUI(fluidPage(
          tags$head(
            tags$style(HTML("
            .option[data-value=a] {
              background: red !important;
              color: white !important;
            }
            .option[data-value=b] {
              background: green !important;
              color: white !important;
            }
      "))
          ),
          sidebarLayout(
            sidebarPanel(
              selectizeInput("select", label=NULL,
                             choices=c("a", "b"),
                             selected = c("a", "b"),
                             multiple=TRUE, options=list(placeholder="Wybierz"))),
            mainPanel())
        )
        ),
      server = function(input, output){}
    )
    

    解决方案 为了实现项目的颜色编码和下拉。我需要将.item 添加到我的代码中

    
    shinyApp(
      ui = 
        shinyUI(fluidPage(
          tags$head(
            tags$style(HTML("
            .option[data-value=a], .item[data-value=a]{
              background: red !important;
              color: white !important;
            }
            .option[data-value=b], .item[data-value=b]{
              background: green !important;
              color: white !important;
            }
      "))
          ),
          sidebarLayout(
            sidebarPanel(
              selectizeInput("select", label=NULL,
                             choices=c("a", "b"),
                             selected = c("a", "b"),
                             multiple=TRUE, options=list(placeholder="Wybierz"))),
            mainPanel())
        )
        ),
      server = function(input, output){}
    )
    

    这会导致菜单和下拉菜单都被着色。

    【讨论】:

    • 这看起来很棒,但是 css 也可以是“反应式的”吗?我正在尝试在从 renderUI 生成的选择框中为某些输入着色。我想为与其他一些反应性数据元素关联的选择着色。可以在 renderUI 函数中更新 CSS 吗?
    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 2018-01-04
    • 2020-11-25
    • 1970-01-01
    • 2021-09-16
    • 2018-03-29
    • 2021-01-13
    • 2021-05-28
    相关资源
    最近更新 更多