【问题标题】:Using index of value in function in R在R中使用函数中的值索引
【发布时间】: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"
          )

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

【问题讨论】:

    标签: r function reactable


    【解决方案1】:

    我知道这已经很晚了,但我正在寻找与您类似的问题的解决方案。您需要提供一个索引来“循环”该行。您之前所做的“有效”,因为您将每个值与 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-05-22
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 2015-05-03
      • 2017-01-02
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多