Skip to content

Instantly share code, notes, and snippets.

@knthls
Created April 3, 2020 09:11
Show Gist options
  • Save knthls/c412a7dd3c9682d5be0a218d2272d6dc to your computer and use it in GitHub Desktop.
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)
# 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