Skip to content

Commit

Permalink
get_datagrid() gives a more informative error message when a variab…
Browse files Browse the repository at this point in the history
…le (#995)

specified in `by` was not found in the data.
  • Loading branch information
strengejacke authored Jan 15, 2025
1 parent 290e823 commit 6a5f930
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
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 @@ -262,6 +262,12 @@ get_datagrid.data.frame <- function(x,
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"
)
})

0 comments on commit 6a5f930

Please sign in to comment.