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

get_datagrid() gives a more informative error message when a variable #995

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 1.0.1.3
Version: 1.0.1.4
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# insight 1.02

## Changes

* `get_datagrid()` gives a more informative error message when a variable
specified in `by` was not found in the data.

## Bug fixes

* Option `"terciles"` and `"terciles2"` in `get_datagrid()` were swapped, i.e.
Expand Down
6 changes: 6 additions & 0 deletions R/get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@

#' @rdname get_datagrid
#' @export
get_datagrid.data.frame <- function(x,

Check warning on line 197 in R/get_datagrid.R

View workflow job for this annotation

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

file=R/get_datagrid.R,line=197,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 50 to at most 40.
by = "all",
factors = "reference",
numerics = "mean",
Expand Down Expand Up @@ -262,6 +262,12 @@
specs$varname <- as.character(specs$varname) # make sure it's a string not fac
specs <- specs[!duplicated(specs$varname), ] # Drop duplicates

# sanity check - target in data?
if (!all(specs$varname %in% colnames(x))) {
format_error(paste0(
"Variable `", setdiff(specs$varname, colnames(x))[1], "` was not found in the data. Please check the spelling." # nolint
))
}
specs$is_factor <- vapply(x[specs$varname], function(x) is.factor(x) || is.character(x), TRUE)

# Create target list of factors -----------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,13 @@ test_that("get_datagrid - include_random works with interacting random effects",
tolerance = 1e-3
)
})


test_that("get_datagrid - informative error when by not found", {
data(iris)
m <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)
expect_error(
insight::get_datagrid(m, by = list(Sepal.Something = c(10, 40, 50))),
regex = "was not found"
)
})
Loading