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

gtsummary中主题元素tbl_summary-arg:type失效,直接传type参数却正常?

gtsummary主题元素tbl_summary-arg:type不生效,但直接使用type参数正常

问题重现代码与结果

  1. 重置主题并生成默认表格
library(gtsummary)
packageVersion("gtsummary")
#> [1] '2.1.0.9010'

# 先重置主题为默认设置
reset_gtsummary_theme()
tbl_summary(trial) |> 
  # 转换为flextable对象,因为gtsummary对象在Stack Overflow上无法正常渲染
  as_flex_table()

默认表格

  1. 应用自定义主题后,设置未生效
# 应用自定义主题
set_gtsummary_theme(list("tbl_summary-arg:type" =
                           list(all_dichotomous() ~ "categorical")))
# 检查是否生效
trial |>
  tbl_summary() |> 
  as_flex_table()

主题设置后未生效的表格

未生效,Patient Died仍以二分类形式展示

  1. 直接使用type参数,运行正常
# 测试type参数
reset_gtsummary_theme()
trial |>
  tbl_summary(type = all_dichotomous() ~ "categorical") |> 
  as_flex_table()

直接使用type参数生效的表格

运行正常


问题原因与解决方案

原因

gtsummary中以-arg:结尾的主题参数,要求传入的是quosure(延迟求值表达式),而非直接的列表对象。你之前的主题设置直接传递了列表,导致函数无法正确解析这个参数。

正确的主题设置方式

需要用rlang::quo()包裹type的赋值内容,代码如下:

library(gtsummary)
library(rlang)

# 重置主题后设置正确的主题
reset_gtsummary_theme()
set_gtsummary_theme(
  list(
    "tbl_summary-arg:type" = quo(list(all_dichotomous() ~ "categorical"))
  )
)

# 验证设置生效
trial |>
  tbl_summary() |> 
  as_flex_table()

原理说明

当直接在tbl_summary()中使用type参数时,函数内部会自动处理参数的求值逻辑;但通过主题设置参数时,需要将表达式包裹为quosure,让gtsummary在调用tbl_summary()时能够正确延迟求值并应用这个设置。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 11:27:25