gtsummmary::modify_header() 与整体列
gtsummmary::modify_header() with overall column
我想修改 {gtsummary} table 中的列 header 格式,使用分类变量(两个水平)和总体列,如 中。我无法找出正确的 gtsummary 变量名来访问各种列名。我目前有一个解决方法,我首先修改分类级别 headers,然后添加整体并修改它。但是,我想知道是否有更好的方法在单个 modify_header
行中执行此操作。 Reprex 在下面进行了各种尝试。
library(gtsummary)
library(dplyr)
# Shorten trial for examples
trial <- select(trial, trt, age)
# Modify headers
trial %>%
tbl_summary(by = trt) %>%
modify_header(update = all_stat_cols() ~ "**{level}**<br>N = {N}") %>%
as_kable()
Characteristic
Drug A
N = 200
Drug B
N = 200
Age
46 (37, 59)
48 (39, 56)
Unknown
7
4
# Adding overall before modifying stats columns doesn't work with {level} or {label}
try(
trial %>%
tbl_summary(by = trt) %>%
add_overall() %>%
modify_header(update = all_stat_cols() ~ "**{level}**<br>N = {N}") %>%
as_kable()
)
#> Error in eval(parse(text = text, keep.source = FALSE), envir) :
#> object 'level' not found
try(
trial %>%
tbl_summary(by = trt) %>%
add_overall() %>%
modify_header(update = all_stat_cols() ~ "**{label}**<br>N = {N}") %>%
as_kable()
)
#> Error in eval(parse(text = text, keep.source = FALSE), envir) :
#> object 'label' not found
# Adding overall before modifying stats columns does work with plain text
trial %>%
tbl_summary(by = trt) %>%
add_overall() %>%
modify_header(update = all_stat_cols() ~ "**THIS WORKS**<br>N = {N}") %>%
as_kable()
Characteristic
THIS WORKS
N = 200
THIS WORKS
N = 200
THIS WORKS
N = 200
Age
47 (38, 57)
46 (37, 59)
48 (39, 56)
Unknown
11
7
4
# And with {column} but then gives the gtsummary backend column name
trial %>%
tbl_summary(by = trt) %>%
add_overall() %>%
modify_header(update = all_stat_cols() ~ "**{column}**<br>N = {N}") %>%
as_kable()
Characteristic
stat_0
N = 200
stat_1
N = 200
stat_2
N = 200
Age
47 (38, 57)
46 (37, 59)
48 (39, 56)
Unknown
11
7
4
# Adding overall after modifying stats columns does work, but need to change label separately
trial %>%
tbl_summary(by = trt) %>%
modify_header(update = all_stat_cols() ~ "**{level}**<br>N = {N}") %>%
add_overall(col_label = "**Overall**<br>N = {N}") %>%
as_kable()
Characteristic
Overall
N = 200
Drug A
N = 200
Drug B
N = 200
Age
47 (38, 57)
46 (37, 59)
48 (39, 56)
Unknown
11
7
4
由 reprex package (v2.0.0)
于 2021-08-20 创建
您 运行 遇到的问题是 all_stat_cols()
默认情况下选择整个列和其他列。根据您是将标签分配给总体列还是拆分列,您需要使用略有不同的语法。
示例如下!
library(gtsummary)
tbl <-
trial %>%
select(trt, age) %>%
tbl_summary(by = trt, missing = "no") %>%
add_overall() %>%
modify_header(
update = list(all_stat_cols(FALSE) ~ "**{level}**<br>N = {n}",
stat_0 ~ "**Overall**<br>N = {N}"))
show_header_names(tbl)
#> i As a usage guide, the code below re-creates the current column headers.
#> modify_header(update = list(
#> label ~ "**Characteristic**",
#> stat_0 ~ "**Overall**<br>N = 200",
#> stat_1 ~ "**Drug A**<br>N = 98",
#> stat_2 ~ "**Drug B**<br>N = 102"
#> ))
#>
#>
#> Column Name Column Header
#> ------------ -----------------------
#> label **Characteristic**
#> stat_0 **Overall**<br>N = 200
#> stat_1 **Drug A**<br>N = 98
#> stat_2 **Drug B**<br>N = 102
由 reprex package (v2.0.1)
于 2021-08-20 创建
我想修改 {gtsummary} table 中的列 header 格式,使用分类变量(两个水平)和总体列,如 modify_header
行中执行此操作。 Reprex 在下面进行了各种尝试。
library(gtsummary)
library(dplyr)
# Shorten trial for examples
trial <- select(trial, trt, age)
# Modify headers
trial %>%
tbl_summary(by = trt) %>%
modify_header(update = all_stat_cols() ~ "**{level}**<br>N = {N}") %>%
as_kable()
Characteristic | Drug A N = 200 |
Drug B N = 200 |
---|---|---|
Age | 46 (37, 59) | 48 (39, 56) |
Unknown | 7 | 4 |
# Adding overall before modifying stats columns doesn't work with {level} or {label}
try(
trial %>%
tbl_summary(by = trt) %>%
add_overall() %>%
modify_header(update = all_stat_cols() ~ "**{level}**<br>N = {N}") %>%
as_kable()
)
#> Error in eval(parse(text = text, keep.source = FALSE), envir) :
#> object 'level' not found
try(
trial %>%
tbl_summary(by = trt) %>%
add_overall() %>%
modify_header(update = all_stat_cols() ~ "**{label}**<br>N = {N}") %>%
as_kable()
)
#> Error in eval(parse(text = text, keep.source = FALSE), envir) :
#> object 'label' not found
# Adding overall before modifying stats columns does work with plain text
trial %>%
tbl_summary(by = trt) %>%
add_overall() %>%
modify_header(update = all_stat_cols() ~ "**THIS WORKS**<br>N = {N}") %>%
as_kable()
Characteristic | THIS WORKS N = 200 |
THIS WORKS N = 200 |
THIS WORKS N = 200 |
---|---|---|---|
Age | 47 (38, 57) | 46 (37, 59) | 48 (39, 56) |
Unknown | 11 | 7 | 4 |
# And with {column} but then gives the gtsummary backend column name
trial %>%
tbl_summary(by = trt) %>%
add_overall() %>%
modify_header(update = all_stat_cols() ~ "**{column}**<br>N = {N}") %>%
as_kable()
Characteristic | stat_0 N = 200 |
stat_1 N = 200 |
stat_2 N = 200 |
---|---|---|---|
Age | 47 (38, 57) | 46 (37, 59) | 48 (39, 56) |
Unknown | 11 | 7 | 4 |
# Adding overall after modifying stats columns does work, but need to change label separately
trial %>%
tbl_summary(by = trt) %>%
modify_header(update = all_stat_cols() ~ "**{level}**<br>N = {N}") %>%
add_overall(col_label = "**Overall**<br>N = {N}") %>%
as_kable()
Characteristic | Overall N = 200 |
Drug A N = 200 |
Drug B N = 200 |
---|---|---|---|
Age | 47 (38, 57) | 46 (37, 59) | 48 (39, 56) |
Unknown | 11 | 7 | 4 |
由 reprex package (v2.0.0)
于 2021-08-20 创建您 运行 遇到的问题是 all_stat_cols()
默认情况下选择整个列和其他列。根据您是将标签分配给总体列还是拆分列,您需要使用略有不同的语法。
示例如下!
library(gtsummary)
tbl <-
trial %>%
select(trt, age) %>%
tbl_summary(by = trt, missing = "no") %>%
add_overall() %>%
modify_header(
update = list(all_stat_cols(FALSE) ~ "**{level}**<br>N = {n}",
stat_0 ~ "**Overall**<br>N = {N}"))
show_header_names(tbl)
#> i As a usage guide, the code below re-creates the current column headers.
#> modify_header(update = list(
#> label ~ "**Characteristic**",
#> stat_0 ~ "**Overall**<br>N = 200",
#> stat_1 ~ "**Drug A**<br>N = 98",
#> stat_2 ~ "**Drug B**<br>N = 102"
#> ))
#>
#>
#> Column Name Column Header
#> ------------ -----------------------
#> label **Characteristic**
#> stat_0 **Overall**<br>N = 200
#> stat_1 **Drug A**<br>N = 98
#> stat_2 **Drug B**<br>N = 102
由 reprex package (v2.0.1)
于 2021-08-20 创建