Skip to content

Commit

Permalink
include_reference = TRUE not working when using cut() directly in…
Browse files Browse the repository at this point in the history
… model formula

Fixes #1025
  • Loading branch information
strengejacke committed Oct 9, 2024
1 parent aa07893 commit e830a6f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
14 changes: 12 additions & 2 deletions R/utils_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,23 @@

# check if we have model data, else return parameter table
if (is.null(model_data)) {
params
return(params)
}

# find factors and factor levels and check if we have any factors in the data
factors <- .find_factor_levels(model_data, model, model_call = attributes(params)$model_call)
if (!length(factors)) {
params
# in case of "on-the-fly" factors, e.g.:
# m <- lm(mpg ~ cut(wt, c(0, 2.5, 3, 5)), data = mtcars)
# we need to receive the data from the model frame, in order to find factors
model_data <- insight::get_data(model, source = "mf", verbose = FALSE)
if (!is.null(model_data)) {
factors <- .find_factor_levels(model_data, model, model_call = attributes(params)$model_call)
}
# if we still didn't find anything, quit...
if (!length(factors)) {
return(params)
}
}

# we need some more information about prettified labels etc.
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-pipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ test_that("print in pipe", {
"Species [setosa] | 0.00 | | | | "
)
})

test_that("print in pipe, on-the-fly factor", {
data(mtcars)
out <- capture.output({
mtcars |>
lm(mpg ~ cut(wt, c(0, 2.5, 3, 5)), data = _) |>
model_parameters(include_reference = TRUE)
})
expect_identical(
out[4],
"cut(wt, c(0, 2.5, 3, 5)) [(0,2.5]] | 0.00 | | | | "
)
})

0 comments on commit e830a6f

Please sign in to comment.