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

如何隐藏flextable生成表格的原始表头行?

隐藏flextable中的原始表头行

你当前的代码通过as_flextable(d)生成表格时,默认会把数据框的原始列名作为第一行表头,再通过set_header_labels添加自定义表头行,导致出现双表头。要隐藏原始的第一行表头,有两种简单方法:

方法1:删除已生成的原始表头行

在现有代码基础上,使用delete_part()函数移除表头的第一行:

library(flextable)
library(stringr)

d <- structure(list(ISO3 = c("AFG", "AGO", "ALB", "AND", "ARE", "ARG", "ARM", "AUS", "AUT", "AZE"), 
                    Name = c("Afghanistan", "Angola", "Albania", "Andorra", "United Arab Emirates", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan"), 
                    `annual_historical_1991-2010` = c(96, 84, 96, 98, 77, 93, 97, 88, 98, 94), 
                    `annual_ssp126_2041-2060` = c(94, 80, 94, 97, 71, 92, 96, 85, 97, 92), 
                    `annual_ssp126_2081-2100` = c(94, 80, 94, 97, 72, 92, 96, 85, 97, 91), 
                    `annual_ssp585_2041-2060` = c(93, 78, 93, 96, 69, 91, 95, 83, 97, 91), 
                    `annual_ssp585_2081-2100` = c(89, 69, 88, 93, 57, 87, 91, 77, 94, 85), 
                    `hot90_historical_1991-2010` = c(89, 76, 88, 94, 58, 87, 91, 76, 95, 83), 
                    `hot90_ssp126_2041-2060` = c(85, 71, 82, 90, 50, 84, 86, 72, 92, 77), 
                    `hot90_ssp126_2081-2100` = c(85, 71, 82, 90, 50, 84, 86, 72, 92, 76), 
                    `hot90_ssp585_2041-2060` = c(83, 68, 79, 88, 46, 82, 84, 70, 90, 74), 
                    `hot90_ssp585_2081-2100` = c(73, 56, 67, 79, 31, 75, 73, 59, 83, 60), 
                    `season_historical_1991-2010` = c(96, 84, 95, 98, 76, 93, 97, 92, 98, 93), 
                    `season_ssp126_2041-2060` = c(94, 81, 92, 96, 71, 91, 95, 90, 97, 91), 
                    `season_ssp126_2081-2100` = c(94, 80, 92, 96, 72, 91, 95, 90, 97, 90), 
                    `season_ssp585_2041-2060` = c(93, 78, 91, 95, 69, 90, 94, 89, 96, 89), 
                    `season_ssp585_2081-2100` = c(88, 69, 85, 91, 58, 86, 90, 84, 93, 84)), 
               row.names = c(2L, 3L, 5L, 6L, 7L, 8L, 9L, 14L, 15L, 16L), class = "data.frame")

col_labels <- str_replace(names(d), "_ssp126_", " SSP1-2.6, ") |> 
  str_replace("_ssp585_", " SSP5-8.5, ") |>
  str_replace("_historical_", " Historical, ") |> 
  str_replace("^annual", "") |> 
  str_replace("^hot90", "") |> 
  str_replace("^season", "")

names_list <- as.list(setNames(col_labels, names(d)))

t_flex <- as_flextable(d) |>
  set_header_labels(values = names_list) |>
  delete_part(part = "header", i = 1)  # 删除第一行原始表头

t_flex

方法2:直接创建单表头表格

跳过默认生成的原始表头,用flextable()函数直接构建表格并设置自定义表头,从根源避免双表头:

t_flex <- flextable(d, col_keys = names(d)) |>
  set_header_labels(values = names_list)

t_flex

补充说明

  • delete_part(part = "header", i = 1)中的i=1指定删除表头区域的第一行(原始列名所在行);
  • 方法2更高效,无需先生成再删除,直接一步到位设置单表头;
  • 修正了原代码中SP5-8.5的拼写错误,统一为SSP5-8.5,保持与SSP1-2.6的命名规范一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 15:10:04