【问题标题】:How do I add separators in the footer (containing the total value of each column) of my reactable table in r?如何在 r 中的可反应表的页脚(包含每列的总值)中添加分隔符?
【发布时间】:2022-01-09 05:44:17
【问题描述】:

我已经使用 RStudio 中的 reactable 包构建了一个表。第一列包含文本,所有其他列包含货币值。我添加了一个页脚,其中包含除第一列之外的每一列的总数。对于表格的主体,我在数字中添加了分隔符(因为它们是大数字)使用

colDef(format = colFormat(prefix = "£", 分隔符 = TRUE, 数字 = 2)

但这不适用于页脚,我不知道如何以相同格式获取页脚中的数字。页脚是使用

sprintf("£%.2f", sum(values))

所以有正确的前缀和小数位,但没有分隔符。有谁知道如何做到这一点?谢谢!

【问题讨论】:

    标签: r reactable


    【解决方案1】:

    在 reactable 中,您可以使用 R 或 返回自定义内容的 JavaScript 函数。

    使用formatC()R render function 可能的解决方案。
    一个简单的可重现示例(仅供记录):

    df <- data.frame(
      TextColumn = c("A", "B", "C", "D", "E"),
      NumColumn1 = c(1000111.01, 1000222.02, 1000333.03, 1000444.04, 1000555.05),
      NumColumn2 = c(2000111, 2000222, 2000333, 2000444, 2000555)
    )
    
    reactable::reactable(
      data = df,
      columns = list(
        NumColumn1 = reactable::colDef(
          format = reactable::colFormat(
            suffix = " €",
            separators = TRUE,
            digits = 2
          ),
          footer = function(values, name) {
            htmltools::div(paste0(formatC(
              x = sum(values),
              digits = 2,
              big.mark = " ",
              format = "f"
            ), " €"))
          }
        ),
        NumColumn2 = reactable::colDef(
          format = reactable::colFormat(
            prefix = "£",
            separators = TRUE,
            digits = 2
          ),
          footer = function(values, name) {
            htmltools::div(paste0("£", formatC(
              x = sum(values),
              digits = 2,
              big.mark = " ",
              format = "f"
            )))
          }
        ),
        TextColumn = reactable::colDef(footer = "Total")
      )
    )
    

    输出:

    您可能感兴趣的解决方案:

    df <- data.frame(
      TextColumn = c("A", "B", "C", "D", "E"),
      NumColumn1 = c(1000111.01, 1000222.02, 1000333.03, 1000444.04, 1000555.05),
      NumColumn2 = c(2000111, 2000222, 2000333, 2000444, 2000555)
    )
    
    reactable::reactable(
      data = df,
      columns = list(
        NumColumn1 = reactable::colDef(
          format = reactable::colFormat(
            prefix = "£",
            separators = TRUE,
            digits = 2
          )),
        NumColumn2 = reactable::colDef(
          format = reactable::colFormat(
            prefix = "£",
            separators = TRUE,
            digits = 2
          )),
        TextColumn = reactable::colDef(
          footer = "Total"
        )
      ),
      defaultColDef = reactable::colDef(
        footer = function(values, name) {
          if (name %in% c("NumColumn1", "NumColumn2")) {
            htmltools::div(paste0("£", formatC(
              x = sum(values),
              digits = 2,
              big.mark = " ",
              format = "f"
            )))
          }
        },
        footerStyle = list(fontWeight = "bold")
        )
    )
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多