diff --git a/R/geom-hex.r b/R/geom-hex.r index 3f786d1080..7d2271a023 100644 --- a/R/geom-hex.r +++ b/R/geom-hex.r @@ -14,6 +14,7 @@ #' @inheritParams geom_point #' @export #' @examples +#' if (!requireNamespace("hexbin", quietly = TRUE)) { #' d <- ggplot(diamonds, aes(carat, price)) #' d + geom_hex() #' @@ -27,6 +28,7 @@ #' d + geom_hex(binwidth = c(1, 1000)) #' d + geom_hex(binwidth = c(.1, 500)) #' } +#' } geom_hex <- function(mapping = NULL, data = NULL, stat = "binhex", position = "identity", ..., diff --git a/R/stat-.r b/R/stat-.r index 5d03ae859a..4e2cecf8b2 100644 --- a/R/stat-.r +++ b/R/stat-.r @@ -97,10 +97,18 @@ Stat <- ggproto("Stat", args <- c(list(data = quote(data), scales = quote(scales)), params) dapply(data, "PANEL", function(data) { scales <- layout$get_scales(data$PANEL[1]) - try_fetch(do.call(self$compute_panel, args), error = function(cnd) { - cli::cli_warn("Computation failed in {.fn {snake_class(self)}}", parent = cnd) - new_data_frame() - }) + try_fetch(do.call(self$compute_panel, args), + error = function(cnd) { + # if the error comes from check_installed(), propagate it immediately. + if (inherits(cnd, "rlib_error_package_not_found")) { + cli::cli_abort("Aborted computation in {.fn {snake_class(self)}}", parent = cnd) + } + + # for other errors, ignore them with warnings + cli::cli_warn("Computation failed in {.fn {snake_class(self)}}", parent = cnd) + new_data_frame() + } + ) }) }, diff --git a/R/stat-summary.r b/R/stat-summary.r index 41107469aa..6e0a83cb7f 100644 --- a/R/stat-summary.r +++ b/R/stat-summary.r @@ -49,6 +49,7 @@ #' @param fun.args Optional additional arguments passed on to the functions. #' @export #' @examples +#' if (requireNamespace("Hmisc", quietly = TRUE)) { #' d <- ggplot(mtcars, aes(cyl, mpg)) + geom_point() #' d + stat_summary(fun.data = "mean_cl_boot", colour = "red", size = 2) #' @@ -126,6 +127,7 @@ #' m2 + coord_trans(y="log10") #' } #' } +#' } stat_summary <- function(mapping = NULL, data = NULL, geom = "pointrange", position = "identity", ..., diff --git a/man/geom_hex.Rd b/man/geom_hex.Rd index 97b41b321d..f856c1c813 100644 --- a/man/geom_hex.Rd +++ b/man/geom_hex.Rd @@ -117,6 +117,7 @@ Learn more about setting these aesthetics in \code{vignette("ggplot2-specs")}. } \examples{ +if (!requireNamespace("hexbin", quietly = TRUE)) { d <- ggplot(diamonds, aes(carat, price)) d + geom_hex() @@ -131,6 +132,7 @@ d + geom_hex(binwidth = c(1, 1000)) d + geom_hex(binwidth = c(.1, 500)) } } +} \seealso{ \code{\link[=stat_bin2d]{stat_bin2d()}} for rectangular binning } diff --git a/man/stat_summary.Rd b/man/stat_summary.Rd index cdae9a3093..375b299801 100644 --- a/man/stat_summary.Rd +++ b/man/stat_summary.Rd @@ -184,6 +184,7 @@ If no aggregation functions are supplied, will default to } \examples{ +if (requireNamespace("Hmisc", quietly = TRUE)) { d <- ggplot(mtcars, aes(cyl, mpg)) + geom_point() d + stat_summary(fun.data = "mean_cl_boot", colour = "red", size = 2) @@ -262,6 +263,7 @@ m2 + coord_trans(y="log10") } } } +} \seealso{ \code{\link[=geom_errorbar]{geom_errorbar()}}, \code{\link[=geom_pointrange]{geom_pointrange()}}, \code{\link[=geom_linerange]{geom_linerange()}}, \code{\link[=geom_crossbar]{geom_crossbar()}} for geoms to diff --git a/tests/testthat/test-stat-summary.R b/tests/testthat/test-stat-summary.R index 5f8cc4b4fd..8f5e62f161 100644 --- a/tests/testthat/test-stat-summary.R +++ b/tests/testthat/test-stat-summary.R @@ -80,3 +80,18 @@ test_that("stat_summary_(2d|hex) work with lambda expressions", { ) }) + +test_that("stat_summary() errors when check_installed() fails (#4736)", { + fun1 <- function(...) { + stop("usual error") + } + + fun2 <- function(...) { + check_installed("nosuchpackage") + } + + p <- ggplot(data.frame(x = 1), aes(x, x)) + + expect_warning(expect_error(ggplot_build(p + stat_summary(fun.data = fun1)), NA)) + expect_error(ggplot_build(p + stat_summary(fun.data = fun2)), class = "rlib_error_package_not_found") +})