我试图为这些值做一些有条件的着色。问题是,我已经在StackOverflow和可达wiki上读到了一些帖子,但是它们都没有工作!
reactable(prueba,
defaultColDef = colDef(
header = function(value) gsub(".", " ", value, fixed = TRUE),
cell = function(value) format(value, nsmall = 1),
align = "center",
minWidth = 150,
headerStyle = list(background = "#f7f7f8")
),
bordered = TRUE,
highlight = TRUE,
defaultSortOrder = "desc",
filterable = TRUE, minRows = 20,
groupBy = "linea",
columns = list(
Inventory = colDef(aggregate = "sum"),
OCC = colDef(aggregate = "mean"),
Tickets.Vendidos = colDef(aggregate = "sum"),
Revenue = colDef(aggregate = "sum"),
RASK = colDef(aggregate = "mean"),
CASK = colDef(aggregate = "mean"),
Rating = colDef(aggregate = 'mean'),
CpS = colDef(aggregate = 'mean'),
Red.discount = colDef(aggregate = 'sum'),
PC1 = colDef(aggregate = 'mean'),
PC1_margin = colDef(aggregate = 'mean'),
ASP = colDef(aggregate = 'mean')
)
)这是我的密码!我希望在OCC列中添加条件。
我希望它是:
红色如果0 <= OCC < 0.25橙色如果0.25 <= OCC < 0.5黄色如果0.5 <= OCC < 0.75绿色如果0.75 <= OCC <= 1
我尝试在我的OCC专栏中使用这个:
style = function(value) {
if (value > 0) {
color <- "#008000"
} else if (value < 0) {
color <- "#e00000"
} else {
color <- "#777"
}
list(color = color, fontWeight = "bold")
}但没有起作用
请帮帮我!
发布于 2022-10-28 06:46:53
我想你不是还在想这件事,但为了防止其他人在这里绊倒。嵌套的ifelse并不优雅,但无论如何,它可以用来设置字体颜色。
styler <- function(value){
color <- ifelse(value > 0, "#008000", ifelse(value < 0, "#e00000", ifelse(is.na(value), "#777", NA)))
list(color = color, fontWeight = "bold")
}定义表外的函数,您可以这样调用它。(函数中有两个位置,value和params$my_comparator,所以我可以在整个过程中使用它们)。
colDef(aggregate = "mean",
style = function(value){
styler(value)
})https://stackoverflow.com/questions/71161070
复制相似问题