Skip to content

Commit 0f92924

Browse files
authored
as_tabyl() retains tabyl_type attribute if input is a tabyl (#524)
fix #523 by tweaking as_tabyl()
1 parent 230d32e commit 0f92924

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ These are all minor breaking changes resulting from enhancements and are not exp
6363

6464
* `adorn_ns()` can act on a single-column data.frame input with custom Ns supplied if the variable to adorn is specified with `...` (#456).
6565

66+
* `adorn_totals()` on a one_way tabyl preserves the `tabyl_type` attribute so that a subsequent call to `adorn_pct_formatting()` works correctly on one-way tabyls (#523).
67+
6668
# janitor 2.1.0 (2021-01-05)
6769

6870
## New features

R/adorn_totals.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ adorn_totals <- function(dat, where = "row", fill = "-", na.rm = TRUE, name = "T
5959
if ("grouped_df" %in% class(dat)) {
6060
dat <- dplyr::ungroup(dat)
6161
}
62-
dat <- as_tabyl(dat)
62+
63+
dat <- as_tabyl(dat) # even a tabyl needs to be recast as a tabyl to reset the core in case it's been sorted
6364

6465
# set totals attribute
6566
if (sum(where %in% attr(dat, "totals")) > 0) { # if either of the values of "where" are already in totals attribute

R/as_and_untabyl.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ as_tabyl <- function(dat, axes = 2, row_var_name = NULL, col_var_name = NULL) {
4747
attr(dat, "core") <- as.data.frame(dat) # core goes first so dat does not yet have attributes attached to it
4848
}
4949

50-
attr(dat, "tabyl_type") <- dplyr::case_when(
51-
axes == 1 ~ "one_way",
52-
axes == 2 ~ "two_way"
53-
)
50+
attr(dat, "tabyl_type") <- ifelse(
51+
!is.null(attr(dat, "tabyl_type")),
52+
attr(dat, "tabyl_type"), # if a one_way tabyl has as_tabyl called on it, it should stay a one_way #523
53+
dplyr::case_when(
54+
axes == 1 ~ "one_way",
55+
axes == 2 ~ "two_way"
56+
))
5457
class(dat) <- c("tabyl", setdiff(class(dat), "tabyl"))
5558

5659
if (!missing(row_var_name) | !missing(col_var_name)) {

tests/testthat/test-adorn-totals.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,14 @@ test_that("supplying NA as fill still works with non-character first col and num
401401
expect_equal(test_df[1:3, 2:7], out[1:3,2:7], ignore_attr = TRUE)
402402
})
403403

404+
test_that("one_way tabyl inputs retain that class", {
405+
expect_equal(
406+
attr(mtcars %>% tabyl(am) %>% adorn_totals("both"), "tabyl_type"),
407+
"one_way"
408+
)
409+
})
410+
411+
404412
# Tests from #413, different values for row and col names
405413
test_that("long vectors are trimmed", {
406414
expect_equal(

0 commit comments

Comments
 (0)