From c64ee787815ec21fda787700b9233da9aa2bff16 Mon Sep 17 00:00:00 2001 From: Emily de la Rua <59304861+edelarua@users.noreply.github.com> Date: Mon, 15 May 2023 04:43:40 -0400 Subject: [PATCH] Add `analyze_patients_exposure_in_cols` (#916) Closes #915 --------- Signed-off-by: Davide Garolini Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Davide Garolini Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com> --- NAMESPACE | 1 + NEWS.md | 1 + R/analyze_colvars_functions.R | 1 + R/summarize_patients_exposure_in_cols.R | 213 +++++++++++++----- man/analyze_colvars_functions.Rd | 1 + man/summarize_patients_exposure_in_cols.Rd | 103 +++++++-- .../summarize_patients_exposure_in_cols.md | 42 ++++ ...test-summarize_patients_exposure_in_cols.R | 66 +++++- 8 files changed, 348 insertions(+), 80 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index bf8555234c..6ea664749e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -42,6 +42,7 @@ export(a_summary) export(add_rowcounts) export(aesi_label) export(analyze_num_patients) +export(analyze_patients_exposure_in_cols) export(analyze_vars_in_cols) export(append_varlabels) export(as.rtable) diff --git a/NEWS.md b/NEWS.md index 5b343d1324..379037fcdb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,7 @@ * Corrected ordering of factor levels by `d_onco_rsp_label`. * Added new "Analyze Functions" and "Summarize Functions" overview pages. * Moved all KM plot documentation to the `g_km` function. +* Added `a_count_patients_sum_exposure` for new analyze function `analyze_patients_exposure_in_cols` and `summarize_patients_exposure_in_cols`. ### Miscellaneous * Implemented the `lubridate` package for date variables in `tern` datasets. diff --git a/R/analyze_colvars_functions.R b/R/analyze_colvars_functions.R index 0250b0e43b..73496c1113 100644 --- a/R/analyze_colvars_functions.R +++ b/R/analyze_colvars_functions.R @@ -5,6 +5,7 @@ #' These functions are wrappers of [rtables::analyze_colvars()] which apply corresponding `tern` statistics functions #' to add an analysis to a given table layout: #' +#' * [analyze_patients_exposure_in_cols()] #' * [analyze_vars_in_cols()] (extended wrapper, never used in other `tern` functions) #' * [summarize_colvars()] (low level wrapper, never used in other `tern` functions) #' * [summarize_coxreg()] (contains also [rtables::summarize_row_groups()]) diff --git a/R/summarize_patients_exposure_in_cols.R b/R/summarize_patients_exposure_in_cols.R index 3cc3307516..b68e448034 100644 --- a/R/summarize_patients_exposure_in_cols.R +++ b/R/summarize_patients_exposure_in_cols.R @@ -5,20 +5,22 @@ #' Counting the number of patients and summing analysis value (i.e exposure values) across all patients #' when a column table layout is required. #' +#' @inheritParams argument_convention +#' #' @name summarize_patients_exposure_in_cols NULL #' @describeIn summarize_patients_exposure_in_cols Statistics function which counts numbers #' of patients and the sum of exposure across all patients. #' -#' @inheritParams argument_convention +#' @param ex_var (`character`)\cr name of the variable within `df` containing exposure values. #' @param custom_label (`string` or `NULL`)\cr if provided and `labelstr` is empty then this will #' be used as label. #' #' @return #' * `s_count_patients_sum_exposure()` returns a named `list` with the statistics: #' * `n_patients`: Number of unique patients in `df`. -#' * `sum_exposure`: Sum of `.var` across all patients in `df`. +#' * `sum_exposure`: Sum of `ex_var` across all patients in `df`. #' #' @examples #' set.seed(1) @@ -39,6 +41,7 @@ NULL #' # Internal function - s_count_patients_sum_exposure #' \dontrun{ #' s_count_patients_sum_exposure(df = df, .N_col = nrow(adsl)) +#' s_count_patients_sum_exposure(df = df, .N_col = nrow(adsl), .stats = "n_patients") #' s_count_patients_sum_exposure( #' df = df, #' .N_col = nrow(adsl), @@ -47,17 +50,19 @@ NULL #' } #' #' @keywords internal -s_count_patients_sum_exposure <- function(df, # nolintr - .var = "AVAL", +s_count_patients_sum_exposure <- function(df, + ex_var = "AVAL", id = "USUBJID", labelstr = "", - .N_col, # nolintr + .stats = c("n_patients", "sum_exposure"), + .N_col, # nolint custom_label = NULL) { - assert_df_with_variables(df, list(.var = .var, id = id)) + assert_df_with_variables(df, list(ex_var = ex_var, id = id)) checkmate::assert_string(id) checkmate::assert_string(labelstr) checkmate::assert_string(custom_label, null.ok = TRUE) - checkmate::assert_numeric(df[[.var]]) + checkmate::assert_numeric(df[[ex_var]]) + checkmate::assert_true(all(.stats %in% c("n_patients", "sum_exposure"))) row_label <- if (labelstr != "") { labelstr @@ -69,65 +74,103 @@ s_count_patients_sum_exposure <- function(df, # nolintr y <- list() - y$n_patients <- - formatters::with_label( - s_num_patients_content( - df = df, - .N_col = .N_col, # nolintr - .var = id, - labelstr = "" - )$unique, - row_label - ) - - y$sum_exposure <- formatters::with_label(sum(df[[.var]]), row_label) + if ("n_patients" %in% .stats) { + y$n_patients <- + formatters::with_label( + s_num_patients_content( + df = df, + .N_col = .N_col, # nolint + .var = id, + labelstr = "" + )$unique, + row_label + ) + } + if ("sum_exposure" %in% .stats) { + y$sum_exposure <- formatters::with_label(sum(df[[ex_var]]), row_label) + } y } +#' @describeIn summarize_patients_exposure_in_cols Analysis function which is used as `afun` in +#' [rtables::analyze_colvars()] within `analyze_patients_exposure_in_cols()` and as `cfun` in +#' [rtables::summarize_row_groups()] within `summarize_patients_exposure_in_cols()`. +#' +#' @return +#' * `a_count_patients_sum_exposure()` returns formatted [rtables::CellValue()]. +#' +#' @examples +#' tern:::a_count_patients_sum_exposure( +#' df = df, +#' var = "SEX", +#' .N_col = nrow(df), +#' .stats = "n_patients" +#' ) +#' +#' @keywords internal +a_count_patients_sum_exposure <- function(df, + var = NULL, + ex_var = "AVAL", + id = "USUBJID", + labelstr = "", + .N_col, # nolint + .stats, + .formats = list(n_patients = "xx (xx.x%)", sum_exposure = "xx"), + custom_label = NULL) { + if (!is.null(var)) { + assert_df_with_variables(df, list(var = var)) + df[[var]] <- as.factor(df[[var]]) + } + + y <- list() + if (is.null(var)) { + y[[.stats]] <- list(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]]) + } else { + for (lvl in levels(df[[var]])) { + y[[.stats]][[lvl]] <- s_count_patients_sum_exposure( + df = subset(df, get(var) == lvl), + ex_var = ex_var, + id = id, + labelstr = labelstr, + .N_col = .N_col, + .stats = .stats, + custom_label = lvl + )[[.stats]] + } + } + + in_rows(.list = y[[.stats]], .formats = .formats[[.stats]]) +} + #' @describeIn summarize_patients_exposure_in_cols Layout-creating function which can take statistics #' function arguments and additional format arguments. This function is a wrapper for #' [rtables::split_cols_by_multivar()] and [rtables::summarize_row_groups()]. #' -#' @inheritParams argument_convention -#' @param col_split (`flag`)\cr whether the columns should be split. Set to `FALSE` when the required -#' column split has been done already earlier in the layout pipe. -#' #' @return #' * `summarize_patients_exposure_in_cols()` returns a layout object suitable for passing to further #' layouting functions, or to [rtables::build_table()]. Adding this function to an `rtable` layout will -#' add formatted rows, with the statistics from `s_count_patients_sum_exposure()` arranged in +#' add formatted content rows, with the statistics from `s_count_patients_sum_exposure()` arranged in #' columns, to the table layout. #' #' @examples #' lyt <- 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) +#' summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE) #' result <- build_table(lyt, df = df, alt_counts_df = adsl) #' result #' #' lyt2 <- basic_table() %>% -#' split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) %>% -#' summarize_patients_exposure_in_cols( -#' var = "AVAL", col_split = TRUE, -#' .stats = "n_patients", custom_label = "some custom label" -#' ) %>% -#' split_rows_by("SEX") %>% -#' summarize_patients_exposure_in_cols(var = "AVAL", col_split = FALSE) +#' summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE, .stats = "sum_exposure") #' result2 <- build_table(lyt2, df = df, alt_counts_df = adsl) #' result2 #' -#' lyt3 <- basic_table() %>% -#' summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE) -#' result3 <- build_table(lyt3, df = df, alt_counts_df = adsl) -#' result3 -#' -#' lyt4 <- basic_table() %>% -#' summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE, .stats = "sum_exposure") -#' result4 <- build_table(lyt4, df = df, alt_counts_df = adsl) -#' result4 -#' #' @export summarize_patients_exposure_in_cols <- function(lyt, # nolintr var, @@ -136,28 +179,82 @@ summarize_patients_exposure_in_cols <- function(lyt, # nolintr .labels = c(n_patients = "Patients", sum_exposure = "Person time"), .indent_mods = NULL, col_split = TRUE) { - afun_list <- Map( - function(stat) { - make_afun( - s_count_patients_sum_exposure, - .stats = stat, - .formats = ifelse(stat == "n_patients", "xx (xx.x%)", "xx") - ) - }, - stat = .stats - ) - if (col_split) { lyt <- split_cols_by_multivar( lyt = lyt, vars = rep(var, length(.stats)), - varlabels = .labels[.stats] + varlabels = .labels[.stats], + extra_args = list(.stats = .stats) ) } summarize_row_groups( lyt = lyt, var = var, - cfun = afun_list, + cfun = a_count_patients_sum_exposure, extra_args = list(...) ) } + +#' @describeIn summarize_patients_exposure_in_cols Layout-creating function which can take statistics +#' function arguments and additional format arguments. This function is a wrapper for +#' [rtables::split_cols_by_multivar()] and [rtables::analyze_colvars()]. +#' +#' @inheritParams argument_convention +#' @param col_split (`flag`)\cr whether the columns should be split. Set to `FALSE` when the required +#' column split has been done already earlier in the layout pipe. +#' +#' @return +#' * `analyze_patients_exposure_in_cols()` returns a layout object suitable for passing to further +#' layouting functions, or to [rtables::build_table()]. Adding this function to an `rtable` layout will +#' add formatted data rows, with the statistics from `s_count_patients_sum_exposure()` arranged in +#' columns, to the table layout. +#' +#' @note As opposed to [summarize_patients_exposure_in_cols()] which generates content rows, this function +#' generates data rows which will _not_ be repeated on multiple pages when pagination is used. +#' +#' @examples +#' lyt3 <- basic_table() %>% +#' split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) %>% +#' summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE) %>% +#' analyze_patients_exposure_in_cols(var = "SEX", col_split = FALSE) +#' result3 <- build_table(lyt3, df = df, alt_counts_df = adsl) +#' result3 +#' +#' lyt4 <- basic_table() %>% +#' split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) %>% +#' summarize_patients_exposure_in_cols( +#' var = "AVAL", col_split = TRUE, +#' .stats = "n_patients", custom_label = "some custom label" +#' ) %>% +#' analyze_patients_exposure_in_cols(var = "SEX", col_split = FALSE, ex_var = "AVAL") +#' result4 <- build_table(lyt4, df = df, alt_counts_df = adsl) +#' result4 +#' +#' lyt5 <- basic_table() %>% +#' analyze_patients_exposure_in_cols(var = "SEX", col_split = TRUE, ex_var = "AVAL") +#' result5 <- build_table(lyt5, df = df, alt_counts_df = adsl) +#' result5 +#' +#' @export +analyze_patients_exposure_in_cols <- function(lyt, # nolint + var = NULL, + ex_var = "AVAL", + col_split = TRUE, + .stats = c("n_patients", "sum_exposure"), + .labels = c(n_patients = "Patients", sum_exposure = "Person time"), + .indent_mods = 0L) { + if (col_split) { + lyt <- split_cols_by_multivar( + lyt = lyt, + vars = rep(ex_var, length(.stats)), + varlabels = .labels[.stats], + extra_args = list(.stats = .stats) + ) + } + lyt <- lyt %>% analyze_colvars( + afun = a_count_patients_sum_exposure, + indent_mod = .indent_mods, + extra_args = list(var = var, ex_var = ex_var) + ) + lyt +} diff --git a/man/analyze_colvars_functions.Rd b/man/analyze_colvars_functions.Rd index 6b211ecf0b..9ae0bd73b7 100644 --- a/man/analyze_colvars_functions.Rd +++ b/man/analyze_colvars_functions.Rd @@ -7,6 +7,7 @@ These functions are wrappers of \code{\link[rtables:analyze_colvars]{rtables::analyze_colvars()}} which apply corresponding \code{tern} statistics functions to add an analysis to a given table layout: \itemize{ +\item \code{\link[=analyze_patients_exposure_in_cols]{analyze_patients_exposure_in_cols()}} \item \code{\link[=analyze_vars_in_cols]{analyze_vars_in_cols()}} (extended wrapper, never used in other \code{tern} functions) \item \code{\link[=summarize_colvars]{summarize_colvars()}} (low level wrapper, never used in other \code{tern} functions) \item \code{\link[=summarize_coxreg]{summarize_coxreg()}} (contains also \code{\link[rtables:summarize_row_groups]{rtables::summarize_row_groups()}}) diff --git a/man/summarize_patients_exposure_in_cols.Rd b/man/summarize_patients_exposure_in_cols.Rd index 25f91944a7..b6ef0edfea 100644 --- a/man/summarize_patients_exposure_in_cols.Rd +++ b/man/summarize_patients_exposure_in_cols.Rd @@ -3,14 +3,29 @@ \name{summarize_patients_exposure_in_cols} \alias{summarize_patients_exposure_in_cols} \alias{s_count_patients_sum_exposure} +\alias{a_count_patients_sum_exposure} +\alias{analyze_patients_exposure_in_cols} \title{Counting Patients Summing Exposure Across All Patients in Columns} \usage{ s_count_patients_sum_exposure( df, - .var = "AVAL", + ex_var = "AVAL", + id = "USUBJID", + labelstr = "", + .stats = c("n_patients", "sum_exposure"), + .N_col, + custom_label = NULL +) + +a_count_patients_sum_exposure( + df, + var = NULL, + ex_var = "AVAL", id = "USUBJID", labelstr = "", .N_col, + .stats, + .formats = list(n_patients = "xx (xx.x\%)", sum_exposure = "xx"), custom_label = NULL ) @@ -23,30 +38,44 @@ summarize_patients_exposure_in_cols( .indent_mods = NULL, col_split = TRUE ) + +analyze_patients_exposure_in_cols( + lyt, + var = NULL, + ex_var = "AVAL", + col_split = TRUE, + .stats = c("n_patients", "sum_exposure"), + .labels = c(n_patients = "Patients", sum_exposure = "Person time"), + .indent_mods = 0L +) } \arguments{ \item{df}{(\code{data.frame})\cr data set containing all analysis variables.} -\item{.var, var}{(\code{string})\cr single variable name that is passed by \code{rtables} when requested -by a statistics function.} +\item{ex_var}{(\code{character})\cr name of the variable within \code{df} containing exposure values.} \item{id}{(\code{string})\cr subject variable name.} \item{labelstr}{(\code{character})\cr label of the level of the parent split currently being summarized (must be present as second argument in Content Row Functions).} +\item{.stats}{(\code{character})\cr statistics to select for the table.} + \item{.N_col}{(\code{count})\cr row-wise N (row group count) for the group of observations being analyzed (i.e. with no column-based subsetting) that is passed by \code{rtables}.} \item{custom_label}{(\code{string} or \code{NULL})\cr if provided and \code{labelstr} is empty then this will be used as label.} +\item{var}{(\code{string})\cr single variable name that is passed by \code{rtables} when requested +by a statistics function.} + +\item{.formats}{(named \code{character} or \code{list})\cr formats for the statistics.} + \item{lyt}{(\code{layout})\cr input layout where analyses will be added to.} \item{...}{additional arguments for the lower level functions.} -\item{.stats}{(\code{character})\cr statistics to select for the table.} - \item{.labels}{(named \code{character})\cr labels for the statistics (without indent).} \item{.indent_mods}{(named \code{integer})\cr indent modifiers for the labels.} @@ -59,14 +88,25 @@ column split has been done already earlier in the layout pipe.} \item \code{s_count_patients_sum_exposure()} returns a named \code{list} with the statistics: \itemize{ \item \code{n_patients}: Number of unique patients in \code{df}. -\item \code{sum_exposure}: Sum of \code{.var} across all patients in \code{df}. +\item \code{sum_exposure}: Sum of \code{ex_var} across all patients in \code{df}. } } +\itemize{ +\item \code{a_count_patients_sum_exposure()} returns formatted \code{\link[rtables:CellValue]{rtables::CellValue()}}. +} + \itemize{ \item \code{summarize_patients_exposure_in_cols()} returns a layout object suitable for passing to further layouting functions, or to \code{\link[rtables:build_table]{rtables::build_table()}}. Adding this function to an \code{rtable} layout will -add formatted rows, with the statistics from \code{s_count_patients_sum_exposure()} arranged in +add formatted content rows, with the statistics from \code{s_count_patients_sum_exposure()} arranged in +columns, to the table layout. +} + +\itemize{ +\item \code{analyze_patients_exposure_in_cols()} returns a layout object suitable for passing to further +layouting functions, or to \code{\link[rtables:build_table]{rtables::build_table()}}. Adding this function to an \code{rtable} layout will +add formatted data rows, with the statistics from \code{s_count_patients_sum_exposure()} arranged in columns, to the table layout. } } @@ -81,11 +121,23 @@ when a column table layout is required. \item \code{s_count_patients_sum_exposure()}: Statistics function which counts numbers of patients and the sum of exposure across all patients. +\item \code{a_count_patients_sum_exposure()}: Analysis function which is used as \code{afun} in +\code{\link[rtables:analyze_colvars]{rtables::analyze_colvars()}} within \code{analyze_patients_exposure_in_cols()} and as \code{cfun} in +\code{\link[rtables:summarize_row_groups]{rtables::summarize_row_groups()}} within \code{summarize_patients_exposure_in_cols()}. + \item \code{summarize_patients_exposure_in_cols()}: Layout-creating function which can take statistics function arguments and additional format arguments. This function is a wrapper for \code{\link[rtables:split_cols_by_multivar]{rtables::split_cols_by_multivar()}} and \code{\link[rtables:summarize_row_groups]{rtables::summarize_row_groups()}}. +\item \code{analyze_patients_exposure_in_cols()}: Layout-creating function which can take statistics +function arguments and additional format arguments. This function is a wrapper for +\code{\link[rtables:split_cols_by_multivar]{rtables::split_cols_by_multivar()}} and \code{\link[rtables:analyze_colvars]{rtables::analyze_colvars()}}. + }} +\note{ +As opposed to \code{\link[=summarize_patients_exposure_in_cols]{summarize_patients_exposure_in_cols()}} which generates content rows, this function +generates data rows which will \emph{not} be repeated on multiple pages when pagination is used. +} \examples{ set.seed(1) df <- data.frame( @@ -105,6 +157,7 @@ adsl <- data.frame( # Internal function - s_count_patients_sum_exposure \dontrun{ s_count_patients_sum_exposure(df = df, .N_col = nrow(adsl)) +s_count_patients_sum_exposure(df = df, .N_col = nrow(adsl), .stats = "n_patients") s_count_patients_sum_exposure( df = df, .N_col = nrow(adsl), @@ -112,34 +165,44 @@ s_count_patients_sum_exposure( ) } +tern:::a_count_patients_sum_exposure( + df = df, + var = "SEX", + .N_col = nrow(df), + .stats = "n_patients" +) + lyt <- 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) + summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE) result <- build_table(lyt, df = df, alt_counts_df = adsl) result lyt2 <- basic_table() \%>\% - split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) \%>\% - summarize_patients_exposure_in_cols( - var = "AVAL", col_split = TRUE, - .stats = "n_patients", custom_label = "some custom label" - ) \%>\% - split_rows_by("SEX") \%>\% - summarize_patients_exposure_in_cols(var = "AVAL", col_split = FALSE) + summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE, .stats = "sum_exposure") result2 <- build_table(lyt2, df = df, alt_counts_df = adsl) result2 lyt3 <- basic_table() \%>\% - summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE) + split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) \%>\% + summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE) \%>\% + analyze_patients_exposure_in_cols(var = "SEX", col_split = FALSE) result3 <- build_table(lyt3, df = df, alt_counts_df = adsl) result3 lyt4 <- basic_table() \%>\% - summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE, .stats = "sum_exposure") + split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) \%>\% + summarize_patients_exposure_in_cols( + var = "AVAL", col_split = TRUE, + .stats = "n_patients", custom_label = "some custom label" + ) \%>\% + analyze_patients_exposure_in_cols(var = "SEX", col_split = FALSE, ex_var = "AVAL") result4 <- build_table(lyt4, df = df, alt_counts_df = adsl) result4 +lyt5 <- basic_table() \%>\% + analyze_patients_exposure_in_cols(var = "SEX", col_split = TRUE, ex_var = "AVAL") +result5 <- build_table(lyt5, df = df, alt_counts_df = adsl) +result5 + } \keyword{internal} diff --git a/tests/testthat/_snaps/summarize_patients_exposure_in_cols.md b/tests/testthat/_snaps/summarize_patients_exposure_in_cols.md index 8fbb2ebeb4..ab49a2913f 100644 --- a/tests/testthat/_snaps/summarize_patients_exposure_in_cols.md +++ b/tests/testthat/_snaps/summarize_patients_exposure_in_cols.md @@ -14,6 +14,17 @@ [1] "Total patients numbers/person time" +# a_count_patients_sum_exposure works as expected + + Code + res + Output + RowsVerticalSection (in_rows) object print method: + ---------------------------- + row_name formatted_cell indent_mod row_label + 1 Female 6 (50.0%) 0 Female + 2 Male 6 (50.0%) 0 Male + # summarize_patients_exposure_in_cols works well with default arguments Code @@ -45,3 +56,34 @@ Output [1] "Patients" +# analyze_patients_exposure_in_cols works well with default arguments + + Code + res + Output + ARM A ARM B Total + Patients Person time Patients Person time Patients Person time + —————————————————————————————————————————————————————————————————————————————————————— + Female 6 (100.0%) 16 0 (0.0%) 0 6 (50.0%) 16 + Male 0 (0.0%) 0 6 (100.0%) 19 6 (50.0%) 19 + +# analyze_patients_exposure_in_cols works well with custom arguments + + Code + res + Output + ARM A ARM B Total + Person time Person time Person time + ———————————————————————————————————————————————— + Female 16 0 16 + Male 0 19 19 + +# analyze_patients_exposure_in_cols works with no variable split and only one statistic + + Code + res + Output + Patients + ———————————————————————————————————————————————— + Total patients numbers/person time 12 (100.0%) + diff --git a/tests/testthat/test-summarize_patients_exposure_in_cols.R b/tests/testthat/test-summarize_patients_exposure_in_cols.R index 16d526e146..c7eb6aa97b 100644 --- a/tests/testthat/test-summarize_patients_exposure_in_cols.R +++ b/tests/testthat/test-summarize_patients_exposure_in_cols.R @@ -17,12 +17,23 @@ adsl_local <- data.frame( testthat::test_that("s_count_patients_sum_exposure works as expected", { df <- anl_local adsl <- adsl_local - result <- s_count_patients_sum_exposure(df = df, .N_col = nrow(adsl)) # nolintr + result <- s_count_patients_sum_exposure(df = df, .N_col = nrow(adsl), .stats = c("n_patients", "sum_exposure")) res <- testthat::expect_silent(result) testthat::expect_snapshot(res) }) +testthat::test_that("a_count_patients_sum_exposure works as expected", { + result <- a_count_patients_sum_exposure( + df = anl_local, + var = "SEX", + .N_col = nrow(adsl_local), + .stats = "n_patients" + ) + + res <- testthat::expect_silent(result) + testthat::expect_snapshot(res) +}) testthat::test_that("summarize_patients_exposure_in_cols works well with default arguments", { df <- anl_local @@ -78,9 +89,60 @@ testthat::test_that( ) %>% build_table(df = df, alt_counts_df = adsl) - invisible(capture.output(result <- col_paths_summary(table)$label)) + invisible(capture.output({ + result <- col_paths_summary(table)$label + })) res <- testthat::expect_silent(result) testthat::expect_snapshot(res) } ) + +testthat::test_that("analyze_patients_exposure_in_cols works well with default arguments", { + df <- anl_local + adsl <- adsl_local + + result <- basic_table() %>% + split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) %>% + analyze_patients_exposure_in_cols(var = "SEX", col_split = TRUE) %>% + build_table(df = df, alt_counts_df = adsl) + + res <- testthat::expect_silent(result) + testthat::expect_snapshot(res) +}) + +testthat::test_that("analyze_patients_exposure_in_cols works well with custom arguments", { + df <- anl_local + adsl <- adsl_local + + result <- basic_table() %>% + split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) %>% + analyze_patients_exposure_in_cols( + var = "SEX", + col_split = TRUE, + .stats = "sum_exposure" + ) %>% + build_table(df = df, alt_counts_df = adsl) + + res <- testthat::expect_silent(result) + testthat::expect_snapshot(res) +}) + +testthat::test_that( + "analyze_patients_exposure_in_cols works with no variable split and only one statistic", + code = { + df <- anl_local + adsl <- adsl_local + + table <- basic_table() %>% + analyze_patients_exposure_in_cols( + ex_var = "AVAL", + col_split = TRUE, + .stats = "n_patients" + ) %>% + build_table(df = df, alt_counts_df = adsl) + + res <- testthat::expect_silent(table) + testthat::expect_snapshot(res) + } +)