【发布时间】:2021-04-13 11:29:58
【问题描述】:
对于我的可反应表,我想创建一个比较两列的函数,即今年的一列和去年的一列。我希望该函数根据它是否高于或低于去年的值来为当前年份列中的值着色。因此,如果值较高,它应该变成绿色,等等。
我的函数大部分都可以工作,但我无法让函数访问函数中值的索引号。
这是我目前所拥有的:
library(reactable)
reactable(test,
highlight = TRUE,
defaultPageSize = 20,
columns = list(
Maand = colDef(footer = "Totaal"),
year2020 = colDef(style = function(value) {
if (value > test[....index of value here...., "year2019"] ) {
color <- "#008000"
} else if (value >1) {
color <- "#e00000"
} else {
color <- "#777"
}
list(color = color, fontWeight = "bold")
})
),
defaultColDef = colDef(footer = function(values) {
if (!is.numeric(values)) return()
sum(values)
},
footerStyle = list(fontWeight = "bold")),
rowClass = "my-row"
)
当我使用这样的随机索引号时它确实有效:
reactable(test,
highlight = TRUE,
defaultPageSize = 20,
columns = list(
Maand = colDef(footer = "Totaal"),
year2020 = colDef(style = function(value) {
if (value > test[1, "year2019"] ) {
color <- "#008000"
} else if (value >1) {
color <- "#e00000"
} else {
color <- "#777"
}
list(color = color, fontWeight = "bold")
})
),
defaultColDef = colDef(footer = function(values) {
if (!is.numeric(values)) return()
sum(values)
},
footerStyle = list(fontWeight = "bold")),
rowClass = "my-row"
)
谁能告诉我我做错了什么?提前致谢!
【问题讨论】: