# 1. Initialize a new simulation setup:
object <- Simulator$new(verbose = TRUE)
# 2. Define function `f` and arguments (if any):
f <- function(x, y = 1) {
Sys.sleep(runif(1)) # to see progress updates
x + y
}
x_args <- list(1, 2)
object$define(f = f, x = x_args)
print(object)
# 3. Define 'future' and 'progress' (optional):
if (FALSE) {
future::plan(future::sequential)
progressr::handlers(global = TRUE)}
# 4. Evaluate `f` `runs` times at each parameter combination (backup is optional):
path <- file.path(tempdir(), paste0("backup_", format(Sys.time(), "%Y-%m-%d-%H-%M-%S")))
object$go(runs = 2, backup = TRUE, path = path)
# 5. Access the results:
object$results
# 6. Check if cases are pending or if an error occurred:
object$cases
# 7. Restore simulation results from backup:
object_restored <- Simulator$new(use_backup = path)
print(object_restored)
if (FALSE) all.equal(object, object_restored)
# 8. Run more simulations and pending simulations (if any):
object_restored$go(runs = 2)
Run the code above in your browser using DataLab