Skip to content

Commit cb20f23

Browse files
authored
error on more than 1e6 bins (#3081)
1 parent f37d525 commit cb20f23

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
* `coord_sf()`, `coord_map()`, and `coord_polar()` now squash `-Inf` and `Inf`
4545
into the min and max of the plot (@yutannihilation, #2972).
4646

47+
* `stat_bin()` will now error when the number of bins exceeds 1e6 to avoid
48+
accidentally freezing the user session (@thomasp85).
49+
4750
# ggplot2 3.1.0
4851

4952
## Breaking changes

R/bin.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ bin_breaks_width <- function(x_range, width = NULL, center = NULL,
8787
max_x <- x_range[2] + (1 - 1e-08) * width
8888
breaks <- seq(origin, max_x, width)
8989

90+
if (length(breaks) > 1e6) {
91+
stop("The number of histogram bins must be less than 1,000,000.\nDid you make `binwidth` too small?", call. = FALSE)
92+
}
93+
9094
bin_breaks(breaks, closed = closed)
9195
}
9296

tests/testthat/test-stat-bin.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ test_that("weights are added", {
124124
expect_equal(out$count, df$y)
125125
})
126126

127+
test_that("bin errors at high bin counts", {
128+
expect_error(bin_breaks_width(c(1, 2e6), 1), "The number of histogram bins")
129+
})
130+
127131
# stat_count --------------------------------------------------------------
128132

129133
test_that("stat_count throws error when y aesthetic present", {

0 commit comments

Comments
 (0)