5

I have a datatable that I want to show in R Shiny, but I want the header column that has the column names to be red and the text to be in white. With formatStyles(), I can only specify entire columns instead of just the row of header names. How would you recommend to tackle this?


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)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
J.Sabree
  • 2,280
  • 19
  • 48
  • Your post says `column names to be red and the text to be in white.`. What is the background color you want – akrun Jun 10 '21 at 22:55

1 Answers1

8

If the column names text is 'white' and background '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'});",
        "}")
    ))
  })
  
  
}

-output

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662