From d6a17f37bfceb3cc010ccecb0870f1bc80fb5ef2 Mon Sep 17 00:00:00 2001 From: billdenney Date: Thu, 19 Dec 2024 02:43:00 +0000 Subject: [PATCH] Style code (GHA) --- R/clean_names.R | 15 +++++++-------- tests/testthat/test-clean-names.R | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/R/clean_names.R b/R/clean_names.R index 388144d..abdcd73 100644 --- a/R/clean_names.R +++ b/R/clean_names.R @@ -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 @@ -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 } @@ -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]] } } @@ -131,7 +130,7 @@ clean_names.tbl_graph <- function(dat, ...) { call. = FALSE ) } # nocov end - + dplyr::rename_all(dat, .funs = make_clean_names, ...) } diff --git a/tests/testthat/test-clean-names.R b/tests/testthat/test-clean-names.R index 15bb942..bc4e704 100644 --- a/tests/testthat/test-clean-names.R +++ b/tests/testthat/test-clean-names.R @@ -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")) @@ -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")) }