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

#567 fix for ordered factors in tbl_summary #568

Merged
merged 12 commits into from
Jul 20, 2020
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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()`,
Expand Down
10 changes: 5 additions & 5 deletions R/tbl_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +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 -------------------------------
data <- dplyr::mutate_if(data,
~inherits(., "ordered") && inherits(., "factor"),
~factor(., levels = attr(., "levels"), ordered = FALSE))

# deleting obs with missing by values ----------------------------------------
# saving variable labels
Expand All @@ -211,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]]
}
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-add_overall.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ 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
)
})

test_that("no errors/warnings with standard use for tbl_svysummary", {
t <- trial %>%
survey::svydesign(data = ., ids = ~ 1, weights = ~ 1) %>%
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-tbl_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})