Skip to content

Commit

Permalink
Check column names of dataframes? (#567)
Browse files Browse the repository at this point in the history
* Check column names of dataframes?
Fixes #276

* Update R/utils.R

Co-authored-by: Etienne Bacher <[email protected]>

* revise wording

---------

Co-authored-by: Etienne Bacher <[email protected]>
  • Loading branch information
strengejacke and etiennebacher authored Dec 13, 2024
1 parent 25f8ec4 commit af076d2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.13.0.17
Version: 0.13.0.18
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531")),
Expand Down
8 changes: 2 additions & 6 deletions R/adjust.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
23 changes: 23 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@
}


#' Checks dataframes for 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.",
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
)
}
}


#' Fuzzy grep, matches pattern that are close, but not identical
#' @examples
#' colnames(iris)
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-adjust.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})

0 comments on commit af076d2

Please sign in to comment.