Search code examples
rshinydatatablebackground-colordt

Utilizing background colors in R Shiny datatable to color cells


Hi I'm working with an R Shiny dashboard of mine using NBA data to showcase the top 15 highest scorers from the previous day's games. I have a simple table setup showing the points scored and the shooting efficiency, but it's pretty bland and I think it would be useful to use background color on certain cells to do things like highlight standout performances like someone getting their season high PTS, or a player having a high efficiency night relative to their average for the season.

Below I've pasted screenshots of what the table looks like & the final columns I want it to have, but I also posted a screenshot of the additional variables I've already calculated in the data frame that I want to color by. I don't want to include those extra variables in the actual table, but I want to use them to background color the cells, if that makes sense. The data frame is called top_15_yesterday and I posted the R Shiny output code as well.

text2

text

output$top_15 <- DT::renderDataTable(datatable(top_15_yesterday, rownames = FALSE,
                                                 options = list(searching = FALSE, pageLength = 15, 
                                                                lengthChange = FALSE, info = FALSE, paging = FALSE)) %>%
                                         formatCurrency(6, currency = "$", interval = 3, mark = ",", digits = 0) %>%
                                         formatPercentage(4, digits = 1))

For example, if a player's ppg_difference is greater than 20 then their PTS cell should be colored as green, any ts_difference above .20 should be colored as green, and if a player got his season high (PTS = season_high) then their PTS cell should be colored as purple. And on the other hand, if a player's ts_difference is say .10 below their average, color their TS% cell as red.

That's just a general idea of the kind of coloring scheme I want to use, but I have no idea how to implement that into datatable. The examples I've seen don't have the setup I have where the variables I want to color by are already in the data frame. If anyone was able to follow along and have any advice on how to implement these background colors I'd appreciate it !


Solution

  • In your options, make the columns you'd like to reference invisible. The first column is zero in JavaScript

    options = list(searching = FALSE, 
    pageLength = 15, 
    lengthChange = FALSE, 
    info = FALSE, 
    paging = FALSE
    columnDefs = list(list(targets = c(0, 7, 8, 9, 10, 11),  
                   visible = FALSE)))
    

    close datatable() and pipe onto

    formatStyle(columns = 'theColumn', 
                        valueColumns = 'theReferenceColumn',
                        backgroundColor = styleEqual(levels = c(a vector with the levels), 
                                                     values = c(a vector with the background colors))
    
    or styleInterval(cuts, values)