【问题标题】:Change colour of pickerInput items in Shiny在 Shiny 中更改 pickerInput 项目的颜色
【发布时间】:2020-11-25 01:58:48
【问题描述】:

从this example 开始,有人可以告诉我这是否可能以及如何从shinyWidgets 包中更改pickerInput UI 的下拉菜单中项目的字体颜色?

这是一个小部件的简短示例:

library("shiny")
library("shinyWidgets")

shinyApp(
  ui = 
    shinyUI(fluidPage(
      sidebarLayout(
        sidebarPanel(
          pickerInput("select", label=NULL,
                      choices=LETTERS,
                      selected = LETTERS,
                      multiple=TRUE, 
                      options = list(
                        `actions-box` = TRUE,
                        size = 10,
                        `selected-text-format` = "count > 3"
                      ))
          ),
        mainPanel())
    )
    ),
  server = function(input, output){}
)
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.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/4.0/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   base     

other attached packages:
 [1] shinyWidgets_0.5.3  dendextend_1.13.4   tidyr_1.1.0         patchwork_1.0.1     ggplot2_3.3.1      
 [6] shinyhelper_0.3.2   colorspace_1.4-1    colourpicker_1.0    shinythemes_1.1.2   DT_0.13            
[11] dplyr_1.0.0         shiny_1.4.0.2       MSnbase_2.14.2      ProtGenerics_1.20.0 S4Vectors_0.26.1   
[16] mzR_2.22.0          Rcpp_1.0.4.6        Biobase_2.48.0      BiocGenerics_0.34.0

【问题讨论】:

    标签: css r shiny


    【解决方案1】:

    尝试添加

    tags$head(
          tags$style(HTML("
            .dropdown-menu a{
                color: red !important;
            }
      "))
    

    这是你要找的吗?

      ui = 
        shinyUI(fluidPage(tags$head(
          tags$style(HTML("
             .dropdown-menu a{
              color: red !important;
             }
      "))
        ),
          sidebarLayout(
            sidebarPanel(
              pickerInput("select", label=NULL,
                          choices=LETTERS,
                          selected = LETTERS,
                          multiple=TRUE, 
                          options = list(
                            `actions-box` = TRUE,
                            size = 10,
                            `selected-text-format` = "count > 3"
                          ))
            ),
            mainPanel())
        )
        )
    server = function(input, output){}
    
    shinyApp(ui, server)
    

    【讨论】:

    • 是否可以编辑答案以明确如何对单个字母进行颜色编码。此示例仅显示如何将下拉菜单中的 所有 项着色为相同颜色。谢谢!
    【解决方案2】:

    您可以在其参数中应用您想要的样式:

    library(shiny)
    library(shinyWidgets)
    
    col.list <- c("red","blue","green","orange")
    
    # Change the color
    colors <- paste0("color:",col.list,";")
    
    # Change the font
    colors <- paste0(colors,"font-family: Arial;")
    
    # Change to bold
    colors <- paste0(colors,"font-weight: bold;")
    
    ui <- fluidPage(
        pickerInput("col", "Colour", multiple=T, choices = col.list, 
                    choicesOpt = list(
                        style = colors)
        )
    )
    
    server <- function(input, output){}
    
    shinyApp(ui, server)
    

    要更改背景,只需应用 background

    library(shiny)
    library(shinyWidgets)
    col.list <- c("red","blue","green","orange")
    #Change the color
    colors <- paste0("background:",col.list,";")
    #Change the color
    colors <- paste0(colors,"color:white;")
    #Change the font
    colors <- paste0(colors,"font-family: Arial;")
    #Change to bold
    colors <- paste0(colors,"font-weight: bold;")
    
    ui <- fluidPage(
        pickerInput("col", "Colour", multiple=T, choices = col.list, 
                    choicesOpt = list(
                        style = colors)
        )
    )
    
    server <- function(input, output){}
    
    shinyApp(ui, server)
    

    要动态使用颜色,您可以执行以下操作:

    library(shiny)
    library(shinyWidgets)
    col.list <- c("red","blue","green","orange")
    
    ui <- fluidPage(
      column(2,
             pickerInput("change", "Select Colour", choices = col.list,multiple = FALSE)
      ),
      column(2,
             pickerInput("col", "Colour", multiple=T, choices = col.list)
      )
    )
    server <- function(input, output,session){
    
      observeEvent(input$change,{
        
        colors <- rep(paste0("color:",input$change,";"),length(col.list))
        updatePickerInput(session, "col", choices = col.list,
                          choicesOpt = list(
                            style = colors
                          )
        )
      })
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 这很棒。我自己的代码(不是此处的示例)中的对象colors 是动态的,它是由用户从我的应用程序内的颜色选择器中设置的。因此colors 是一个reactive 对象,我可以使用'renderUI' 从服务器传递它,然后使用uiOutput("colors") 在UI 中调用。哪个不起作用。我可能要问一个关于如何动态传递它的新问题。
    • 看最后一个例子
    • @PorkChop:如何更改选择框本身的背景颜色?谢谢
    猜你喜欢
    • 2019-02-12
    • 2020-08-03
    • 1970-01-01
    • 2022-12-04
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    相关资源
    最近更新 更多