Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix format precision for format_count_fraction_fixed_dp #1192

Merged
merged 10 commits into from
Feb 26, 2024
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Bug Fixes
* Fixed nested column split label overlay issue in `rtable2gg` to clean up appearance of text labels.
* Fixed bug in `s_ancova` causing incorrect difference calculations for arm variables with irregular levels.
* Fixed bug in `format_count_fraction_fixed_dp` that did not have the same print when the fraction was 1 (100%).

### Miscellaneous
* Added function `expect_snapshot_ggplot` to test setup file to process plot snapshot tests and allow plot dimensions to be set.
Expand Down
2 changes: 1 addition & 1 deletion R/formatting_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ format_count_fraction_fixed_dp <- function(x, ...) {

result <- if (x[1] == 0) {
"0"
} else if (x[2] == 1) {
} else if (all.equal(x[2], 1)) {
sprintf("%d (100%%)", x[1])
} else {
sprintf("%d (%.1f%%)", x[1], x[2] * 100)
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-formats.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,22 @@ testthat::test_that("format_extreme_values_ci works with easy inputs", {
"Number of inserted values as result \\(1\\)*"
)
})

testthat::test_that("formats with nominator == to denominator are always formatted as 1", {
# Regression test for #1191
df <- data.frame(Ncol = seq(500)) %>%
rowwise() %>%
mutate(count = Ncol) %>%
mutate(pct = count * (1 / Ncol)) %>%
mutate(check_new = all.equal(pct, 1)) %>%
mutate(check = pct == 1) %>%
mutate(fmt_print = format_count_fraction_fixed_dp(c(count, pct)))

testthat::expect_true(nrow(df %>% filter(isFALSE(check))) > 0)
testthat::expect_equal(nrow(df %>% filter(isFALSE(check_new))), 0)

testthat::expect_equal(
sapply(df$fmt_print, function(x) substr(x, max(1, nchar(x) - 5), nchar(x)), USE.NAMES = FALSE),
rep("(100%)", nrow(df))
)
})
Loading