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

如何修改gt表格中分组行样式(将分组标题设为粗体)

实现gt表格中特定分组标题的加粗效果

你需要针对newname列里的Consumer Non-Durables、Healthcare、Hitech等分组标题单独设置粗体,可通过gt包的tab_style配合行匹配实现,修改后的完整代码如下:

# Create a table using gt
table <- wide_summary_df %>%
  gt() %>%
  tab_header(
    title = "Summary Table",
    subtitle = "Total Acqprop by Year, Newname, and Decile"
  ) %>%
  cols_label(
    newname = "Newname",
    decile = "Deciles per sector"
  ) %>%
  fmt_percent(decimals = 1, columns = c("1985", "1990", "1995", "2001", "2008", "2020" )) %>%
  # 给decile列设置粗体(保留原有逻辑)
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(
      columns = decile
    )) %>%
  # 新增:给指定分组标题设置粗体
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(
      columns = newname,
      rows = newname %in% c("Consumer Non-Durables", "Healthcare", "Hitech")
    ))

# Print the table
table

关键说明:

  • 新增的tab_style块通过rows = newname %in% c(...)精准定位需要加粗的分组行
  • 若后续需要添加更多加粗的分组名称,直接在向量中补充即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 22:46:11