From 0edd037ddba5313158e1bb655febd5dd31a6f8bc Mon Sep 17 00:00:00 2001 From: billdenney Date: Wed, 18 Dec 2024 21:48:42 +0000 Subject: [PATCH] Style code (GHA) --- R/clean_names.R | 15 ++++++------ tests/testthat/test-clean-names.R | 20 ++++++++-------- vignettes/assertions.Rmd | 40 +++++++++++++++---------------- 3 files changed, 37 insertions(+), 38 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")) } diff --git a/vignettes/assertions.Rmd b/vignettes/assertions.Rmd index 6ee31f9..7099cf9 100644 --- a/vignettes/assertions.Rmd +++ b/vignettes/assertions.Rmd @@ -70,16 +70,16 @@ Using `assert_count()`, you would find this error because of an error raised by ```{r clean_assert} try({ -clean_assert <- - raw %>% - mutate( - test_score = - case_when( - assert_count(student_id == 124 & is.na(test_score)) ~ 84, - assert_count(student_id == 125 & is.na(test_score)) ~ 91, - TRUE ~ test_score - ) - ) + clean_assert <- + raw %>% + mutate( + test_score = + case_when( + assert_count(student_id == 124 & is.na(test_score)) ~ 84, + assert_count(student_id == 125 & is.na(test_score)) ~ 91, + TRUE ~ test_score + ) + ) }) ``` @@ -123,15 +123,15 @@ raw_v2 <- ) try({ -clean_assert <- - raw_v2 %>% - mutate( - test_score = - case_when( - assert_count(student_id == 123 & is.na(test_score)) ~ 84, - assert_count(student_id == 125 & is.na(test_score)) ~ 91, - TRUE ~ test_score - ) - ) + clean_assert <- + raw_v2 %>% + mutate( + test_score = + case_when( + assert_count(student_id == 123 & is.na(test_score)) ~ 84, + assert_count(student_id == 125 & is.na(test_score)) ~ 91, + TRUE ~ test_score + ) + ) }) ```