我希望列值根据使用可反应的另一列中的值而具有不同的颜色。这是我使用的代码,但它不会改变文本的颜色,所以我做错了一些事情,希望得到一些帮助。
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"
}
}
)))
这可以像这样实现:
注意:我使用简化版本的
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)
}
)
))