Skip to content

Commit

Permalink
Corrected bug in tbl_cross(digits) (#1944)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg authored Aug 30, 2024
1 parent 90c36ff commit 19950e9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gtsummary
Title: Presentation-Ready Data Summary and Analytic Result Tables
Version: 2.0.1.9010
Version: 2.0.1.9011
Authors@R: c(
person("Daniel D.", "Sjoberg", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-0862-2018")),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Updates to address regressions in the v2.0.0 release:
* We can again report unweighted statistics in the headers of `tbl_svysummary()` tables. (#1911)
* `tbl_uvregression()` properly handles variables specified in the `include` argument with non-syntactic names. (#1932)
* `NA` values can again be specified in `add_stat_label(label)` to suppress a statistic label from being placed. (#1937)
* Corrected bug in `tbl_cross()` where the `digits` argument was not always being passed accurately to `tbl_summary()`. (#1943)

### Other updates

Expand Down
2 changes: 1 addition & 1 deletion R/tbl_cross.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ tbl_cross <- function(data,
statistic = ~ statistic,
digits =
case_switch(
!is_empty(digits) ~ everything() ~ digits,
!is_empty(digits) ~ (everything() ~ digits),
.default = NULL
),
percent = ifelse(percent == "none", "cell", percent),
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/_snaps/tbl_cross.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,22 @@
Error in `tbl_cross()`:
! `percent` must be a character vector, not the number 1.

# tbl_cross(digits) works

Code
as.data.frame(tbl_cross(dplyr::bind_rows(rep(list(trial), 11L)), row = grade,
col = trt, statistic = "{n}/{N_nonmiss}/{N} ({p}%)", digits = c(0, 0, 0, 4)))
Output
Drug A Drug B
1 grade <NA> <NA>
2 I 385/1,078/2,200 (17.5000%) 363/1,122/2,200 (16.5000%)
3 II 352/1,078/2,200 (16.0000%) 396/1,122/2,200 (18.0000%)
4 III 341/1,078/2,200 (15.5000%) 363/1,122/2,200 (16.5000%)
5 Total 1,078/1,078/2,200 (49.0000%) 1,122/1,122/2,200 (51.0000%)
Total
1 <NA>
2 748/2,200/2,200 (34.0000%)
3 748/2,200/2,200 (34.0000%)
4 704/2,200/2,200 (32.0000%)
5 2,200/2,200/2,200 (100.0000%)

16 changes: 16 additions & 0 deletions tests/testthat/test-tbl_cross.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,19 @@ test_that("tbl_cross(percent) errors properly", {
expect_snapshot(error = TRUE, tbl_cross(trial2, percent = "columsadasn"))
expect_snapshot(error = TRUE, tbl_cross(trial2, percent = 1))
})

# tbl_cross(digits) -----------------------------------------------------------
test_that("tbl_cross(digits) works", {
# ensuring the proper formatting is passed along
expect_snapshot(
tbl_cross(
rep(list(trial), 11L) |> dplyr::bind_rows(),
row = grade,
col = trt,
statistic = "{n}/{N_nonmiss}/{N} ({p}%)",
digits = c(0, 0, 0, 4)
) |>
as.data.frame()
)
})

0 comments on commit 19950e9

Please sign in to comment.