在 reactable 中,您可以使用返回自定义内容的 R 或 JavaScript 函数自定义数据呈现。
使用R 渲染函数的可能解决方案formatC()
。
一个简单的可重现示例(仅供记录):
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")
)
)
输出: