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

如何在Rmarkdown表格的表头下方添加分隔线?

在RMarkdown的kableExtra表格表头下方添加分隔线

你可以通过修改row_spec()函数对表头行(第0行)的设置,直接添加底部边框来实现需求。

原代码

library(knitr)
library(kableExtra)

x <- cbind(c("Term", "Intercept"),
           c( "Estimate", round(2.684698447, digits = 3)), 
           c("P-value", "$< 2 \\times 10^{-16}$"),
           c("Significance", "***"))


knitr::kable(x, align="c", escape = F, caption = "Summary of the fitted linear model with miles per gallon as the response variable. Significance codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1",
      booktabs = T) %>%
  kable_styling("hover", full_width = F, font_size = 11, latex_options = "hold_position"
                )%>%
  row_spec(0, bold = T, font_size = 12) %>%
  column_spec(1, border_right = TRUE)  %>%
  row_spec(3, hline_after = TRUE)

修改后的代码

只需要在表头行的row_spec()中添加border_bottom = TRUE参数,就能在表头下方生成一条分隔线:

library(knitr)
library(kableExtra)

x <- cbind(c("Term", "Intercept"),
           c( "Estimate", round(2.684698447, digits = 3)), 
           c("P-value", "$< 2 \\times 10^{-16}$"),
           c("Significance", "***"))


knitr::kable(x, align="c", escape = F, caption = "Summary of the fitted linear model with miles per gallon as the response variable. Significance codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1",
      booktabs = T) %>%
  kable_styling("hover", full_width = F, font_size = 11, latex_options = "hold_position"
                )%>%
  row_spec(0, bold = T, font_size = 12, border_bottom = TRUE) %>% # 添加表头底部边框
  column_spec(1, border_right = TRUE)  %>%
  row_spec(3, hline_after = TRUE)

补充说明

如果你想要适配booktabs风格的细线,可以改用hline_after = TRUE参数替代border_bottom = TRUE,生成的线条会符合booktabs的排版规范:

row_spec(0, bold = T, font_size = 12, hline_after = TRUE)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 11:03:15