Skip to content

Commit

Permalink
use with_seed_null() from ggplot2
Browse files Browse the repository at this point in the history
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
  • Loading branch information
slowkow committed Nov 27, 2022
1 parent 2e6162e commit e9bcb75
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
========================

Expand Down
8 changes: 4 additions & 4 deletions R/geom-label-repel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions R/geom-text-repel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e9bcb75

Please sign in to comment.