Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to automatically restore initial random state after set.seed #228

Closed
kassambara opened this issue Nov 26, 2022 · 3 comments
Closed

Comments

@kassambara
Copy link

Summary

geom_text_repel() changes the global seed when the option seed is specified, if you run code here-after, you will always get same result. Would you add the possibility to automatically restore initial random state after set.seed like in ggplo2::position_jitter() ?

Minimal code example

suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(ggrepel))
options(ggrepel.max.overlaps = Inf)
for(i in 1:3){
p <- ggplot(mtcars,
            aes(wt, mpg, label = rownames(mtcars), colour = factor(cyl))) +
  geom_point()
  print(p + ggrepel::geom_text_repel(seed = 42))
  print(sample(1:100,1))
}
#> [1] 6
#> [1] 6
#> [1] 6
@slowkow
Copy link
Owner

slowkow commented Nov 27, 2022

Thanks for the issue and the reproducible code example!

I also want to thank you for the tip regarding ggplot2::position_jitter(), where I found this utility function:

with_seed_null <- function(seed, code) {
  if (is.null(seed)) {
    code
  } else {
    withr::with_seed(seed, code)
  }
}

I think we should be able to integrate this code into ggrepel.

slowkow added a commit that referenced this issue Nov 27, 2022
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
@slowkow
Copy link
Owner

slowkow commented Nov 27, 2022

@kassambara Could I please ask you to go ahead and try the new code and check that it works for you?

I tried your example and it seems that it is working as expected. If I haven't made any blunders, then I think it would be nice to push this to CRAN.

@kassambara
Copy link
Author

Fantastic, works perfectly! This is an important update for anyone doing any kind of stochastic processing after ggrepel call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants