Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change color of one column in R reactable?

I have created a reactable "MasterIndexTable" where the final column "Rank" in the table is a value 1 through 435. I have defined a color palette so the cell with 1 gets a red color and 435 gets a blue color. However, since the background colors from that palette are dark I want to change the font of those cell values to white.

I’m not sure how to edit an individual column’s font color? I’m assuming it happens within the [Rank – colDef] component?

RtoDColorPalette <- function(x) rgb(colorRamp(c("red","blue"))(x),maxColorValue = 255)

MasterIndexTable <- reactable(
  HousePVI,
  columns = list(
    ID_No = colDef(show = FALSE),
    State = colDef(defaultSortOrder = "asc", width = 60),
    District_Number = colDef(name = "District", defaultSortOrder = "asc", width = 70),
    First_Name = colDef(name = "First Name"),
    Last_Name = colDef(name = "Last Name"),
    Member_Party = colDef(name = "Party", width = 60),
    PVI_Lean = colDef(name = "PVI Lean", width = 60),
    PVI_Value = colDef(name = "PVI Value", width = 60),
    Rank = colDef(name = "Rank (Most Republican (1) to Most Democratic (435)", width = 230,  
              style = function(value) {
                normalized <- (value - min(HousePVI$Rank))/(max(HousePVI$Rank) min(HousePVI$Rank))
                color <- RtoDColorPalette(normalized)
                list(background = color)
                }
              )
    ),
  theme = reactableTheme(
    color = 'black',
    backgroundColor = 'white',
    borderColor = 'black',
    stripedColor = 'aliceblue' )
  ) 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Inside your style function add color="white" to the returned list.

Using some fake data:

library(reactable)

set.seed(123)

RtoDColorPalette <- function(x) rgb(colorRamp(c("red", "blue"))(x), maxColorValue = 255)

HousePVI <- data.frame(
  ID_No = 1:10,
  State = LETTERS[1:10],
  Rank = sample(1:10)
)

reactable(
  HousePVI,
  columns = list(
    ID_No = colDef(show = FALSE),
    Rank = colDef(
      name = "Rank (Most Republican (1) to Most Democratic (435)", width = 230,
      style = function(value) {
        normalized <- (value - min(HousePVI$Rank)) / (max(HousePVI$Rank) - min(HousePVI$Rank))
        color <- RtoDColorPalette(normalized)
        list(background = color, color = "white")
      }
    )
  ),
  theme = reactableTheme(
    color = "black",
    backgroundColor = "white",
    borderColor = "black",
    stripedColor = "aliceblue"
  )
)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading