【问题标题】:Reactable conditional formatting one column text color based on another columns基于另一列的可反应条件格式化一列文本颜色
【发布时间】:2021-06-21 18:26:15
【问题描述】:

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

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


    【解决方案1】:

    这可以这样实现:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      相关资源
      最近更新 更多