Skip to content

Commit

Permalink
update to use on.exit
Browse files Browse the repository at this point in the history
  • Loading branch information
gowerc committed Jan 10, 2025
1 parent 4d2a39e commit de7b348
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,14 @@ get_stan_model <- function() {
# See https://github.com/insightsengineering/rbmi/issues/469
# Note that .Random.seed is only set if the seed has been set or if a random number
# has been generated.
if (exists(".Random.seed")) {
current_seed_state <- .Random.seed
}
current_seed_state <- globalenv()$.Random.seed
on.exit({
if (is.null(current_seed_state) && exists(".Random.seed", envir = globalenv())) {
rm(".Random.seed", envir = globalenv(), inherits = FALSE)
} else {
assign(".Random.seed", value = current_seed_state, envir = globalenv(), inherits = FALSE)
}
})

ensure_rstan()
local_file <- file.path("inst", "stan", "MMRM.stan")
Expand All @@ -600,9 +605,6 @@ get_stan_model <- function() {
model_name = "rbmi_mmrm"
)

if (exists("current_seed_state")) {
.Random.seed <- current_seed_state
}
return(model)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-reproducibility.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ test_that("Results are if model is recompiled", {

## Tidy up things that will never be the same:
drawobj$formula <- NULL # Formulas contain environments specific to their build
drawobj$fit <- NULL # Bayes object has "fit" which contains a timestamp
anaobj$call <- NULL # Argument names are different (imputeobj2)
drawobj$fit <- NULL # Bayes object has "fit" which contains a timestamp
anaobj$call <- NULL # Argument names are different (imputeobj2)

return(list(
draws = drawobj,
Expand Down

0 comments on commit de7b348

Please sign in to comment.