Skip to content

Commit

Permalink
more informative error
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Feb 24, 2025
1 parent 6b2f7e7 commit b0fee66
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 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: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 1.0.2.16
Version: 1.0.2.17
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
13 changes: 9 additions & 4 deletions R/get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,14 @@ get_datagrid.data.frame <- function(x,

# sanity check - target in data?
if (!all(specs$varname %in% colnames(x))) {
suggestion <- .misspelled_string(
colnames(x),
setdiff(specs$varname, colnames(x))[1],
default_message = "Please check the spelling."
)
format_error(paste0(
"Variable `", setdiff(specs$varname, colnames(x))[1], "` was not found in the data. Please check the spelling." # nolint
"Variable `", setdiff(specs$varname, colnames(x))[1], "` was not found in the data. ", # nolint
suggestion$msg
))
}
specs$is_factor <- vapply(x[specs$varname], function(x) is.factor(x) || is.character(x), TRUE)
Expand Down Expand Up @@ -791,9 +797,8 @@ get_datagrid.comparisons <- get_datagrid.slopes

if (is.na(by_expression) && is.data.frame(x)) {
if (is.na(varname)) {
format_error(
"Couldn't find which variables were selected in `by`. Check spelling and specification."
)
suggestion <- .misspelled_string(colnames(x), by, default_message = "Check spelling and specification.") # nolint
format_error(paste("Couldn't find which variables were selected in `by`.", suggestion$msg)) # nolint
} else {
x <- x[[varname]]
}
Expand Down
8 changes: 4 additions & 4 deletions R/validate_argument.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ validate_argument <- function(argument, options) {
}


.misspelled_string <- function(source, searchterm, default_message = NULL) {
.misspelled_string <- function(valid_strings, searchterm, default_message = NULL) {
if (is.null(searchterm) || length(searchterm) < 1) {
return(default_message)
}
Expand All @@ -53,12 +53,12 @@ validate_argument <- function(argument, options) {
# init default
msg <- ""
# remove matching strings
same <- intersect(source, searchterm)
same <- intersect(valid_strings, searchterm)
searchterm <- setdiff(searchterm, same)
source <- setdiff(source, same)
valid_strings <- setdiff(valid_strings, same)
# guess the misspelled string
possible_strings <- unlist(lapply(searchterm, function(s) {
source[.fuzzy_grep(source, s)] # nolint
valid_strings[.fuzzy_grep(valid_strings, s)] # nolint
}), use.names = FALSE)
if (length(possible_strings)) {
msg <- "Did you mean "
Expand Down

0 comments on commit b0fee66

Please sign in to comment.