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

gtsummary 2.3.0移除列默认上标及脚注配置错误求助

问题解决与代码修正

错误原因分析

报错源于modify_footnote_body()的rows参数语法错误:直接罗列变量名的写法不符合R的逻辑判断规则,导致R将这些变量识别为多余参数。

需求适配与代码修正

以下是适配gtsummary 2.3.0版本的完整代码,同时满足你的两个需求:

## 加载必要依赖包
library(gtsummary)
library(dplyr)
library(gt)

## 创建描述性统计表格
descriptivestable <- KUWRowPowerDF |> 
  select(all_of(allvars)) |> 
  tbl_summary(
    type = all_of(c(normalvar, nonnormalvar)) ~ "continuous",
    statistic = list(
      all_of(normalvar) ~ "{mean} (± {sd})",
      all_of(nonnormalvar) ~ "{median} ({p25}, {p75})"
    ),
    digits = all_continuous() ~ 1,
    label = list(
      `Age` ~ "Age (years) †",
      `Height_cm` ~ "Height (cm)",
      `Weight_kg` ~ "Weight (kg)",
      `On-Water_Rowing_Experience` ~ "On-Water Rowing Experience (Years) †",
      `Indoor_Rowing_Experience` ~ "Indoor Rowing Experience (Years) †",
      `7-Stroke_W` ~ "7-Stroke Peak Power Test (W)",
      `7-Stroke_W/kg` ~ "7-Stroke Peak Power Test (W/kg)",
      `20-Second_W` ~ "20-Second All Out Test (W) †",
      `20-Second_W/kg` ~ "20-Second All Out Test (W/kg)",
      `60-Second_W` ~ "60-Second All Out Test (W)",
      `60-Second_W/kg` ~ "60-Second All Out Test (W/kg)",
      `2k_Avg_W` ~ "2,000-meter (W)",
      `2k_W/kg` ~ "2,000-meter (W/kg)",
      `2k_Avg_Speed_m/s` ~ "2,000-meter Speed (m/s) †",
      `2k_Stroke_length` ~ "2,000-meter Stroke Length (m)",
      `2k_Total_Time_sec` ~ "2,000-meter Time (s)"
    ),
    footnote = FALSE  # 关闭默认统计量脚注,从根源避免列标题上标
  ) |> 
  add_stat(fns = stat_fns) |>                
  modify_header(
    stat_0 ~ "**Mean ± SD**",
    add_stat_1 ~ "**Shapiro–Wilk p**",  
    label ~ "**Variable**"                  
  ) |> 
  bold_labels() |> 
  remove_footnote_header(columns = all_stat_cols()) |> 
  modify_footnote(everything() ~ NA) |>  # 清除所有默认脚注
  modify_footnote_body(
    footnote = "† Denotes non-normally distributed variable, reported as Median (p25, p75)",
    columns = "label",
    rows = variable %in% c("Age", "On-Water_Rowing_Experience", "Indoor_Rowing_Experience", 
                           "20-Second_W", "2k_Avg_Speed_m/s") & row_type == "label"
  ) |> 
  modify_table_body(~ .x |> 
                      dplyr::arrange(factor(variable, levels = allvars))
  ) |> 
  as_gt() |> 
  gt::tab_header(title = md("**Table 1 | Participant Characteristics**"),
                 subtitle = md("**N = 40; p ≤ 0.05**")
  )

descriptivestable

关键修改说明

  1. 移除列标题上标:

    • 在tbl_summary()中添加footnote = FALSE,直接关闭默认统计量脚注的生成逻辑,从根源避免列标题出现上标。
    • 保留remove_footnote_header()作为兜底,确保无残留表头脚注。
  2. 设置自定义脚注:

    • 用modify_footnote(everything() ~ NA)清除所有默认脚注,保证最终仅显示你需要的内容。
    • 将rows参数改为variable %in% c(...)的正确语法,批量匹配目标变量,避免语法错误。

内容的提问来源于stack exchange,提问作者Joseph M. DeLeo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:18:09