Skip to content

Commit

Permalink
print(include_reference = TRUE) doesn't work with pipes
Browse files Browse the repository at this point in the history
Fixes #923
  • Loading branch information
strengejacke committed Dec 1, 2023
1 parent 242610b commit 61c6843
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: parameters
Title: Processing of Model Parameters
Version: 0.21.3.4
Version: 0.21.3.5
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
Expand Up @@ -11,6 +11,11 @@
* `model_parameters()` for models of package *survey* now gives informative
messages when `bootstrap = TRUE` (which is currently not supported).

## Bug fixes

* `print(include_reference = TRUE)` for `model_parameters()` did not work when
run inside a pipe-chain.

# parameters 0.21.3

## Changes
Expand Down
12 changes: 6 additions & 6 deletions R/methods_ggeffects.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ model_parameters.ggeffects <- function(model, keep = NULL, drop = NULL, verbose

# ignore this string when determining maximum length
poplev <- which(cv %in% c("NA (population-level)", "0 (population-level)"))
if (!insight::is_empty_object(poplev)) {
mcv <- cv[-poplev]
} else {
if (insight::is_empty_object(poplev)) {
mcv <- cv
} else {
mcv <- cv[-poplev]
}

if (!insight::is_empty_object(mcv)) {
cv.space2 <- max(nchar(mcv))
} else {
if (insight::is_empty_object(mcv)) {
cv.space2 <- 0
} else {
cv.space2 <- max(nchar(mcv))
}

adjusted_predictors <- paste0(sprintf("* %*s = %*s", cv.space, cv.names, cv.space2, cv), collapse = "\n")
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
if (is.null(model) || inherits(model, "parameters_model")) {
model <- .safe(get(obj_name, envir = globalenv()))
}
# prevent self reference
if (is.null(model) || inherits(model, "parameters_model")) {
model <- .safe(.dynGet(obj_name))
}
}
model
}
Expand Down
7 changes: 5 additions & 2 deletions R/utils_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,14 @@
# check if we have a model object, else return parameter table
model <- .get_object(params)
if (is.null(model)) {
params
# get data from model call
model_data <- .safe(eval(attributes(params)$model_call$data))
} else {
# get data from model object
model_data <- insight::get_data(model, verbose = FALSE)
}

# check if we have model data, else return parameter table
model_data <- insight::get_data(model, verbose = FALSE)
if (is.null(model_data)) {
params
}
Expand Down
3 changes: 3 additions & 0 deletions R/utils_model_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
# add parameters with value and variable
attr(params, "pretty_labels") <- .format_value_labels(params, model)

# save model call
attr(params, "model_call") <- .safe(insight::get_call(model))

# use tryCatch, these might fail...
attr(params, "test_statistic") <- .safe(insight::find_statistic(model))
attr(params, "log_response") <- .safe(isTRUE(grepl("log", insight::find_transformation(model), fixed = TRUE)))
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-pipe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
skip_on_cran()
skip_if(getRversion() < "4.2.0")

test_that("print in pipe", {
data(iris)
out <- capture.output(lm(Sepal.Length ~ Petal.Length + Species, data = iris) |>
model_parameters() |>
print(include_reference = TRUE))
expect_identical(
out[5],
"Species [setosa] | 0.00 | | | | "
)
})

0 comments on commit 61c6843

Please sign in to comment.