Skip to content

Commit

Permalink
New and improved way to check for summ method
Browse files Browse the repository at this point in the history
summ.merMod is never found by getS3method() because none of the merMod objects have merMod in class()
  • Loading branch information
jacob-long committed Jan 7, 2024
1 parent 58efd83 commit f726fb2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 1 addition & 3 deletions R/simple_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,7 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
# Since output columns are conditional, I call summ here to see what they will
# be. I set vifs = FALSE to make sure it isn't fit due to user options.
# Need proper name for test statistic\
has_summ <- any(sapply(class(model), function(x) {
!is.null(getS3method("summ", x, optional = TRUE))
}))
has_summ <- check_method(summ, model)
tcol <- try(colnames(summary(model)$coefficients)[3], silent = TRUE)
if (!is.null(tcol) & class(tcol) != "try-error") {
tcol <- gsub("value", "val.", tcol)
Expand Down
16 changes: 16 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,19 @@ j_update <- function(mod, formula = NULL, data = NULL, offset = NULL,

eval(call, env, call.env)
}

# adapted from https://stackoverflow.com/a/42742370
# Looking for whether a method is defined for a given object (...)
# getS3method() doesn't work for something like merMod because the string
# "merMod" is not in the vector returned by class()
check_method <- function(generic, ...) {
ch <- deparse(substitute(generic))
f <- X <- function(x, ...) UseMethod("X")
for(m in methods(ch)) assign(sub(ch, "X", m, fixed = TRUE), "body<-"(f, value = m))
tryCatch({
X(...)
TRUE
}, error = function(e) {
FALSE
})
}

0 comments on commit f726fb2

Please sign in to comment.