diff --git a/R/plot_model_diagnostics.R b/R/plot_model_diagnostics.R index 587277a2..fb0d73c3 100644 --- a/R/plot_model_diagnostics.R +++ b/R/plot_model_diagnostics.R @@ -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"]])) + geom_point(size = 0.1) + theme_default_dalex() + scale_color_manual(name = "Model", values = colors_discrete_drwhy(nlabels)) diff --git a/R/plot_predict_diagnostics.R b/R/plot_predict_diagnostics.R index 2db5f3ca..79aee132 100644 --- a/R/plot_predict_diagnostics.R +++ b/R/plot_predict_diagnostics.R @@ -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)) + diff --git a/tests/testthat/test_model_diagnostics.R b/tests/testthat/test_model_diagnostics.R index de729640..c59fff87 100644 --- a/tests/testthat/test_model_diagnostics.R +++ b/tests/testthat/test_model_diagnostics.R @@ -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) diff --git a/tests/testthat/test_predict_diagnostics.R b/tests/testthat/test_predict_diagnostics.R index f3730a7d..7cf56c72 100644 --- a/tests/testthat/test_predict_diagnostics.R +++ b/tests/testthat/test_predict_diagnostics.R @@ -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