-
Notifications
You must be signed in to change notification settings - Fork 51
Closed
Description
I was trying to implement the nested = FALSE
logic to make the rbind
work without having to subset (as discussed yesterday) but the "Overall" row disappears. So I tried to make a simplified version that uses only rtables
functions and here it is (my apologies for the mean function being a bit weird):
library(rtables)
lyt <- basic_table() %>%
split_rows_by(
var = "ARM",
label_pos = "topleft",
split_label = "Treatment Group",
split_fun = drop_split_levels,
section_div = ""
) %>%
add_rowcounts() %>%
split_rows_by(
var = "STRATA1",
label_pos = "topleft",
split_label = "Visit"
)
# I know this is ugly but I manage like this
my_mean <- function(x, ...) list("a" = mean(x, ...))
afun_list <- list("mean 1" = make_afun(my_mean, .stats = "a", .labels = "Overall"),
"mean 2" = make_afun(my_mean, .stats = "a", .labels = "Overall"))
lyt_std <- lyt %>%
split_cols_by_multivar(
vars = c("BMRKR1", "AGE"),
varlabels = c("mean 1", "mean 2")
) %>%
analyze_colvars(list(mean, mean), format = "xx.x")
lyt_overall <- basic_table() %>%
split_cols_by_multivar(
vars = c("BMRKR1", "AGE"),
varlabels = c("mean 1", "mean 2")
) %>%
analyze_colvars(afun_list,
format = "xx.x",
nested = TRUE,
extra_args = list(".labels" = "Overall")
)
result_tbl <- build_table(lyt_std, DM)
result_overall <- build_table(lyt_overall, DM)
result_overall # This produces the right row (1)
col_info(result_tbl) <- col_info(result_overall)
result <- rbind(result_tbl, result_overall)
result # row (1) disappears
table_structure(result)
Ideally, we would like to get rid of the rbind
entirely and at the moment we managed only like this:
tbl <- basic_table() %>%
split_rows_by(
var = "ARM",
label_pos = "topleft",
split_label = "Treatment Group",
split_fun = add_overall_level(
trim = TRUE, first = FALSE,
valname = "ARM_ALL", label = "Overall"
),
section_div = ""
) %>%
add_rowcounts() %>%
split_rows_by(
var = "STRATA1",
label_pos = "topleft",
split_label = "Strata 1",
split_fun = add_overall_level(
trim = TRUE, first = FALSE,
valname = "strata_ALL", label = "Overall"
)
) %>%
split_cols_by_multivar(
vars = c("BMRKR1", "AGE"),
varlabels = c("mean 1", "mean 2")
) %>%
analyze_colvars(list(mean, mean), format = "xx.x") %>%
build_table(DM)
tbl[ -c(c(8, 9), c(16, 17), c(26, 27), 29:35), ] # horrible but we get the right table