From 6f5e208b618e0e7d37685e8228158bc72b86fc9e Mon Sep 17 00:00:00 2001 From: ddsjoberg Date: Wed, 8 Jul 2020 09:00:31 -0400 Subject: [PATCH 1/6] #567 fix for ordered factors in tbl_summary --- R/tbl_summary.R | 7 ++++--- tests/testthat/test-tbl_summary.R | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/R/tbl_summary.R b/R/tbl_summary.R index 1658eb062b..0cae156fb3 100644 --- a/R/tbl_summary.R +++ b/R/tbl_summary.R @@ -195,9 +195,10 @@ tbl_summary <- function(data, by = NULL, label = NULL, statistic = NULL, # checking input data -------------------------------------------------------- tbl_summary_data_checks(data) - # removing orderered class from factor by variables -------------------------- - if (!is.null(by) && inherits(data[[by]], "ordered") && inherits(data[[by]], "factor")) { - data[[by]] <- factor(data[[by]], ordered = FALSE) + # removing ordered class from factor variables ------------------------------- + for (v in names(data)) { + if (inherits(data[[v]], "ordered") && inherits(data[[v]], "factor")) + data[[v]] <- factor(data[[v]], ordered = FALSE) } # deleting obs with missing by values ---------------------------------------- diff --git a/tests/testthat/test-tbl_summary.R b/tests/testthat/test-tbl_summary.R index 708e624d7f..2de73c1243 100644 --- a/tests/testthat/test-tbl_summary.R +++ b/tests/testthat/test-tbl_summary.R @@ -401,3 +401,14 @@ test_that("tbl_summary- works with grouped data (it ungroups it first)", { ) }) +test_that("tbl_summary-works with ordered factors", { + expect_error( + trial %>% + select(response, trt) %>% + dplyr::mutate_at(vars(response, trt), + ~factor(., ordered = TRUE)) %>% + tbl_summary(by = trt), + NA + ) +}) + From 221d87a27970f1eaed692b45d25d9d980c5a82e6 Mon Sep 17 00:00:00 2001 From: ddsjoberg Date: Wed, 8 Jul 2020 10:59:16 -0400 Subject: [PATCH 2/6] updates after checks --- R/tbl_summary.R | 9 ++++----- tests/testthat/test-add_overall.R | 12 ++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/R/tbl_summary.R b/R/tbl_summary.R index 0cae156fb3..806404430e 100644 --- a/R/tbl_summary.R +++ b/R/tbl_summary.R @@ -196,10 +196,9 @@ tbl_summary <- function(data, by = NULL, label = NULL, statistic = NULL, tbl_summary_data_checks(data) # removing ordered class from factor variables ------------------------------- - for (v in names(data)) { - if (inherits(data[[v]], "ordered") && inherits(data[[v]], "factor")) - data[[v]] <- factor(data[[v]], ordered = FALSE) - } + data <- dplyr::mutate_if(data, + ~inherits(., "ordered") && inherits(., "factor"), + ~factor(., levels = attr(., "levels"), ordered = FALSE)) # deleting obs with missing by values ---------------------------------------- # saving variable labels @@ -212,7 +211,7 @@ tbl_summary <- function(data, by = NULL, label = NULL, statistic = NULL, lbls <- purrr::map(data, ~ attr(.x, "label")) data <- data[!is.na(data[[by]]), ] - # re-applying labels---I think this will NOT be necessary after dplyr 0.9.0 + # re-applying labels---I think this will NOT be necessary after dplyr 1.0 for (i in names(lbls)) { attr(data[[i]], "label") <- lbls[[i]] } diff --git a/tests/testthat/test-add_overall.R b/tests/testthat/test-add_overall.R index 590915a1cf..7d4a1f598f 100644 --- a/tests/testthat/test-add_overall.R +++ b/tests/testthat/test-add_overall.R @@ -24,3 +24,15 @@ test_that("no errors/warnings with missing data", { test_that("no errors/warnings with missing data in by variable", { expect_error(trial %>% tbl_summary(by = response) %>% add_overall(), NA) }) + +test_that("add_overall-works with ordered factors", { + expect_error( + trial %>% + select(response, trt) %>% + dplyr::mutate_at(vars(response, trt), + ~factor(., ordered = TRUE)) %>% + tbl_summary(by = trt) %>% + add_overall(), + NA + ) +}) From 08171104230f671665d9a1fecb1fff557df9e499 Mon Sep 17 00:00:00 2001 From: ddsjoberg Date: Wed, 8 Jul 2020 11:32:47 -0400 Subject: [PATCH 3/6] Update R-CMD-check.yaml --- .github/workflows/R-CMD-check.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index f45437283b..dd942340bd 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -57,6 +57,11 @@ jobs: if: runner.os == 'macOS' run: brew cask install xquartz + # testing 2020-07-08 + - name: Install system dependencies + if: runner.os == 'Linux' + run: brew install nlopt + - name: Query dependencies run: | install.packages('remotes') From 2f9e0922da764c76578af8823884a08df63885a2 Mon Sep 17 00:00:00 2001 From: ddsjoberg Date: Wed, 8 Jul 2020 11:48:54 -0400 Subject: [PATCH 4/6] Update R-CMD-check.yaml --- .github/workflows/R-CMD-check.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index dd942340bd..f45437283b 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -57,11 +57,6 @@ jobs: if: runner.os == 'macOS' run: brew cask install xquartz - # testing 2020-07-08 - - name: Install system dependencies - if: runner.os == 'Linux' - run: brew install nlopt - - name: Query dependencies run: | install.packages('remotes') From 51a498d50e66ffa90800f6471efc49ac97169833 Mon Sep 17 00:00:00 2001 From: "Daniel D. Sjoberg" Date: Wed, 15 Jul 2020 08:40:34 -0400 Subject: [PATCH 5/6] Update test-add_overall.R --- tests/testthat/test-add_overall.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-add_overall.R b/tests/testthat/test-add_overall.R index 279290df15..a827a0bd01 100644 --- a/tests/testthat/test-add_overall.R +++ b/tests/testthat/test-add_overall.R @@ -35,6 +35,7 @@ test_that("add_overall-works with ordered factors", { add_overall(), NA ) +}) test_that("no errors/warnings with standard use for tbl_svysummary", { t <- trial %>% From 448b1fd69a6e9b2f6d8ae10e6ae14b0ff93a552c Mon Sep 17 00:00:00 2001 From: ddsjoberg Date: Sun, 19 Jul 2020 21:29:37 -0400 Subject: [PATCH 6/6] increment version number --- DESCRIPTION | 2 +- NEWS.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index c5f4e236e6..d75689cace 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: gtsummary Title: Presentation-Ready Data Summary and Analytic Result Tables -Version: 1.3.2.9008 +Version: 1.3.2.9009 Authors@R: c(person(given = "Daniel D.", family = "Sjoberg", diff --git a/NEWS.md b/NEWS.md index 7bd43d0dac..aa94984719 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # gtsummary (development version) +* Bug fix `tbl_summary()` when a data frame contained an ordered factor column (#567) + +* Updated API for `set_gtsummary_theme()`. Users no longer need call `set_gtsummary_theme(theme_gtsummary_journal())`; rather, they call `theme_gtsummary_journal()`. Each built-in theme now has an argument `set_theme = TRUE`. When `FALSE`, the theme function will invisibly return the named list the theme elements, and the theme will not be set. (#522) + * Bug fix when only categorical summary statistics were requested for continuous variables in `tbl_summary()` and `tbl_svysummary()` (#528) * Added `tbl_svysummary()` function to summarize complex and weighted survey designs. `tbl_svysummary` is now its own class that works with `add_n()`, `add_p()`,