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

J.S*_*ree 5 formatting r shiny dt htmlwidgets

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


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

akr*_*run 9

如果列名称文本为“白色”且背景为“红色”

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'});",
        "}")
    ))
  })
  
  
}
Run Code Online (Sandbox Code Playgroud)

-输出

在此输入图像描述