Created
April 3, 2020 09:11
-
-
Save knthls/c412a7dd3c9682d5be0a218d2272d6dc to your computer and use it in GitHub Desktop.
Isolate code execution in seperate environment in R (particularly calls to set seed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Isolate execution of set.seed (or anything else) in own env | |
s1 <- .GlobalEnv$.Random.seed | |
draw <- function(){ | |
eval({ | |
set.seed(12) | |
samples = runif(10) | |
}, envir = new.env()) | |
samples | |
} | |
samples1 <- draw() | |
set.seed(42) | |
samples2 <- draw() | |
s2 <- .GlobalEnv$.Random.seed | |
# TRUE | |
print(all(s1 == s2)) | |
# TRUE | |
print(all(samples1 == samples2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment