From e9bcb756d6f7ee9e3bbb160672bc693dea15bd02 Mon Sep 17 00:00:00 2001 From: Kamil Slowikowski Date: Sat, 26 Nov 2022 20:42:17 -0500 Subject: [PATCH] use with_seed_null() from ggplot2 When we set the seed in geom_text_repel(seed = 1) or geom_label_repel(seed = 1), this will no longer override the seed for other unrelated code. Thanks for @kassambara for reporting this in #228 --- DESCRIPTION | 5 +++-- NEWS.md | 12 ++++++++++++ R/geom-label-repel.R | 8 ++++---- R/geom-text-repel.R | 8 ++++---- R/utilities.R | 8 ++++++++ 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6a94711..da3e289 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: ggrepel -Version: 0.9.2 +Version: 0.9.2.9999 Authors@R: c( person("Kamil", "Slowikowski", email = "kslowikowski@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-2843-6370")), person("Alicia", "Schep", role = "ctb", comment = c(ORCID = "0000-0002-3915-0618")), @@ -30,7 +30,8 @@ Imports: grid, Rcpp, rlang (>= 0.3.0), - scales (>= 0.5.0) + scales (>= 0.5.0), + withr (>= 2.5.0) Suggests: knitr, rmarkdown, diff --git a/NEWS.md b/NEWS.md index ace64b6..5d5837c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,16 @@ +ggrepel 0.9.2.9999 +======================== + +## Bug fixes + +* When we set the seed in `ggrepel::geom_text_repel(seed = 1)`, this will + no longer override the seed for other unrelated code. Thanks to + @kassambara for reporting this in [issue 228]. + +[issue 228]: https://github.com/slowkow/ggrepel/issues/228 + + ggrepel 0.9.2 ======================== diff --git a/R/geom-label-repel.R b/R/geom-label-repel.R index 7240dbb..c2f23f4 100644 --- a/R/geom-label-repel.R +++ b/R/geom-label-repel.R @@ -258,8 +258,8 @@ makeContent.labelrepeltree <- function(x) { }) # Make the repulsion reproducible if desired. - if (is.null(x$seed) || !is.na(x$seed)) { - set.seed(x$seed) + if (!is.null(x$seed) && is.na(x$seed)) { + x$seed <- sample.int(.Machine$integer.max, 1L) } # The points are represented by circles. @@ -281,7 +281,7 @@ makeContent.labelrepeltree <- function(x) { ) / 13 # Repel overlapping bounding boxes away from each other. - repel <- repel_boxes2( + repel <- with_seed_null(x$seed, repel_boxes2( data_points = as.matrix(x$data[,c("x","y")]), point_size = point_size, point_padding_x = point_padding, @@ -298,7 +298,7 @@ makeContent.labelrepeltree <- function(x) { max_overlaps = x$max.overlaps, direction = x$direction, verbose = x$verbose - ) + )) if (any(repel$too_many_overlaps)) { warn( diff --git a/R/geom-text-repel.R b/R/geom-text-repel.R index 60956f4..b5f6b66 100644 --- a/R/geom-text-repel.R +++ b/R/geom-text-repel.R @@ -397,8 +397,8 @@ makeContent.textrepeltree <- function(x) { }) # Make the repulsion reproducible if desired. - if (is.null(x$seed) || !is.na(x$seed)) { - set.seed(x$seed) + if (!is.null(x$seed) && is.na(x$seed)) { + x$seed <- sample.int(.Machine$integer.max, 1L) } # The points are represented by circles. @@ -420,7 +420,7 @@ makeContent.textrepeltree <- function(x) { ) / 13 # Repel overlapping bounding boxes away from each other. - repel <- repel_boxes2( + repel <- with_seed_null(x$seed, repel_boxes2( data_points = as.matrix(x$data[,c("x","y")]), point_size = point_size, point_padding_x = point_padding, @@ -437,7 +437,7 @@ makeContent.textrepeltree <- function(x) { max_overlaps = x$max.overlaps, direction = x$direction, verbose = x$verbose - ) + )) if (any(repel$too_many_overlaps)) { warn( diff --git a/R/utilities.R b/R/utilities.R index 6c0717f..b159d1f 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -7,6 +7,14 @@ ggname <- function(prefix, grob) { grob } +with_seed_null <- function(seed, code) { + if (is.null(seed)) { + code + } else { + withr::with_seed(seed, code) + } +} + .pt <- 72.27 / 25.4 "%||%" <- function(a, b) {