Skip to content

Commit

Permalink
Remove the ungroup_stats() function (#1385)
Browse files Browse the repository at this point in the history
Fixes #1370
  • Loading branch information
edelarua authored Jan 31, 2025
1 parent 41cd8c4 commit 2321c63
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 72 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### Bug Fixes
* Fixed bug in `a_count_patients_with_flags()` preventing select custom label and indentation specification formats from being applied.

### Miscellaneous
* Removed internal function `ungroup_stats()` and replaced its usage with the `get_*_from_stats()` functions.

# tern 0.9.7

### Enhancements
Expand Down
41 changes: 0 additions & 41 deletions R/utils_factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,44 +276,3 @@ fct_collapse_only <- function(.f, ..., .na_level = "<Missing>") {
x <- forcats::fct_collapse(.f, ..., other_level = .na_level)
do.call(forcats::fct_relevel, args = c(list(.f = x), as.list(new_lvls)))
}

#' Ungroup non-numeric statistics
#'
#' Ungroups grouped non-numeric statistics within input vectors `.formats`, `.labels`, and `.indent_mods`.
#'
#' @inheritParams argument_convention
#' @param stat_out (named `list` of `numeric`)\cr list of numeric statistics containing the statistics to ungroup.
#'
#' @return A `list` with modified elements `stat_out`, `.formats`, `.labels`, `.levels`, and `.indent_mods`.
#'
#' @seealso [a_summary()] which uses this function internally.
#'
#' @keywords internal
ungroup_stats <- function(stat_out,
.formats,
.indent_mods) {
checkmate::assert_list(stat_out)
empty_pval <- "pval" %in% names(stat_out) && length(stat_out[["pval"]]) == 0
empty_pval_counts <- "pval_counts" %in% names(stat_out) && length(stat_out[["pval_counts"]]) == 0
stat_out <- unlist(stat_out, recursive = FALSE)

# If p-value is empty it is removed by unlist and needs to be re-added
if (empty_pval) stat_out[["pval"]] <- character()
if (empty_pval_counts) stat_out[["pval_counts"]] <- character()
.stats <- sapply(regmatches(names(stat_out), regexpr("\\.", names(stat_out)), invert = TRUE), function(xi) xi[[1]])

# Ungroup stats
.formats <- lapply(.stats, function(x) {
.formats[[if (!grepl("\\.", x)) x else regmatches(x, regexpr("\\.", x), invert = TRUE)[[1]][1]]]
})

.indent_mods <- sapply(.stats, function(x) {
.indent_mods[[if (!grepl("\\.", x)) x else regmatches(x, regexpr("\\.", x), invert = TRUE)[[1]][1]]]
})

list(
x = stat_out,
.formats = .formats,
.indent_mods = .indent_mods
)
}
27 changes: 0 additions & 27 deletions man/ungroup_stats.Rd

This file was deleted.

1 change: 1 addition & 0 deletions tern.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 9441de74-2fb5-42be-9c7a-c2a704b8aa93

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
6 changes: 2 additions & 4 deletions tests/testthat/test-analyze_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,10 @@ testthat::test_that("analyze_vars works well with additional stat names (.stat_n

# format for v is correctly printed (added external statistic)
testthat::expect_equal(
as_result_df(res, data_format = "strings")[nrow(res2), ncol(res2)],
as_result_df(res, data_format = "strings")[["all obs"]][nrow(res2)],
c("0.00") # i.e. x.xx
)
})
testthat::test_that("analyze_vars works well with additional stat names (.stat_names) and stats (custom fnc)", {
dt <- data.frame("VAR" = c(0.001, 0.2, 0.0011000, 3, 4), "VAR2" = letters[seq(5)])

res <- basic_table() %>%
analyze_vars(
vars = c("VAR", "VAR2"),
Expand Down

0 comments on commit 2321c63

Please sign in to comment.