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

withr::with_preserve_seed() does not preserve is.null(.Random.seed) #124

Closed
HenrikBengtsson opened this issue Dec 23, 2019 · 4 comments · Fixed by #125
Closed

withr::with_preserve_seed() does not preserve is.null(.Random.seed) #124

HenrikBengtsson opened this issue Dec 23, 2019 · 4 comments · Fixed by #125

Comments

@HenrikBengtsson
Copy link

Example:

$ Rscript --vanilla -e "str(.GlobalEnv$.Random.seed); withr::with_preserve_seed(x <- sample.int(1L)); str(.GlobalEnv$.Random.seed)"
 NULL
int [1:626] 10403 1 1586168631 257966637 -1833262268 2037450858 175878869 786533487 163480950 -2036568416 ...
> packageVersion("withr")
[1] '2.1.2'
@krlmlr
Copy link
Member

krlmlr commented Dec 27, 2019

I'm not sure it's even possible to preserve a NULL random seed. Can we solve this by documentation?

@HenrikBengtsson
Copy link
Author

Sorry, I wasn't clear. The value is not NULL - that's just a side effect of <env>$ - its that it does not exist at all. To solve it, first check exists(".Random.seed", envir = .GlobalEnv) (or is.null(oseed)). Then, reset .Random.seed either to the old value or remove it using rm(".Random.seed", envir = .GlobalEnv). This approach is used in several packages.

BTW, .GlobalEnv$.Random.seed <- NULL is not a valid seed. It'll trigger warnings by R itself.

I'd say it's not sufficient to document. A real-world problem where withr::with_preserve_seed() fail because of this is:

$ R --quiet --vanilla
> exists(".Random.seed", envir = .GlobalEnv)
[1] FALSE
> library(ggplot2)
> exists(".Random.seed", envir = .GlobalEnv)
[1] TRUE

It's an unfortunate side effect that loading/attaching a package causes an update to the RNG state. It prevents certain type of RNG validations. (This is actually how I tracked it down).

jimhester added a commit that referenced this issue Dec 27, 2019
jimhester added a commit that referenced this issue Dec 27, 2019
@jimhester
Copy link
Member

jimhester commented Dec 27, 2019

#125 should fix this, after that change we have the following behavior (run in an interactive session so that the ggplot onAttach code would trigger)

exists(".Random.seed", envir = .GlobalEnv)
#> [1] FALSE
library(ggplot2)
exists(".Random.seed", envir = .GlobalEnv)
#> [1] FALSE

@HenrikBengtsson
Copy link
Author

Yes, LGTM. Thxs. Not sure when you want to close this issue, so I leave that to you.

jimhester added a commit that referenced this issue Dec 30, 2019
* Restore an unset `.Random.seed`

Fixes #124

* Move tests

* Tweak and add test

Co-authored-by: Kirill Müller <krlmlr@users.noreply.github.com>
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

Successfully merging a pull request may close this issue.

3 participants