diff --git a/R/analyze_variables.R b/R/analyze_variables.R index bb9f3a8eb6..2c104e2c08 100644 --- a/R/analyze_variables.R +++ b/R/analyze_variables.R @@ -160,8 +160,8 @@ s_summary <- function(x, denom, control, ...) { s_summary.numeric <- function(x, control = control_analyze_vars(), ...) { checkmate::assert_numeric(x) args_list <- list(...) - .N_row <- args_list[[".N_row"]] - .N_col <- args_list[[".N_col"]] + .N_row <- args_list[[".N_row"]] # nolint + .N_col <- args_list[[".N_col"]] # nolint na_rm <- args_list[["na_rm"]] %||% TRUE compare_with_ref_group <- args_list[["compare_with_ref_group"]] @@ -308,8 +308,8 @@ s_summary.numeric <- function(x, control = control_analyze_vars(), ...) { s_summary.factor <- function(x, denom = c("n", "N_col", "N_row"), ...) { assert_valid_factor(x) args_list <- list(...) - .N_row <- args_list[[".N_row"]] - .N_col <- args_list[[".N_col"]] + .N_row <- args_list[[".N_row"]] # nolint + .N_col <- args_list[[".N_col"]] # nolint na_rm <- args_list[["na_rm"]] %||% TRUE verbose <- args_list[["verbose"]] %||% TRUE compare_with_ref_group <- args_list[["compare_with_ref_group"]] @@ -322,7 +322,7 @@ s_summary.factor <- function(x, denom = c("n", "N_col", "N_row"), ...) { y <- list() - y$n <- list("n" = c("n" = length(x))) # all list of a list + y$n <- list("n" = c("n" = length(x))) # all list of a list y$count <- lapply(as.list(table(x, useNA = "ifany")), setNames, nm = "count") @@ -446,8 +446,8 @@ s_summary.character <- function(x, ...) { s_summary.logical <- function(x, denom = c("n", "N_col", "N_row"), ...) { checkmate::assert_logical(x) args_list <- list(...) - .N_row <- args_list[[".N_row"]] - .N_col <- args_list[[".N_col"]] + .N_row <- args_list[[".N_row"]] # nolint + .N_col <- args_list[[".N_col"]] # nolint na_rm <- args_list[["na_rm"]] %||% TRUE compare_with_ref_group <- args_list[["compare_with_ref_group"]] @@ -497,14 +497,16 @@ s_summary.logical <- function(x, denom = c("n", "N_col", "N_row"), ...) { #' @describeIn analyze_variables Formatted analysis function which is used as `afun` in `analyze_vars()` and #' `compare_vars()` and as `cfun` in `summarize_colvars()`. #' -#' @param compare_with_ref_group (`flag`)\cr whether comparison statistics should be analyzed instead of summary statistics -#' (`compare_with_ref_group = TRUE` adds `pval` statistic comparing against reference group). +#' @param compare_with_ref_group (`flag`)\cr whether comparison statistics should be analyzed instead of summary +#' statistics (`compare_with_ref_group = TRUE` adds `pval` statistic comparing +#' against reference group). #' #' @return #' * `a_summary()` returns the corresponding list with formatted [rtables::CellValue()]. #' #' @note -#' * To use for comparison (with additional p-value statistic), parameter `compare_with_ref_group` must be set to `TRUE`. +#' * To use for comparison (with additional p-value statistic), parameter +#' `compare_with_ref_group` must be set to `TRUE`. #' * Ensure that either all `NA` values are converted to an explicit `NA` level or all `NA` values are left as is. #' #' @examples @@ -529,7 +531,8 @@ s_summary.logical <- function(x, denom = c("n", "N_col", "N_row"), ...) { #' ) #' #' a_summary(rnorm(10), .N_col = 10, .N_row = 20, .var = "bla") -#' a_summary(rnorm(10, 5, 1), .ref_group = rnorm(20, -5, 1), .var = "bla", compare_with_ref_group = TRUE, +#' a_summary(rnorm(10, 5, 1), +#' .ref_group = rnorm(20, -5, 1), .var = "bla", compare_with_ref_group = TRUE, #' .in_ref_col = FALSE #' ) #' @@ -559,19 +562,17 @@ a_summary <- function(x, dots_extra_args$.additional_fun_parameters <- NULL # After extraction we do not need them anymore # If one col has NA vals, must add NA row to other cols (using placeholder lvl `fill-na-level`) - if (any(is.na(dots_extra_args$.df_row[[dots_extra_args$.var]])) && - !any(is.na(x)) && - !dots_extra_args$na_rm) { + if (any(is.na(dots_extra_args$.df_row[[dots_extra_args$.var]])) && !any(is.na(x)) && !dots_extra_args$na_rm) { levels(x) <- c(levels(x), "fill-na-level") } # Check if compare_with_ref_group is TRUE but no ref col is set if (isTRUE(dots_extra_args$compare_with_ref_group) && - all( - length(dots_extra_args[[".ref_group"]]) == 0, # only used for testing - length(extra_afun_params[[".ref_group"]]) == 0 - ) - ) { + all( + length(dots_extra_args[[".ref_group"]]) == 0, # only used for testing + length(extra_afun_params[[".ref_group"]]) == 0 + ) + ) { stop( "For comparison (compare_with_ref_group = TRUE), the reference group must be specified.", "\nSee split_fun in spit_cols_by()." @@ -593,8 +594,9 @@ a_summary <- function(x, met_grp <- paste0(c("analyze_vars", type), collapse = "_") .stats <- c( get_stats(met_grp, - stats_in = .stats, - add_pval = dots_extra_args$compare_with_ref_group %||% FALSE), + stats_in = .stats, + add_pval = dots_extra_args$compare_with_ref_group %||% FALSE + ), names(custom_stat_functions) # Additional stats from custom functions ) @@ -726,13 +728,13 @@ analyze_vars <- function(lyt, vars, var_labels = vars, na_str = default_na_str(), - na_rm = TRUE, nested = TRUE, show_labels = "default", table_names = vars, section_div = NA_character_, - compare_with_ref_group = FALSE, ..., + na_rm = TRUE, + compare_with_ref_group = FALSE, .stats = c("n", "mean_sd", "median", "range", "count_fraction"), .stat_names_in = NULL, .formats = NULL, @@ -745,8 +747,10 @@ analyze_vars <- function(lyt, if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods # Adding additional arguments to the analysis function (depends on the specific call) - extra_args <- c(extra_args, "na_rm" = na_rm, - "compare_with_ref_group" = compare_with_ref_group, ...) + extra_args <- c(extra_args, + "na_rm" = na_rm, + "compare_with_ref_group" = compare_with_ref_group, ... + ) # Adding all additional information from layout to analysis functions (see ?rtables::additional_fun_params) extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) diff --git a/R/compare_variables.R b/R/compare_variables.R index e25cc74ce6..92f31aa1bc 100644 --- a/R/compare_variables.R +++ b/R/compare_variables.R @@ -222,7 +222,6 @@ compare_vars <- function(lyt, .formats = NULL, .labels = NULL, .indent_mods = NULL) { - analyze_vars( lyt = lyt, compare_with_ref_group = TRUE, diff --git a/man/analyze_variables.Rd b/man/analyze_variables.Rd index 800faebbd0..af28fa9509 100644 --- a/man/analyze_variables.Rd +++ b/man/analyze_variables.Rd @@ -16,13 +16,13 @@ analyze_vars( vars, var_labels = vars, na_str = default_na_str(), - na_rm = TRUE, nested = TRUE, show_labels = "default", table_names = vars, section_div = NA_character_, - compare_with_ref_group = FALSE, ..., + na_rm = TRUE, + compare_with_ref_group = FALSE, .stats = c("n", "mean_sd", "median", "range", "count_fraction"), .stat_names_in = NULL, .formats = NULL, @@ -71,11 +71,12 @@ times, to avoid warnings from \code{rtables}.} \item{section_div}{(\code{string})\cr string which should be repeated as a section divider after each group defined by this split instruction, or \code{NA_character_} (the default) for no section divider.} -\item{compare_with_ref_group}{(\code{flag})\cr whether comparison statistics should be analyzed instead of summary statistics -(\code{compare_with_ref_group = TRUE} adds \code{pval} statistic comparing against reference group).} - \item{...}{arguments passed to \code{s_summary()}.} +\item{compare_with_ref_group}{(\code{flag})\cr whether comparison statistics should be analyzed instead of summary +statistics (\code{compare_with_ref_group = TRUE} adds \code{pval} statistic comparing +against reference group).} + \item{.stats}{(\code{character})\cr statistics to select for the table. Options for numeric variables are: \verb{'n', 'sum', 'mean', 'sd', 'se', 'mean_sd', 'mean_se', 'mean_ci', 'mean_sei', 'mean_sdi', 'mean_pval', 'median', 'mad', 'median_ci', 'quantiles', 'iqr', 'range', 'min', 'max', 'median_range', 'cv', 'geom_mean', 'geom_mean_ci', 'geom_cv', 'median_ci_3d', 'mean_ci_3d', 'geom_mean_ci_3d'} @@ -241,7 +242,8 @@ created from character variables before passing the dataset to \code{\link[rtabl } \itemize{ -\item To use for comparison (with additional p-value statistic), parameter \code{compare_with_ref_group} must be set to \code{TRUE}. +\item To use for comparison (with additional p-value statistic), parameter +\code{compare_with_ref_group} must be set to \code{TRUE}. \item Ensure that either all \code{NA} values are converted to an explicit \code{NA} level or all \code{NA} values are left as is. } } @@ -399,7 +401,8 @@ a_summary( ) a_summary(rnorm(10), .N_col = 10, .N_row = 20, .var = "bla") -a_summary(rnorm(10, 5, 1), .ref_group = rnorm(20, -5, 1), .var = "bla", compare_with_ref_group = TRUE, +a_summary(rnorm(10, 5, 1), + .ref_group = rnorm(20, -5, 1), .var = "bla", compare_with_ref_group = TRUE, .in_ref_col = FALSE ) diff --git a/tests/testthat/test-analyze_variables.R b/tests/testthat/test-analyze_variables.R index b5569a85ac..e467e4b086 100644 --- a/tests/testthat/test-analyze_variables.R +++ b/tests/testthat/test-analyze_variables.R @@ -230,7 +230,8 @@ testthat::test_that("a_summary works with custom input.", { testthat::expect_snapshot(res) result <- a_summary( - factor(c("a", "a", "b", "c", NA)), compare_with_ref_group = FALSE, + factor(c("a", "a", "b", "c", NA)), + compare_with_ref_group = FALSE, .N_row = 10, .N_col = 10, .formats = c(n = "xx.xx"), .labels = c(n = "number of records"), .indent_mods = c(n = -1L, count = 5L), na_rm = FALSE ) @@ -242,8 +243,10 @@ testthat::test_that("a_summary works with healthy input when compare_with_ref_gr options("width" = 100) # numeric input set.seed(1) - result <- a_summary(rnorm(10, 5, 1), .ref_group = rnorm(20, -5, 1), .var = "bla", - compare_with_ref_group = TRUE, .in_ref_col = FALSE) + result <- a_summary(rnorm(10, 5, 1), + .ref_group = rnorm(20, -5, 1), .var = "bla", + compare_with_ref_group = TRUE, .in_ref_col = FALSE + ) res <- testthat::expect_silent(result) testthat::expect_snapshot(res) @@ -257,13 +260,19 @@ testthat::test_that("a_summary works with healthy input when compare_with_ref_gr testthat::expect_snapshot(res) # character input - result <- a_summary(c("A", "B", "A", "C"), .ref_group = c("B", "A", "C"), - .var = "x", compare_with_ref_group = TRUE, .in_ref_col = FALSE, verbose = FALSE) + result <- a_summary(c("A", "B", "A", "C"), + .ref_group = c("B", "A", "C"), + .var = "x", compare_with_ref_group = TRUE, .in_ref_col = FALSE, + verbose = FALSE + ) res <- testthat::expect_silent(result) testthat::expect_snapshot(res) # logical input - result <- a_summary(c(TRUE, FALSE, FALSE, TRUE, TRUE), .ref_group = c(TRUE, FALSE), compare_with_ref_group = TRUE, .in_ref_col = FALSE) + result <- a_summary(c(TRUE, FALSE, FALSE, TRUE, TRUE), + .ref_group = c(TRUE, FALSE), compare_with_ref_group = TRUE, + .in_ref_col = FALSE + ) res <- testthat::expect_silent(result) testthat::expect_snapshot(res) }) diff --git a/tests/testthat/test-compare_variables.R b/tests/testthat/test-compare_variables.R index dec35ac963..6028a150c3 100644 --- a/tests/testthat/test-compare_variables.R +++ b/tests/testthat/test-compare_variables.R @@ -3,8 +3,7 @@ testthat::test_that("s_compare works for numeric", { stats::rnorm(10, 5, 1), .ref_group = stats::rnorm(5, -5, 1), .in_ref_col = FALSE - ) - ) + )) res <- testthat::expect_silent(names(result)) testthat::expect_snapshot(res) diff --git a/tests/testthat/test-utils_default_stats_formats_labels.R b/tests/testthat/test-utils_default_stats_formats_labels.R index 962288e29b..1213366e1e 100644 --- a/tests/testthat/test-utils_default_stats_formats_labels.R +++ b/tests/testthat/test-utils_default_stats_formats_labels.R @@ -227,9 +227,4 @@ testthat::test_that("get_stat_names works fine", { out <- get_stat_names(stat_results, list("n" = "argh")) testthat::expect_equal(out[1], list("n" = "argh")) - - # testthat::expect_error( - # out <- get_stat_names(stat_results, list("n" = c("1", "2"))), - # "The number of stat names for n" - # ) })