From f149936f8714c6832654242f42ac83c0ed3e0801 Mon Sep 17 00:00:00 2001 From: Jacob Long Date: Sun, 28 Jul 2024 23:02:49 -0400 Subject: [PATCH] Fix problem with merModTest, make broom hard dependency --- DESCRIPTION | 2 +- R/simple_slopes.R | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 980338e..11ed22b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,11 +20,11 @@ Imports: ggplot2 (>= 3.4.0), cli, generics, + broom, jtools (>= 2.0.3), rlang (>= 0.3.0), tibble Suggests: - broom, cowplot, broom.mixed, glue, diff --git a/R/simple_slopes.R b/R/simple_slopes.R index 25aa93b..c509705 100644 --- a/R/simple_slopes.R +++ b/R/simple_slopes.R @@ -243,20 +243,28 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL, c(sapply(pred_names, function(x) paste0(x, ulevels(d[[pred]])))) } } - tidied <- tryCatch(generics::tidy(model), - error = function(x) stop_wrap("tidy() could not find a method for this kind - of model. If you are using a package like glmmTMB or other mixed modeling - packages, install and load the broom.mixed package and try again. Make sure - you have the broom package installed and loaded otherwise.")) + tidied <- try(generics::tidy(model), silent = TRUE) + + if (inherits(tidied, "try-error")) { + if (rlang::is_installed("broom.mixed")) { + tidied <- try(broom.mixed::tidy(model), silent = TRUE) + } + } + + if (inherits(tidied, "try-error")) { + stop_wrap("tidy() could not find a method for this kind of model. If you + are using a package like glmmTMB or other mixed modeling packages, install + and load the broom.mixed package and try again.") + } pred_names <- pred_names %just% tidied$term if (length(pred_names) == 0) { stop_wrap("Could not find the focal predictor in the model. If it was transformed (e.g. logged or turned into a factor), put the transformation into the 'pred' argument. For example, - pred = 'as.numeric(x)'.") + pred = 'log(x)'.") + } } - } wname <- get_weights(model, d)$weights_name wts <- get_weights(model, d)$weights