From 7c317256364e655c31fb5010e0aa490e5dc451a1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 24 Jul 2024 11:13:01 +0200 Subject: [PATCH] `ellipses_info()` doesn't work with `do.call()` Fixes #778 --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/ellipsis_info.R | 19 +++++++++++++++++++ tests/testthat/test-ellipses_info.R | 19 +++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7e07b8de6..d7ff0ae54 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: insight Title: Easy Access to Model Information for Various Model Objects -Version: 0.20.2.7 +Version: 0.20.2.9 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NEWS.md b/NEWS.md index 133f3dd07..ebf59ee22 100644 --- a/NEWS.md +++ b/NEWS.md @@ -19,6 +19,8 @@ * Fixed issue in `get_modelmatrix()` for models from package *brms* with special functions in the formula (like `mo()`). +* Fixed issue in `ellipses_info()` when this function was called from `do.call()`. + # insight 0.20.2 ## New supported models diff --git a/R/ellipsis_info.R b/R/ellipsis_info.R index ae0094b89..f0c86c20f 100644 --- a/R/ellipsis_info.R +++ b/R/ellipsis_info.R @@ -38,6 +38,25 @@ ellipsis_info.default <- function(..., only_models = TRUE, verbose = TRUE) { # Create list with names model_objects <- list(...) object_names <- match.call(expand.dots = FALSE)[["..."]] + # fix names - if "..." is a list of models, the name is of type "language" + # and we coerce to character here + object_names <- lapply(object_names, function(i) { + if (is.language(i)) { + safe_deparse(i) + } else { + # all other classes are unchanged + i + } + }) + # now check if we have character names. If `ellipses_info()` is called + # via `do.call()`, we have the model objects instead of their names (see #778) + # and we then use fixed names + if (!all(vapply(object_names, is.character, logical(1)))) { + object_names <- paste0("model", seq_along(model_objects)) + } else if (is.list(object_names)) { + # convert list of characters into regular character vector + object_names <- unlist(object_names, use.names = FALSE) + } names(model_objects) <- object_names # If only one object was provided, check if it is a list of models, like "list(m1, m2)" diff --git a/tests/testthat/test-ellipses_info.R b/tests/testthat/test-ellipses_info.R index 1a13cd744..371f8fd1a 100644 --- a/tests/testthat/test-ellipses_info.R +++ b/tests/testthat/test-ellipses_info.R @@ -102,3 +102,22 @@ test_that("ellipses_info, random effects", { expect_true(attributes(info)$re_nested_increasing) expect_true(attributes(info)$re_nested_decreasing) }) + +test_that("ellipses_info, do.call", { + data(iris) + lm1 <- lm(Sepal.Length ~ Species, data = iris) + lm2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris) + lm3 <- lm(Sepal.Length ~ Species * Petal.Length, data = iris) + + out <- do.call(ellipsis_info, list(lm1, lm2, lm3, only_models = TRUE)) + expect_length(out, 3) + expect_named(out, c("model1", "model2", "model3")) + + out <- ellipsis_info(list(lm1, lm2, lm3), only_models = TRUE) + expect_length(out, 3) + expect_named(out, c("lm1", "lm2", "lm3")) + + out <- ellipsis_info(lm1, lm2, lm3, only_models = TRUE) + expect_length(out, 3) + expect_named(out, c("lm1", "lm2", "lm3")) +})