You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R语言formattable包中,如何将total行设置为加粗且字号为8?

在formattable中设置Total行的加粗与字号样式

你可以通过在formatter的style参数中加入条件判断,针对category列的total值单独设置加粗和字号样式。修改后的代码如下:

library(formattable)
df <- data.frame(category=c('a','b','total'),value=c(1,2,3))
formattable(df,list(
  category = formatter("span",
                       style = x ~ style(
                         color = "red",
                         font.weight = ifelse(x == "total", "bold", "normal"),
                         font.size = ifelse(x == "total", "8px", "inherit")
                       )
                       ),
  # 若需同步设置value列的total行样式,可添加以下配置
  value = formatter("span",
                    style = x ~ style(
                      font.weight = ifelse(df$category == "total", "bold", "normal"),
                      font.size = ifelse(df$category == "total", "8px", "inherit")
                    )
                    )
))

关键说明:

  • 借助ifelse()判断当前值是否为total,对目标行应用font.weight = "bold"实现加粗,font.size = "8px"设置字号;
  • 非total行可设置font.weight = "normal"(默认字重)和font.size = "inherit"(继承默认字号),也可根据需求调整为其他样式;
  • 若要让value列的total行同步样式,需单独为该列配置formatter,通过df$category匹配行条件。

内容的提问来源于stack exchange,提问作者anderwyang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 20:00:58