Skip to content

Commit

Permalink
Merge branch 'main' into strengejacke/issue571
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Dec 13, 2024
2 parents 094190c + af076d2 commit 8a68cf1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
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 8a68cf1

Please sign in to comment.