diff --git a/NAMESPACE b/NAMESPACE index a4954f0ea4..f09604b8be 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -114,6 +114,7 @@ export(forest_viewport) export(format_auto) export(format_count_fraction) export(format_count_fraction_fixed_dp) +export(format_count_fraction_lt10) export(format_extreme_values) export(format_extreme_values_ci) export(format_fraction) diff --git a/NEWS.md b/NEWS.md index 99ae050914..6c2c8410b9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,10 @@ # tern 0.9.1.9000 +### Enhancements +* Added formatting function `format_count_fraction_lt10` for formatting `count_fraction` with special consideration when count is less than 10. + # tern 0.9.1 + ### New Features * Added `imputation_rule` function to apply imputation rule to data. * Added new format function `format_sigfig` to allow for numeric value formatting by a specified number of significant figures. @@ -23,6 +27,7 @@ * Removed `a_summary_internal()` in favor of only one main `a_summary()`. # tern 0.9.0 + ### New Features * Added `stat_propdiff_ci` function to calculate proportion/risk difference and CI. * Added risk difference column functionality via the `riskdiff` argument to functions `count_occurrences`, `count_occurrences_by_grade`, `count_patients_with_event`, `count_patients_with_flags`, `analyze_num_patients`, and `summarize_num_patients`. diff --git a/R/formatting_functions.R b/R/formatting_functions.R index 42753c65cf..37057a90e9 100644 --- a/R/formatting_functions.R +++ b/R/formatting_functions.R @@ -160,6 +160,43 @@ format_count_fraction_fixed_dp <- function(x, ...) { return(result) } +#' Formatting Count and Fraction with Special Case for Count < 10 +#' +#' @description `r lifecycle::badge("stable")` +#' +#' Formats a count together with fraction with special consideration when count is less than 10. +#' +#' @inheritParams format_count_fraction +#' +#' @return A string in the format `count (fraction %)`. If `count` is less than 10, only `count` is printed. +#' +#' @examples +#' format_count_fraction_lt10(x = c(275, 0.9673)) +#' format_count_fraction_lt10(x = c(2, 0.6667)) +#' format_count_fraction_lt10(x = c(9, 1)) +#' +#' @family formatting functions +#' @export +format_count_fraction_lt10 <- function(x, ...) { + attr(x, "label") <- NULL + + if (any(is.na(x))) { + return("NA") + } + + checkmate::assert_vector(x) + checkmate::assert_integerish(x[1]) + assert_proportion_value(x[2], include_boundaries = TRUE) + + result <- if (x[1] < 10) { + paste0(x[1]) + } else { + paste0(x[1], " (", round(x[2] * 100, 1), "%)") + } + + return(result) +} + #' Formatting: XX as Formatting Function #' #' Translate a string where x and dots are interpreted as number place diff --git a/man/extreme_format.Rd b/man/extreme_format.Rd index bb2fb05e3a..80d28c0fab 100644 --- a/man/extreme_format.Rd +++ b/man/extreme_format.Rd @@ -57,6 +57,7 @@ h_format_threshold(1000) Other formatting functions: \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, diff --git a/man/format_auto.Rd b/man/format_auto.Rd index 8f1323c13b..25b36cd3fd 100644 --- a/man/format_auto.Rd +++ b/man/format_auto.Rd @@ -46,6 +46,7 @@ format_auto(no_sc_x, "range")(x = no_sc_x) Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, diff --git a/man/format_count_fraction.Rd b/man/format_count_fraction.Rd index 3848647997..06520e7d60 100644 --- a/man/format_count_fraction.Rd +++ b/man/format_count_fraction.Rd @@ -29,6 +29,7 @@ Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, \code{\link{format_fraction_fixed_dp}()}, diff --git a/man/format_count_fraction_fixed_dp.Rd b/man/format_count_fraction_fixed_dp.Rd index 53b2db8ad1..0893f1aa9f 100644 --- a/man/format_count_fraction_fixed_dp.Rd +++ b/man/format_count_fraction_fixed_dp.Rd @@ -29,6 +29,7 @@ format_count_fraction_fixed_dp(x = c(0, 0)) Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, diff --git a/man/format_count_fraction_lt10.Rd b/man/format_count_fraction_lt10.Rd new file mode 100644 index 0000000000..ae4ca68f34 --- /dev/null +++ b/man/format_count_fraction_lt10.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/formatting_functions.R +\name{format_count_fraction_lt10} +\alias{format_count_fraction_lt10} +\title{Formatting Count and Fraction with Special Case for Count < 10} +\usage{ +format_count_fraction_lt10(x, ...) +} +\arguments{ +\item{x}{(\code{integer})\cr vector of length 2, count and fraction.} + +\item{...}{required for \code{rtables} interface.} +} +\value{ +A string in the format \verb{count (fraction \%)}. If \code{count} is less than 10, only \code{count} is printed. +} +\description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} + +Formats a count together with fraction with special consideration when count is less than 10. +} +\examples{ +format_count_fraction_lt10(x = c(275, 0.9673)) +format_count_fraction_lt10(x = c(2, 0.6667)) +format_count_fraction_lt10(x = c(9, 1)) + +} +\seealso{ +Other formatting functions: +\code{\link{extreme_format}}, +\code{\link{format_auto}()}, +\code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction}()}, +\code{\link{format_extreme_values_ci}()}, +\code{\link{format_extreme_values}()}, +\code{\link{format_fraction_fixed_dp}()}, +\code{\link{format_fraction_threshold}()}, +\code{\link{format_fraction}()}, +\code{\link{format_sigfig}()}, +\code{\link{format_xx}()}, +\code{\link{formatting_functions}} +} +\concept{formatting functions} diff --git a/man/format_extreme_values.Rd b/man/format_extreme_values.Rd index 5bf237a813..e523e45e04 100644 --- a/man/format_extreme_values.Rd +++ b/man/format_extreme_values.Rd @@ -30,6 +30,7 @@ Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_fraction_fixed_dp}()}, diff --git a/man/format_extreme_values_ci.Rd b/man/format_extreme_values_ci.Rd index 0e526c8fe2..f7b1d56988 100644 --- a/man/format_extreme_values_ci.Rd +++ b/man/format_extreme_values_ci.Rd @@ -30,6 +30,7 @@ Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values}()}, \code{\link{format_fraction_fixed_dp}()}, diff --git a/man/format_fraction.Rd b/man/format_fraction.Rd index dfd46accd9..452b5414d6 100644 --- a/man/format_fraction.Rd +++ b/man/format_fraction.Rd @@ -29,6 +29,7 @@ Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, diff --git a/man/format_fraction_fixed_dp.Rd b/man/format_fraction_fixed_dp.Rd index 71f9d0d615..37686e7a9d 100644 --- a/man/format_fraction_fixed_dp.Rd +++ b/man/format_fraction_fixed_dp.Rd @@ -31,6 +31,7 @@ Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, diff --git a/man/format_fraction_threshold.Rd b/man/format_fraction_threshold.Rd index 899eba757c..e586490dd0 100644 --- a/man/format_fraction_threshold.Rd +++ b/man/format_fraction_threshold.Rd @@ -33,6 +33,7 @@ Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, diff --git a/man/format_sigfig.Rd b/man/format_sigfig.Rd index 2177142c96..f592939630 100644 --- a/man/format_sigfig.Rd +++ b/man/format_sigfig.Rd @@ -39,6 +39,7 @@ Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, diff --git a/man/format_xx.Rd b/man/format_xx.Rd index 957d534023..ed08e9c382 100644 --- a/man/format_xx.Rd +++ b/man/format_xx.Rd @@ -34,6 +34,7 @@ Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, diff --git a/man/formatting_functions.Rd b/man/formatting_functions.Rd index e9998ab398..d800e54664 100644 --- a/man/formatting_functions.Rd +++ b/man/formatting_functions.Rd @@ -16,6 +16,7 @@ Other formatting functions: \code{\link{extreme_format}}, \code{\link{format_auto}()}, \code{\link{format_count_fraction_fixed_dp}()}, +\code{\link{format_count_fraction_lt10}()}, \code{\link{format_count_fraction}()}, \code{\link{format_extreme_values_ci}()}, \code{\link{format_extreme_values}()}, diff --git a/tests/testthat/_snaps/formats.md b/tests/testthat/_snaps/formats.md index 78f507bca7..854ad6994f 100644 --- a/tests/testthat/_snaps/formats.md +++ b/tests/testthat/_snaps/formats.md @@ -68,6 +68,20 @@ Output [1] "0" +# format_count_fraction_lt10 works with healthy inputs + + Code + res + Output + [1] "10 (100%)" "19 (51.8%)" "76 (99.6%)" + +# format_count_fraction_lt10 works with count less than 10 + + Code + res + Output + [1] "9" "1" "7" + # format_xx works with easy inputs Code diff --git a/tests/testthat/test-formats.R b/tests/testthat/test-formats.R index 80f66e621b..1ff90a747a 100644 --- a/tests/testthat/test-formats.R +++ b/tests/testthat/test-formats.R @@ -73,6 +73,30 @@ testthat::test_that("format_count_fraction_fixed_dp works with count of 0", { testthat::expect_snapshot(res) }) +testthat::test_that("format_count_fraction_lt10 works with healthy inputs", { + x <- list(c(10, 1), c(19, 0.5183), c(76, 0.996)) + + result <- sapply(x, format_count_fraction_lt10) + + res <- testthat::expect_silent(result) + testthat::expect_snapshot(res) +}) + +testthat::test_that("format_count_fraction_lt10 works with count less than 10", { + x <- list(c(9, 1), c(1, 0.5), c(7, 0.99)) + + result <- sapply(x, format_count_fraction_lt10) + + res <- testthat::expect_silent(result) + testthat::expect_snapshot(res) +}) + +testthat::test_that("format_count_fraction_lt10 works with NA input", { + result <- format_count_fraction_lt10(NA) + testthat::expect_identical(result, "NA") +}) + + testthat::test_that("format_fraction fails with bad inputs", { x <- list( c(num = c(1L, 2L, 3L), denom = 5L),