如何在 R Shiny 中更改 DT Datable header 的背景和文本颜色

how to change background and text color of DT Datable header in R Shiny

我有一个要在 R Shiny 中显示的数据表,但我希望 header 列的列名称为红色,文本为白色。使用 formatStyles(),我只能指定整个列,而不仅仅是 header 行的名称。您建议如何解决这个问题?


library(shiny)
library(dplyr)


ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
        ),
    mainPanel(
        DT::DTOutput("table")
    )
))

server <- function(input, output) {
    data <- tibble(name = c("Justin", "Corey", "Sibley"),
           grade = c(50, 100, 100))
    
    output$table <- renderDT({
        datatable(data)
    })

    
}

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

如果列名称文本为 'white' 且背景为 'red'

server <- function(input, output) {
  
  data <- tibble(name = c("Justin", "Corey", "Sibley"),
                 grade = c(50, 100, 100))
  
  output$table <- DT::renderDT({
    datatable(data, options = list(
      
      initComplete = JS(
        "function(settings, json) {",
        "$(this.api().table().header()).css({'background-color': 'red', 'color': 'white'});",
        "}")
    ))
  })
  
  
}

-输出