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

clean_names() support for all types #483

Merged
merged 4 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
clean_names() support for all types
with some tests and a bullet in news
  • Loading branch information
DanChaltiel committed May 15, 2022
commit 3fe4a0b8d073632b5893e12f407ab2099a03d9c6
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

S3method(chisq.test,default)
S3method(chisq.test,tabyl)
S3method(clean_names,data.frame)
S3method(clean_names,default)
S3method(clean_names,sf)
S3method(clean_names,tbl_graph)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

* `make_clean_names()` (and therefore `clean_names()`) issues a warning if the mu or micro symbol is in the names and it is not or may not be handled by a `replace` argument value. (#448, thanks **@IndrajeetPatil** for reporting and **@billdenney** for fixing) The rationale is that standard transliteration would convert "[mu]g" to "mg" when it would be more typically be converted to "ug" for use as a unit. A new, unexported constant (janitor:::mu_to_u) was added to help with mu to "u" replacements.

* `clean_names()` now supports all object types that have either names or dimnames (#481, @DanChaltiel).

## Bug fixes

* When a numeric variable is supplied as the 2nd variable (column) or 3rd variable (list) of a `tabyl`, the resulting columns or list are now sorted in numeric order, not alphabetic. (#438, thanks **@daaronr** for reporting and **@mattroumaya** for fixing)
Expand Down
24 changes: 14 additions & 10 deletions R/clean_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,23 @@ clean_names <- function(dat, ...) {
UseMethod("clean_names")
}

#' @rdname clean_names
#' @export
clean_names.data.frame <- function(dat, ...) {
stats::setNames(dat, make_clean_names(names(dat), ...))
}

#' @rdname clean_names
#' @export
clean_names.default <- function(dat, ...) {
stop(
"No `clean_names()` method exists for the class ", paste(class(dat), collapse=", "),
"\nConsider janitor::make_clean_names() for other cases of manipulating vectors of names."
)
if(is.null(names(dat)) && is.null(dimnames(dat))) {
stop(
"`clean_names()` requires that either names or dimnames be non-null.",
call. = FALSE
)
return(dat)
DanChaltiel marked this conversation as resolved.
Show resolved Hide resolved
}
if(is.null(names(dat))) {
colnames(dat) <- make_clean_names(colnames(dat), ...)
rownames(dat) <- make_clean_names(rownames(dat), ...)
} else {
names(dat) <- make_clean_names(names(dat), ...)
}
dat
}

#' @rdname clean_names
Expand Down
3 changes: 0 additions & 3 deletions man/clean_names.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions tests/testthat/test-clean-names.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,23 @@ test_that("Tests for cases beyond default snake", {
expect_warning(expect_equal(names(clean_names(test_df, "small_camel")), names(clean_names(test_df, "lower_camel"))))
})

test_that("errors if not called on a data.frame", {
test_that("Tests for clean_names.default() on lists and vectors", {
test_v <- seq_along(testing_vector)
names(test_v) <- testing_vector
test_list <- as.list(test_v)

# Warnings due to partially handled mu
clean_v <- suppressWarnings(clean_names(test_v))
DanChaltiel marked this conversation as resolved.
Show resolved Hide resolved
clean_l <- suppressWarnings(clean_names(test_list))
expect_equal(names(clean_v)[1], "sp_ace")
expect_equal(names(clean_l)[1], "sp_ace")
expect_type(clean_v, "integer")
expect_type(clean_l, "list")

unnamed <- seq_along(testing_vector)
expect_error(
clean_names(1:3),
regexp="No `clean_names()` method exists for the class integer",
clean_names(unnamed),
regexp="requires that either names or dimnames be non-null.",
fixed=TRUE
)
})
Expand Down