更改 Shiny 中 pickerInput 项目的颜色

Lis*_*els 5 css r shiny

在此示例之后,有人可以告诉我是否可能以及如何更改包中pickerInputUI下拉菜单中项目的字体颜色shinyWidgets

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

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){}
)
Run Code Online (Sandbox Code Playgroud)
> 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
Run Code Online (Sandbox Code Playgroud)

Por*_*hop 7

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

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

要更改背景,只需应用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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述