Search code examples
htmlcssrshinyrhandsontable

How to display specific column header in Rhandsontable in a particular color using css?


I would like change the header color of specific columns to a specific color using css. For example can someone suggest on how to change the color of the header 'mpg', 'hp' and 'gear' to red color, and change the color of 'disp', 'wt' and 'carb' to blue color in the following example table?

My question is almost similar to the question posted in this forum earlier.

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  rHandsontableOutput("table1"),
  tags$style(type="text/css", "#table1 th {font-weight:bold;}")
)

server=function(input, output, session) {

  output$table1 <- renderRHandsontable({
    rhandsontable(head(mtcars),rowHeaders=F)
  })
}

shinyApp(ui,server)

Solution

  • Lastly I had similar issue to make custom HTML header for rhandsontable and found this thread. The solution is really nice. But I also ran into a problem with using hot_col() after setting colHeaders = table_headers_html within rhandsontable().

    I was able to make following workaround; it just modifies the column headers directly in the object created with rhandsontable() - after setting desired options with hot_col().

    server=function(input, output, session) {
    
        output$table1 <- renderRHandsontable({
            rhandsontable_object <- rhandsontable(
                head(mtcars),
                rowHeaders=F
            ) %>%
              hot_col(col = mpg, allowInvalid = TRUE)
    
            rhandsontable_object$x$colHeaders <- table_headers_html
    
            rhandsontable_object
        })
    }