Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 21, 2024
1 parent 01aca65 commit 9bd1f6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
10 changes: 6 additions & 4 deletions R/standardize.models.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ standardize.default <- function(x,
}

# check model formula. Some notations don't work when standardizing data
if (!insight::formula_ok(x, verbose = verbose)) {
insight::format_alert(insight::color_text("Model cannot be standardized.", "red"))
return(x)
}
insight::formula_ok(
x,
action = "error",
prefix_msg = "Model cannot be standardized.",
verbose = verbose
)

data_std <- NULL # needed to avoid note
.standardize_models(x,
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-standardize_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ test_that("standardize | problematic formulas", {

colnames(mtcars)[1] <- "1_mpg"
m <- lm(`1_mpg` ~ hp, data = mtcars)
expect_message(expect_warning(standardise(m), regex = "Looks like"))
expect_error(standardise(m), regex = "Looks like")

data(mtcars)
m <- lm(mtcars$mpg ~ mtcars$hp)
expect_message(expect_warning(standardise(m), regex = "model formulas"))
# works interactive only
# data(mtcars)
# m <- lm(mtcars$mpg ~ mtcars$hp)
# expect_error(standardise(m), regex = "model formulas")

m <- lm(mtcars[, 1] ~ hp, data = mtcars)
expect_message(expect_warning(standardise(m), regex = "indexed data"))
expect_error(standardise(m), regex = "indexed data")
})


Expand Down Expand Up @@ -228,15 +229,14 @@ test_that("standardize non-Gaussian response", {
# variables evaluated in the environment $$$ ------------------------------
test_that("variables evaluated in the environment", {
m <- lm(mtcars$mpg ~ mtcars$cyl + am, data = mtcars)
w <- capture_warnings(standardize(m))
expect_true(any(grepl("mtcars$mpg", w, fixed = TRUE)))
w <- capture_error(standardize(m))
expect_true(any(grepl("Using `$`", w, fixed = TRUE)))

## Note:
# No idea why this is suddenly not giving a warning on older R versions.
m <- lm(mtcars$mpg ~ mtcars$cyl + mtcars$am, data = mtcars)
warns <- capture_warnings(standardize(m))
expect_true(any(grepl("mtcars$mpg", warns, fixed = TRUE)))
expect_true(any(grepl("No variables", warns, fixed = TRUE)))
w <- capture_error(standardize(m))
expect_true(any(grepl("Using `$`", w, fixed = TRUE)))
})


Expand Down

0 comments on commit 9bd1f6b

Please sign in to comment.