Skip to content

Commit

Permalink
Add support for displaying the label attribute for column name.
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed May 23, 2024
1 parent 49acf01 commit 6c4a196
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ These are all minor breaking changes resulting from enhancements and are not exp

## New features

* `tabyl()` now defaults to displaying the label attribute for the column name (@olivroy, #394).

* A new function `paste_skip_na()` pastes without including NA values (#537).

* `row_to_names()` now accepts multiple rows as input, and merges them using a new `sep` argument (#536). The default is `sep = "_"`. When handling multiple `NA` values, `row_to_names()` ignores them and only merges non-NA values for column names. When all values are `NA`, `row_to_names()` creates a column name of `"NA"`, a character, rather than `NA`.
Expand Down
18 changes: 14 additions & 4 deletions R/tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ tabyl.default <- function(dat, show_na = TRUE, show_missing_levels = TRUE, ...)
} else {
var_name <- names(dat)
}



# useful error message if input vector doesn't exist
if (is.null(dat)) {
stop(paste0("object ", var_name, " not found"))
Expand All @@ -75,6 +74,13 @@ tabyl.default <- function(dat, show_na = TRUE, show_missing_levels = TRUE, ...)
if (length(var_name) > 1) {
var_name <- paste(var_name, collapse = "")
}

# Try to retrieve label
if (is.data.frame(dat)) {
var_label <- attr(dat[, var_name], "label", exact = TRUE) %||% var_name
} else {
var_label <- attr(dat, "label", exact = TRUE) %||% var_name
}

# if show_na is not length-1 logical, error helpfully (#377)
if (length(show_na) > 1 || !inherits(show_na, "logical")) {
Expand Down Expand Up @@ -133,8 +139,8 @@ tabyl.default <- function(dat, show_na = TRUE, show_missing_levels = TRUE, ...)
dplyr::mutate(percent = n / sum(n, na.rm = TRUE)) # recalculate % without NAs
}

# reassign correct variable name
names(result)[1] <- var_name
# reassign correct variable name (or label if it exists)
names(result)[1] <- var_label

# in case input var name was "n" or "percent", call helper function to set unique names
result <- handle_if_special_names_used(result)
Expand Down Expand Up @@ -169,6 +175,10 @@ tabyl.data.frame <- function(dat, var1, var2, var3, show_na = TRUE, show_missing
}
}

get_label <- function(x) {
attr(x, "label", exact = TRUE)
}

# a one-way frequency table; this was called "tabyl" in janitor <= 0.3.0
tabyl_1way <- function(dat, var1, show_na = TRUE, show_missing_levels = TRUE) {
x <- dplyr::select(dat, !!var1)
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,21 @@ test_that("3-way tabyl with 3rd var factor is listed in right order, #250", {
expect_equal(names(tabyl(z, am, gear, cyl)), c("8", "6", "NA_"))
})

test_that("tabyl works with label attributes (#394)", {
mt_label <- mtcars
attr(mt_label$cyl, "label") <- "Number of cyl"
tab <- tabyl(mt_label, cyl)
expect_named(
tab,
c("Number of cyl", "n", "percent")
)
tab2 <- tabyl(mt_label, cyl, am)
expect_named(
tab2,
c("Number of cyl", "0", "1")
)
})

test_that("tabyl works with ordered 1st variable, #386", {
mt_ordered <- mtcars
mt_ordered$cyl <- ordered(mt_ordered$cyl, levels = c("4", "8", "6"))
Expand Down

0 comments on commit 6c4a196

Please sign in to comment.