0

对于我的可反应表,我想创建一个比较两列的函数,即今年一列和去年一列。我希望函数根据它是否高于或低于去年的值来为当前年份列中的值着色。因此,如果值更高,它应该变成绿色,等等。

我的函数大部分工作,但我无法让函数访问函数中值的索引号。

这是我到目前为止所拥有的:

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

谁能告诉我我做错了什么?提前致谢!

4

1 回答 1

0

我知道这已经很晚了,但我正在寻找与您类似的问题的解决方案。您需要提供一个索引来“循环”该行。您之前所做的“有效”,因为您将每个值与 year2019 列中的第一行进行了比较。

reactable(test, 
          highlight = TRUE,
          defaultPageSize = 20,
          columns = list(
            Maand = colDef(footer = "Totaal"),
            year2020 = colDef(style = function(value, index) {
              if (value > test[index, "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"
          )

于 2021-07-16T15:30:12.723 回答