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

gtsummary函数参数换行失效问题求助

gtsummary换行失效问题排查与解决

这是gtsummary包更新导致的兼容性问题,并非你的设备异常。

问题原因

gtsummary在v1.7.0及后续版本中调整了文本解析逻辑:

  • 旧版本中\n可直接在markdown模式下触发换行
  • 新版本中,markdown模式下\n不再被解析为换行,需改用HTML换行标签<br>,或遵循markdown原生换行规则(文本末尾加两个空格后再换行)

修复后的代码示例

方案1:使用HTML换行标签(推荐,兼容性更稳定)

## loading packages ##
library(dplyr)
library(gtsummary)

## gtsummary table ##
trial %>% tbl_summary(include = c("trt","stage","grade"),
                      by = "trt",
                      statistic = all_categorical() ~ "{p}% <br> ({n})",
                      missing = "no") %>%
  modify_header(update = list(all_stat_cols() ~ "**{level}** <br> ({p}%, <br> N = {n})"),
                text_interpret = "html")

方案2:遵循markdown原生换行规则

## loading packages ##
library(dplyr)
library(gtsummary)

## gtsummary table ##
trial %>% tbl_summary(include = c("trt","stage","grade"),
                      by = "trt",
                      statistic = all_categorical() ~ "{p}%  \n ({n})", # 注意%后有两个空格
                      missing = "no") %>%
  modify_header(update = list(all_stat_cols() ~ "**{level}**  \n ({p}%,  \n N = {n})"), # 同样添加两个空格
                text_interpret = "md")

补充说明

该变化是官方为统一文本解析逻辑做出的调整,目前最新版gtsummary文档已更新了换行的正确实现方式,你可以根据输出场景选择对应方案。

内容的提问来源于stack exchange,提问作者Clément Mertens

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 05:10:27