如何在R reactable中改变一列的颜色?

brc7rcf0  于 2023-07-31  发布在  React
关注(0)|答案(1)|浏览(129)

我已经创建了一个可React的“MasterIndexTable”,其中表中的最后一列“Rank”是1到435的值。我已经定义了一个调色板,所以单元格1得到红色,435得到蓝色。但是,由于该调色板的背景颜色是暗的,因此我想将这些单元格值的字体更改为白色。
我不知道如何编辑单个列的字体颜色?我假设它发生在[Rank - colDef]组件中?

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' )
  )

字符串

smtd7mpg

smtd7mpg1#

在样式函数中,将color="white"添加到返回的list中。
使用虚假数据:

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"
  )
)

字符串


的数据

相关问题