Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check column names of dataframes? #567

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.15
Version: 0.13.0.16
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 @@ -30,7 +30,7 @@
#'
#' @return A data frame comparable to `data`, with adjusted variables.
#'
#' @examplesIf require("bayestestR", quietly = TRUE) && require("rstanarm", quietly = TRUE) && require("gamm4", quietly = TRUE)

Check warning on line 33 in R/adjust.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/adjust.R,line=33,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 127 characters.

Check warning on line 33 in R/adjust.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/adjust.R,line=33,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 127 characters.
#' adjusted_all <- adjust(attitude)
#' head(adjusted_all)
#' adjusted_one <- adjust(attitude, effect = "complaints", select = "rating")
Expand Down Expand Up @@ -75,12 +75,8 @@
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")
etiennebacher marked this conversation as resolved.
Show resolved Hide resolved

# check for formula notation, convert to character vector
if (inherits(effect, "formula")) {
Expand Down
16 changes: 16 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@
}


#' Checks data framesfor syntactically valid column names
strengejacke marked this conversation as resolved.
Show resolved Hide resolved
#' 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)
Expand Down Expand Up @@ -125,7 +141,7 @@
for (nm in setdiff(names(custom_attr), names(attributes(data.frame())))) {
attr(data, which = nm) <- custom_attr[[nm]]
}
return(data)

Check warning on line 144 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils.R,line=144,col=3,[return_linter] Use implicit return behavior; explicit return() is not needed.
}


Expand Down Expand Up @@ -246,11 +262,11 @@

# Evaluate
if (eval) {
args <- lapply(exprs, function(expr) {

Check warning on line 265 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils.R,line=265,col=5,[object_overwrite_linter] 'args' is an exported object from package 'base'. Avoid re-using such symbols.
eval(parse(text = expr), envir = src)
})
} else {
args <- unname(mget(exprs, envir = as.environment(src)))

Check warning on line 269 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils.R,line=269,col=5,[object_overwrite_linter] 'args' is an exported object from package 'base'. Avoid re-using such symbols.
}

# Create the string(s)
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"
)
})
Loading