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

include_reference = TRUE not working when using cut() directly in model formula #1026

Merged
merged 1 commit into from
Oct 9, 2024
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
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 Expand Up @@ -526,7 +536,7 @@

# helper to format the header / subheader of different model components --------------

.format_model_component_header <- function(x,

Check warning on line 539 in R/utils_format.R

View workflow job for this annotation

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

file=R/utils_format.R,line=539,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 42 to at most 40.
type,
split_column,
is_zero_inflated,
Expand Down Expand Up @@ -895,7 +905,7 @@
# or edge cases...

#' @keywords internal
.format_columns_multiple_components <- function(x,

Check warning on line 908 in R/utils_format.R

View workflow job for this annotation

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

file=R/utils_format.R,line=908,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 101 to at most 40.
pretty_names,
split_column = "Component",
digits = 2,
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 | | | | "
)
})
Loading