Skip to content

Commit

Permalink
Merge branch 'main' into 898_s_summary_na@main
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua committed Jun 6, 2023
2 parents 943821e + 4980e32 commit c9bb68a
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tern
Title: Create Common TLGs Used in Clinical Trials
Version: 0.8.2.9005
Version: 0.8.2.9006
Date: 2023-06-06
Authors@R: c(
person("Joe", "Zhu", , "[email protected]", role = c("aut", "cre")),
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# tern 0.8.2.9005
# tern 0.8.2.9006

### Enhancements
* Added explicit zero counts to `g_km` plot "at risk" annotation tables.
* Added a flag for total level split in `analyze_patients_exposure_in_cols`.
* Implemented `.indent_mods` argument in functions `h_tab_one_biomarker`, `h_tab_rsp_one_biomarker`, `h_tab_surv_one_biomarker`, `summarize_logistic`, `logistic_summary_by_flag`, `tabulate_rsp_biomarkers`, a_coxreg, `summarize_coxreg`, `tabulate_survival_biomarkers`, `surv_time`, `surv_timepoint`, and `cfun_by_flag`.

### Miscellaneous
Expand Down
2 changes: 2 additions & 0 deletions R/argument_convention.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#' by a statistics function.
#' @param .spl_context (`data.frame`)\cr gives information about ancestor split states
#' that is passed by `rtables`.
#' @param add_total_level (`flag`)\cr adds a "total" level after the others which includes all the levels
#' that constitute the split. A custom label can be set for this level via the `custom_label` argument.
#' @param col_by (`factor`)\cr defining column groups.
#' @param conf_level (`proportion`)\cr confidence level of the interval.
#' @param data (`data.frame`)\cr the dataset containing the variables to summarize.
Expand Down
42 changes: 40 additions & 2 deletions R/summarize_patients_exposure_in_cols.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ a_count_patients_sum_exposure <- function(df,
ex_var = "AVAL",
id = "USUBJID",
labelstr = "",
add_total_level = FALSE,
.N_col, # nolint
.stats,
.formats = list(n_patients = "xx (xx.x%)", sum_exposure = "xx"),
custom_label = NULL) {
checkmate::assert_flag(add_total_level)

if (!is.null(var)) {
assert_df_with_variables(df, list(var = var))
df[[var]] <- as.factor(df[[var]])
Expand Down Expand Up @@ -144,6 +147,17 @@ a_count_patients_sum_exposure <- function(df,
custom_label = lvl
)[[.stats]]
}
if (add_total_level) {
y[[.stats]][["Total"]] <- s_count_patients_sum_exposure(
df = df,
ex_var = ex_var,
id = id,
labelstr = labelstr,
.N_col = .N_col,
.stats = .stats,
custom_label = custom_label
)[[.stats]]
}
}

in_rows(.list = y[[.stats]], .formats = .formats[[.stats]])
Expand Down Expand Up @@ -234,14 +248,31 @@ summarize_patients_exposure_in_cols <- function(lyt, # nolint
#' result5 <- build_table(lyt5, df = df, alt_counts_df = adsl)
#' result5
#'
#' # Adding total levels and custom label
#' lyt <- basic_table(
#' show_colcounts = TRUE
#' ) %>%
#' analyze_patients_exposure_in_cols(
#' var = "ARMCD",
#' col_split = TRUE,
#' add_total_level = TRUE,
#' custom_label = "TOTAL"
#' ) %>%
#' append_topleft(c("", "Sex"))
#'
#' tbl <- build_table(lyt, df = df, alt_counts_df = adsl)
#' tbl
#'
#' @export
analyze_patients_exposure_in_cols <- function(lyt, # nolint
var = NULL,
ex_var = "AVAL",
col_split = TRUE,
add_total_level = FALSE,
.stats = c("n_patients", "sum_exposure"),
.labels = c(n_patients = "Patients", sum_exposure = "Person time"),
.indent_mods = 0L) {
.indent_mods = 0L,
...) {
if (col_split) {
lyt <- split_cols_by_multivar(
lyt = lyt,
Expand All @@ -253,7 +284,14 @@ analyze_patients_exposure_in_cols <- function(lyt, # nolint
lyt <- lyt %>% analyze_colvars(
afun = a_count_patients_sum_exposure,
indent_mod = .indent_mods,
extra_args = list(var = var, ex_var = ex_var)
extra_args = c(
list(
var = var,
ex_var = ex_var,
add_total_level = add_total_level
),
...
)
)
lyt
}
3 changes: 3 additions & 0 deletions man/argument_convention.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion man/summarize_patients_exposure_in_cols.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/_snaps/summarize_patients_exposure_in_cols.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,21 @@
————————————————————————————————————————————————
Total patients numbers/person time 12 (100.0%)

# patients_exposure_in_cols works with totals after the row split

Code
res
Output
Extent of Exposure
——————————————————————————————————————
Patients Person time
Sex (N=12) (N=12)
——————————————————————————————————————
Female 6 (50.0%) 16
Male 6 (50.0%) 19
REAL TOTAL 12 (100.0%) 35
——————————————————————————————————————
* Patient Time is the sum of patients and times

29 changes: 24 additions & 5 deletions tests/testthat/test-summarize_patients_exposure_in_cols.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ testthat::test_that("summarize_patients_exposure_in_cols works well with default
result <- basic_table() %>%
split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) %>%
summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE) %>%
split_rows_by("SEX") %>%
summarize_patients_exposure_in_cols(var = "AVAL", col_split = FALSE) %>%
analyze_patients_exposure_in_cols(var = "SEX", col_split = FALSE) %>%
build_table(df = df, alt_counts_df = adsl)

res <- testthat::expect_silent(result)
Expand All @@ -62,9 +61,8 @@ testthat::test_that("summarize_patients_exposure_in_cols works well with custom
custom_label = "xyz",
.stats = "sum_exposure"
) %>%
split_rows_by("SEX") %>%
summarize_patients_exposure_in_cols(
var = "AVAL",
analyze_patients_exposure_in_cols(
var = "SEX",
col_split = FALSE,
.stats = "sum_exposure"
) %>%
Expand Down Expand Up @@ -146,3 +144,24 @@ testthat::test_that(
testthat::expect_snapshot(res)
}
)

testthat::test_that("patients_exposure_in_cols works with totals after the row split", {
# Fixes adding total as last analyze level, issue #950
lyt <- basic_table(
title = "Extent of Exposure",
main_footer = "* Patient Time is the sum of patients and times",
show_colcounts = TRUE
) %>%
analyze_patients_exposure_in_cols(
var = "SEX",
col_split = TRUE,
add_total_level = TRUE,
custom_label = "REAL TOTAL"
) %>%
append_topleft(c("", "Sex"))

tbl <- build_table(lyt, anl_local)

res <- testthat::expect_silent(tbl)
testthat::expect_snapshot(res)
})

0 comments on commit c9bb68a

Please sign in to comment.