Skip to content

Commit

Permalink
Merge branch '240-breaking-off-tabyl-and-adorn_-into-their-own-packag…
Browse files Browse the repository at this point in the history
…e' of https://github.com/sfirke/janitor into 240-breaking-off-tabyl-and-adorn_-into-their-own-package
  • Loading branch information
billdenney committed Dec 19, 2024
2 parents d9c50ba + d6a17f3 commit 886aefa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
15 changes: 7 additions & 8 deletions R/clean_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' support using `clean_names()` on `sf` and `tbl_graph` (from
#' `tidygraph`) objects as well as on database connections through
#' `dbplyr`. For cleaning other named objects like named lists
#' and vectors, use `make_clean_names()`. When `set_labels` is set to `TRUE`, the old names,
#' and vectors, use `make_clean_names()`. When `set_labels` is set to `TRUE`, the old names,
#' stored as column labels, can be restored using `sjlabelled::label_to_colnames()`.
#'
#' @export
Expand Down Expand Up @@ -83,14 +83,13 @@ clean_names.default <- function(dat, ..., set_labels = FALSE) {
if (is.null(names(dat))) {
dimnames(dat) <- lapply(dimnames(dat), make_clean_names, ...)
} else {
if (set_labels){
if (set_labels) {
old_names <- names(dat)
for (i in seq_along(old_names)){
for (i in seq_along(old_names)) {
attr(dat[[i]], "label") <- old_names[[i]]
}
}
names(dat) <- make_clean_names(names(dat), ...)

}
dat
}
Expand All @@ -112,9 +111,9 @@ clean_names.sf <- function(dat, ..., set_labels = FALSE) {
sf_cleaned <- make_clean_names(sf_names[cols_to_rename], ...)
# rename original df
names(dat)[cols_to_rename] <- sf_cleaned
if(set_labels){
for (i in seq_along(sf_names[cols_to_rename])){

if (set_labels) {
for (i in seq_along(sf_names[cols_to_rename])) {
attr(dat[[i]], "label") <- sf_names[[i]]
}
}
Expand All @@ -131,7 +130,7 @@ clean_names.tbl_graph <- function(dat, ...) {
call. = FALSE
)
} # nocov end

dplyr::rename_all(dat, .funs = make_clean_names, ...)
}

Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-clean-names.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ test_that("labels are created in default method (feature request #563)", {
dat_df <- dplyr::tibble(`a a` = c(11, 22), `b b` = c(2, 3))
dat_df_clean_labels <- clean_names(dat_df, set_labels = TRUE)
dat_df_clean <- clean_names(dat_df)
for (i in seq_along(names(dat_df))){

for (i in seq_along(names(dat_df))) {
# check that old names are saved as labels when set_labels is TRUE
expect_equal(attr(dat_df_clean_labels[[i]], "label"), names(dat_df)[[i]])
# check that old names are not stored if set_labels is not TRUE
expect_null(attr(dat_df_clean[[i]], "label"))
}
}

# expect names are always cleaned
expect_equal(names(dat_df_clean), c("a_a", "b_b"))
expect_equal(names(dat_df_clean_labels), c("a_a", "b_b"))
Expand Down Expand Up @@ -605,19 +605,19 @@ test_that("Tests for cases beyond default snake for sf objects", {

test_that("labels are created in sf method (feature request #563)", {
skip_if_not_installed("sf")

dat_df <- dplyr::tibble(`a a` = c(11, 22), `b b` = c(2, 3))
dat_sf <- dat_df
dat_sf$x <- c(1,2)
dat_sf$y <- c(1,2)
dat_sf$x <- c(1, 2)
dat_sf$y <- c(1, 2)
dat_sf <- sf::st_as_sf(dat_sf, coords = c("x", "y"))
dat_sf_clean_labels <- clean_names(dat_sf, set_labels = TRUE)
dat_sf_clean <- clean_names(dat_sf)
for (i in seq_along(names(dat_df))){

for (i in seq_along(names(dat_df))) {
# check that old names are saved as labels when set_labels is TRUE
expect_equal(attr(dat_sf_clean_labels[[i]], "label"), names(dat_sf)[[i]])

# check that old names are not stored if set_labels is not TRUE
expect_null(attr(dat_sf_clean[[i]], "label"))
}
Expand Down

0 comments on commit 886aefa

Please sign in to comment.