Skip to content

Commit

Permalink
Remove various instances of testing equality of object class via == i…
Browse files Browse the repository at this point in the history
…nstead of inherits

When using getS3method() with an object with multiple classes, tests will error under some circumstances due to this function being designed to test only a single class at a time. This is probably good (albeit cryptic) since I would otherwise be missing out on instances where there actually is a matching method but just not to the first string in class(object)
  • Loading branch information
jacob-long committed Jan 7, 2024
1 parent 9bb7074 commit b055040
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion R/cat_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ cat_plot <- function(model, pred, modx = NULL, mod2 = NULL,
pred.labels = pred.labels,
modx.labels = modx.labels, mod2.labels = mod2.labels,
facet.modx = FALSE, d = d,
survey = "svyglm" %in% class(model), weights = weights,
survey = inherits(model, "svyglm"), weights = weights,
preds.per.level = 100,
partial.residuals = partial.residuals, at = at, ...)

Expand Down
2 changes: 1 addition & 1 deletion R/int_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ prep_data <- function(model, d, pred, modx, mod2, pred.values = NULL,
resp <- jtools::get_response_name(model, ...)

# Create a design object
design <- if ("svyglm" %in% class(model)) {
design <- if (inherits(model, "svyglm")) {
model$survey.design
} else {
NULL
Expand Down
2 changes: 1 addition & 1 deletion R/simple_margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ sim_margins <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
ss <- structure(ss, digits = digits)

d <- get_data(model)
if (is_survey <- "svyglm" %in% class(model)) {
if (is_survey <- inherits(model, "svyglm")) {
design <- model$survey.design
} else {design <- NULL}
# Which variables are factors?
Expand Down
14 changes: 8 additions & 6 deletions R/simple_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
ss <- structure(ss, digits = digits)

d <- get_data(model)
if (is_survey <- "svyglm" %in% class(model)) {
if (is_survey <- inherits(model, "svyglm")) {
design <- model$survey.design
} else {design <- NULL}
# Which variables are factors?
Expand Down Expand Up @@ -336,8 +336,10 @@ 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 <- !is.null(getS3method("summ", class(model), optional = TRUE))
# Need proper name for test statistic\
has_summ <- any(sapply(class(model), function(x) {
!is.null(getS3method("summ", x, optional = TRUE))
}))
tcol <- try(colnames(summary(model)$coefficients)[3], silent = TRUE)
if (!is.null(tcol) & class(tcol) != "try-error") {
tcol <- gsub("value", "val.", tcol)
Expand Down Expand Up @@ -786,7 +788,7 @@ print.sim_slopes <- function(x, ...) {
m$ints <- m$ints[names(m$ints) %nin% unlist(make_ci_labs(x$ci.width))]
}

if (class(x$mod2.values) != "character") {
if (!inherits(x$mod2.values, "character")) {
x$mod2.values <- format(x$mod2.values, nsmall = x$digits)
}

Expand Down Expand Up @@ -838,7 +840,7 @@ print.sim_slopes <- function(x, ...) {

for (i in seq_along(x$modx.values)) {

if (class(x$modx.values) != "character") {
if (!inherits(x$modx.values, "character")) {
x$modx.values <- format(x$modx.values, nsmall = x$digits)
}

Expand Down Expand Up @@ -943,7 +945,7 @@ tidy.sim_slopes <- function(x, conf.level = .95, ...) {
base$conf.high <- all_slopes[,make_ci_labs(conf.level)[[2]]]
} else { # If not, calculate them
alpha <- (1 - conf.level) / 2
crit_t <- if (class(x$mods[[1]]) == "lm") {
crit_t <- if (inherits(x$mods[[1]], "lm")) {
abs(qt(alpha, df = df.residual(x$mods[[1]])))
} else {
abs(qnorm(alpha))
Expand Down

0 comments on commit b055040

Please sign in to comment.