Skip to content

Commit af076d2

Browse files
Check column names of dataframes? (#567)
* 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]>
1 parent 25f8ec4 commit af076d2

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: datawizard
33
Title: Easy Data Wrangling and Statistical Transformations
4-
Version: 0.13.0.17
4+
Version: 0.13.0.18
55
Authors@R: c(
66
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
77
comment = c(ORCID = "0000-0003-1995-6531")),

R/adjust.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,8 @@ adjust <- function(data,
7575
ignore_case = FALSE,
7676
regex = FALSE,
7777
verbose = FALSE) {
78-
if (!all(colnames(data) == make.names(colnames(data), unique = TRUE))) {
79-
insight::format_warning(
80-
"Bad column names (e.g., with spaces) have been detected which might create issues in many functions.",
81-
"Please fix it (you can run `names(mydata) <- make.names(names(mydata))` for a quick fix)."
82-
)
83-
}
78+
# make sure column names are syntactically valid
79+
.check_dataframe_names(data, action = "error")
8480

8581
# check for formula notation, convert to character vector
8682
if (inherits(effect, "formula")) {

R/utils.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@
4747
}
4848

4949

50+
#' Checks dataframes for syntactically valid column names
51+
#' Argument "action" can be "warning", "error", or "message".
52+
#'
53+
#' @keywords internal
54+
#' @noRd
55+
.check_dataframe_names <- function(x, action = "warning", verbose = TRUE) {
56+
if (verbose && !all(colnames(x) == make.names(colnames(x), unique = TRUE))) {
57+
insight::format_alert(
58+
"Bad column names (e.g., with spaces) have been detected which might create issues in many functions.",
59+
paste0(
60+
"We recommend to rename following columns: ",
61+
text_concatenate(
62+
colnames(x)[colnames(x) != make.names(colnames(x), unique = TRUE)],
63+
enclose = "`"
64+
)
65+
),
66+
"You can run `names(mydata) <- make.names(names(mydata))` or use `janitor::clean_names()` for a quick fix.", # nolint
67+
type = action
68+
)
69+
}
70+
}
71+
72+
5073
#' Fuzzy grep, matches pattern that are close, but not identical
5174
#' @examples
5275
#' colnames(iris)

tests/testthat/test-adjust.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@ test_that("adjust regex", {
2828
adjust(mtcars, select = "mpg")
2929
)
3030
})
31+
32+
# select helpers ------------------------------
33+
test_that("adjust, invalid column names", {
34+
data(iris)
35+
colnames(iris)[1] <- "I am"
36+
expect_error(
37+
adjust(iris[c("I am", "Species")], multilevel = FALSE, bayesian = FALSE),
38+
regex = "Bad column names"
39+
)
40+
})

0 commit comments

Comments
 (0)