TERN (Rtables) 包格式化函数 这里有人熟悉 TERN 函数中的覆盖自动格式(例如 count_患者_with_flags)吗?
lyt_adae <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ACTARM") %>%
analyze_num_patients(
vars = "USUBJID",
.stats = c("unique", "nonunique"),
.labels = c(
unique = "Total number of patients with at least one AE",
nonunique = "Total number of AEs"
),
.formats = list(unique = format_count_fraction_fixed_dp, nonunique = "xx"),
show_labels = "hidden"
) %>%
count_patients_with_flags(
"USUBJID",
flag_variables = aesi_vars,
denom = "N_col",
var_labels = "Total number of patients with at least one",
show_labels = "visible"
)
result_adae <- build_table(lyt_adae, df = adae)
我正在尝试实现一件非常简单的事情:将变量计数和百分比更改为分数和百分比。所以从 10 (50%) 到 10/20 (50%)。
与上面的analyze_num_患者函数一样,我认为我可以使用添加了.formats的代码覆盖count_患者_with_flags函数中的默认函数:
count_patients_with_flags(
"USUBJID",
flag_variables = aesi_vars,
denom = "N_col",
var_labels = "Total number of patients with at least one",
show_labels = "visible",
.formats = c(count_fraction = format_count_fraction))
)
但是,format_count_fraction是一种仅适用于向量的格式,并且被扔进函数的对象是S4。另外,format_count_fraction
错误:
任何帮助将不胜感激。对于初学者来说,还有比 Hadley's Advanced 更好的学习 R 的 S3、S4 等 OOP 的书籍吗?他的基础数据科学书很棒,但是高级书很难理解。
谢谢!
我尝试使用 as.vector 函数手动将表格的每个单元格转换为向量,然后应用粗略的自定义格式函数来实现分数 + 百分比,但这太乏味了,我一直坚持将向量重新转换回 S4 兼容要输出到最终表格单元格的对象。
format_rcell(result_adae[2,1],custom_format)
您可以使用自定义格式函数覆盖格式。请注意,您需要再次从分数中检索总计数。
library(tern)
library(dplyr)
# Add labelled flag variables to analysis dataset.
adae <- tern_ex_adae %>%
mutate(
fl1 = TRUE %>% with_label("Total AEs"),
fl2 = (TRTEMFL == "Y") %>%
with_label("Total number of patients with at least one adverse event"),
fl3 = (TRTEMFL == "Y" & AEOUT == "FATAL") %>%
with_label("Total number of patients with fatal AEs"),
fl4 = (TRTEMFL == "Y" & AEOUT == "FATAL" & AEREL == "Y") %>%
with_label("Total number of patients with related fatal AEs")
)
my_format_func <- function(x, ...) {
attr(x, "label") <- NULL
if (any(is.na(x))) {
return("NA")
}
# browser()
checkmate::assert_vector(x)
checkmate::assert_int(x[1])
checkmate::assert_number(x[2], lower = 0, upper = 1) # the stat returns a fraction
result <- if (x[1] == 0) {
paste0(x[1])
} else {
dn <- round(x[1] / x[2]) # Retrieving the denominator
paste0(
x[1], "/", dn,
" (", round(x[2] * 100, 1), "%)"
)
}
return(result)
}
lyt_adae <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ACTARM") %>%
analyze_num_patients(
vars = "USUBJID",
.stats = c("unique", "nonunique"),
.labels = c(
unique = "Total number of patients with at least one AE",
nonunique = "Total number of AEs"
),
.formats = list(unique = format_count_fraction_fixed_dp, nonunique = "xx"),
show_labels = "hidden"
) %>%
count_patients_with_flags(
"USUBJID",
flag_variables = c("fl1", "fl2", "fl3", "fl4"),
denom = "N_col",
var_labels = "Total number of patients with at least one",
show_labels = "visible",
.formats = c(count_fraction = my_format_func)
)
result_adae <- build_table(lyt_adae, df = adae)
result_adae
#> A: Drug X B: Placebo C: Combination
#> (N=202) (N=177) (N=162)
#> —————————————————————————————————————————————————————————————————————————————————————————————————————————————
#> Total number of patients with at least one AE 59 (29.2%) 57 (32.2%) 48 (29.6%)
#> Total number of AEs 202 177 162
#> Total number of patients with at least one
#> Total AEs 59/202 (29.2%) 57/177 (32.2%) 48/162 (29.6%)
#> Total number of patients with at least one adverse event 59/202 (29.2%) 57/177 (32.2%) 48/162 (29.6%)
#> Total number of patients with fatal AEs 28/202 (13.9%) 31/177 (17.5%) 20/162 (12.3%)
#> Total number of patients with related fatal AEs 28/202 (13.9%) 31/177 (17.5%) 20/162 (12.3%)
创建于 2024-07-03,使用 reprex v2.1.0