Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/plot_model_diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ plot.model_diagnostics <- function(x, ..., variable = "y_hat", yvariable = "resi
class(all_models) <- "data.frame"
nlabels <- length(unique(all_models$label))

pl <- ggplot(all_models, aes_string(x = variable, y = yvariable, color = "label", group = "label")) +
pl <- ggplot(all_models, aes(x = .data[[variable]], y = .data[[yvariable]], color = .data[["label"]], group = .data[["label"]])) +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

color = label etc will do

geom_point(size = 0.1) +
theme_default_dalex() +
scale_color_manual(name = "Model", values = colors_discrete_drwhy(nlabels))
Expand Down
2 changes: 1 addition & 1 deletion R/plot_predict_diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ plot.predict_diagnostics <- function(x, ...) {
statistic <- x$test$statistic
cut_points <- x$cut_points

pl <- ggplot(df, aes_string("Var1", "Freq", fill = "direction")) +
pl <- ggplot(df, aes(x = .data[["Var1"]], y = .data[["Freq"]], fill = .data[["direction"]])) +
geom_col() +
scale_y_continuous("") +
scale_x_discrete("residuals", labels = as.character(cut_points)) +
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test_model_diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ test_that("Print",{
expect_error(print(diag_ranger_3), NA)
})

test_that("Plot does not use deprecated aes_string() (#582)", {
# ggplot2 >= 3.0.0 hard-deprecates aes_string(); plot.model_diagnostics()
# used to call it directly, which raised a `lifecycle::deprecate_warn()`
# every time a diagnostics plot was drawn (see reprex in issue #582).
#
# We check this structurally (source no longer references aes_string())
# rather than by capturing the warning at call time, because lifecycle
# warnings are only emitted once per R session: whichever test runs
# plot.model_diagnostics() first "uses up" the warning for the rest of
# the session/suite, which would make a warning-capturing test silently
# pass regardless of whether the bug is actually fixed.
plot_fun_src <- deparse(body(getS3method("plot", "model_diagnostics")))
expect_false(any(grepl("aes_string", plot_fun_src, fixed = TRUE)))

# the plot itself must still work and produce a ggplot object
expect_is(plot(diag_ranger_1), "gg")
expect_is(plot(diag_ranger_1, diag_lm, variable = "construction.year"), "gg")
})

explainer_array <- explainer_ranger
explainer_array$y_hat <- as.array(explainer_array$y_hat)

Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test_predict_diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ test_that("Output format - plot",{
expect_is(plot(lm_rd), "gg")
})

test_that("Plot does not use deprecated aes_string() (#582)", {
# Same root cause as plot.model_diagnostics(): the histogram branch of
# plot.predict_diagnostics() (used when variables = NULL) also called the
# deprecated aes_string(). See test_model_diagnostics.R for why this is
# checked structurally instead of via expect_no_warning() (lifecycle
# deprecation warnings only fire once per R session).
plot_fun_src <- deparse(body(getS3method("plot", "predict_diagnostics")))
expect_false(any(grepl("aes_string", plot_fun_src, fixed = TRUE)))

ranger_rd_hist <- predict_diagnostics(explainer_classif_ranger, new_observation = titanic_imputed[1, -8], variables = NULL)
expect_is(plot(ranger_rd_hist), "gg")
})


#:# alias

Expand Down