From fda3383e4bd4162777f84113b61f5a18047bf68d Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 28 Nov 2024 13:50:21 +0100 Subject: [PATCH 1/3] Check column names of dataframes? Fixes #276 --- DESCRIPTION | 2 +- R/adjust.R | 8 ++------ R/utils.R | 16 ++++++++++++++++ tests/testthat/test-adjust.R | 10 ++++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b75f96278..588bfd4eb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.13.0.15 +Version: 0.13.0.16 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531")), diff --git a/R/adjust.R b/R/adjust.R index 5d50b16d0..9cd5805e9 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -75,12 +75,8 @@ adjust <- function(data, ignore_case = FALSE, regex = FALSE, verbose = FALSE) { - if (!all(colnames(data) == make.names(colnames(data), unique = TRUE))) { - insight::format_warning( - "Bad column names (e.g., with spaces) have been detected which might create issues in many functions.", - "Please fix it (you can run `names(mydata) <- make.names(names(mydata))` for a quick fix)." - ) - } + # make sure column names are syntactically valid + .check_dataframe_names(data, action = "error") # check for formula notation, convert to character vector if (inherits(effect, "formula")) { diff --git a/R/utils.R b/R/utils.R index 25275fee5..c822f3b63 100644 --- a/R/utils.R +++ b/R/utils.R @@ -47,6 +47,22 @@ } +#' Checks data framesfor syntactically valid column names +#' Argument "action" can be "warning", "error", or "message". +#' +#' @keywords internal +#' @noRd +.check_dataframe_names <- function(x, action = "warning", verbose = TRUE) { + if (verbose && !all(colnames(x) == make.names(colnames(x), unique = TRUE))) { + insight::format_alert( + "Bad column names (e.g., with spaces) have been detected which might create issues in many functions.", + "Please fix it (you can run `names(mydata) <- make.names(names(mydata))` or use `janitor::clean_names()` for a quick fix).", # nolint + type = action + ) + } +} + + #' Fuzzy grep, matches pattern that are close, but not identical #' @examples #' colnames(iris) diff --git a/tests/testthat/test-adjust.R b/tests/testthat/test-adjust.R index 44e5e8dc2..c5a3364ee 100644 --- a/tests/testthat/test-adjust.R +++ b/tests/testthat/test-adjust.R @@ -28,3 +28,13 @@ test_that("adjust regex", { adjust(mtcars, select = "mpg") ) }) + +# select helpers ------------------------------ +test_that("adjust, invalid column names", { + data(iris) + colnames(iris)[1] <- "I am" + expect_error( + adjust(iris[c("I am", "Species")], multilevel = FALSE, bayesian = FALSE), + regex = "Bad column names" + ) +}) From d1dafff81a8f6c506f0a6d6b51d3c9e886412e71 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 28 Nov 2024 16:46:08 +0100 Subject: [PATCH 2/3] Update R/utils.R Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index c822f3b63..d64538e52 100644 --- a/R/utils.R +++ b/R/utils.R @@ -47,7 +47,7 @@ } -#' Checks data framesfor syntactically valid column names +#' Checks dataframes for syntactically valid column names #' Argument "action" can be "warning", "error", or "message". #' #' @keywords internal From 5c9d3f7cfa68bbb2fc9f1bdb23a81c9b259dd373 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 29 Nov 2024 08:35:31 +0100 Subject: [PATCH 3/3] revise wording --- R/utils.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index d64538e52..97f6d922b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -56,7 +56,14 @@ if (verbose && !all(colnames(x) == make.names(colnames(x), unique = TRUE))) { insight::format_alert( "Bad column names (e.g., with spaces) have been detected which might create issues in many functions.", - "Please fix it (you can run `names(mydata) <- make.names(names(mydata))` or use `janitor::clean_names()` for a quick fix).", # nolint + paste0( + "We recommend to rename following columns: ", + text_concatenate( + colnames(x)[colnames(x) != make.names(colnames(x), unique = TRUE)], + enclose = "`" + ) + ), + "You can run `names(mydata) <- make.names(names(mydata))` or use `janitor::clean_names()` for a quick fix.", # nolint type = action ) }