可反应的条件格式基于另一列的一列文本颜色

问题描述 投票:0回答:1

我希望列值根据使用可反应的另一列中的值而具有不同的颜色。这是我使用的代码,但它不会改变文本的颜色,所以我做错了一些事情,希望得到一些帮助。

library(reactable)

iris %>% 
  reactable(columns = list(
    Sepal.Length = colDef(
      style = function(index){
        if(iris$Species[index] == "setosa"){
          color <- "red"
        }else if(iris$Species[index] == "versicolor"){
          color <- "yellow"
        }else if(iris$Species[index] == "virginica"){
          color <- "green" 
        }
      }
    )))
r reactable
1个回答
4
投票

这可以像这样实现:

  1. 要传递行索引,请使样式函数成为双参数函数。
  2. 以列表形式返回样式信息。

注意:我使用简化版本的

iris

library(reactable)

iris1 <- iris[c(1:2, 51:52, 101:102), ]

reactable(iris1, columns = list(
  Sepal.Length = colDef(
    style = function(value, index) {
      if (iris1$Species[index] == "setosa") {
        color <- "red"
      } else if (iris1$Species[index] == "versicolor") {
        color <- "yellow"
      } else if (iris1$Species[index] == "virginica") {
        color <- "green"
      }
      list(color = color)
    }
  )
))

© www.soinside.com 2019 - 2024. All rights reserved.