Skip to content

Commit

Permalink
update (ddsjoberg#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg authored Oct 13, 2024
1 parent af17cb2 commit 09015d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Bug fix in `add_ci.tbl_svysummary()` for factor variables where order was alphabetical instead of the factor levels. (#2036)

* Addressing encoding issue where `sort()` and `dplyr::arrange()` sorted differently, and the order of the `by` levels was inconsistent in the resulting table. (#2038)

# gtsummary 2.0.3

### New Features and Functions
Expand Down
4 changes: 2 additions & 2 deletions R/tbl_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ tbl_summary <- function(data,
!cards$context %in% "attributes",
) |>
dplyr::select(cards::all_ard_groups(), "variable", "context") |>
dplyr::distinct() |>
dplyr::arrange(unlist(!!sym("group1_level"))) |>
dplyr::distinct() %>%
{.[order(unlist(.$group1_level)), ]} |> # styler: off
dplyr::mutate(
.by = cards::all_ard_groups(),
gts_column = paste0("stat_", dplyr::cur_group_id())
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-tbl_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,18 @@ test_that("tbl_summary() data frame column labels are not dropped", {
"Age"
)
})

# addressing issue #2038
test_that("tbl_summary() test encoding/sorting difference between sort() and dplyr::arrange()", {
# before this fix, the columns would print in reverse order, e.g. stat_3, stat_2, stat_1
expect_equal(
data.frame(
groupe_etude = c(rep("Tem", 13), rep("TTA-", 7), rep("TTA+", 18)),
tympan_g_inc = rep(1,38)
) |>
tbl_summary(by = groupe_etude, include = tympan_g_inc) |>
as.data.frame(col_labels = FALSE) |>
names(),
c("label", "stat_1", "stat_2", "stat_3")
)
})

0 comments on commit 09015d4

Please sign in to comment.