Skip to content

Commit

Permalink
simulator function assigned via string
Browse files Browse the repository at this point in the history
(allows functions external to the package to pass into parallel simulation calls)
  • Loading branch information
seanhaythorne committed Mar 16, 2021
1 parent adddd4b commit b1805ec
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions R/ModelSimulator.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ ModelSimulator <- R6Class("ModelSimulator",
if (is.null(value) || is.function(value) ||
(is.character(value) &&
tryCatch(is.function(eval(parse(text = value))), error = function(e) FALSE))) {
if (is.character(value)) {
value <- eval(parse(text = value))
}
private$.simulation_function <- value
} else {
stop(paste("Could not assign function", value), call. = FALSE)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_simulation_manager.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ test_that("initialization and parameter setting", {
expect_is(sim_manager$model_simulator, "ModelSimulator")
expect_null(sim_manager$model_simulator$simulation_function)
sim_manager$model_template <- PopulationModel$new()
expect_equal(sim_manager$model_simulator$simulation_function, "population_simulator")
expect_true(is.function(sim_manager$model_simulator$simulation_function))
sim_manager <- SimulationManager$new(model_template = PopulationModel$new())
expect_equal(sim_manager$model_simulator$simulation_function, "population_simulator")
expect_true(is.function(sim_manager$model_simulator$simulation_function))
# Invalid attributes
expect_error(sim_manager$model_template <- "dummy",
"Model template must be a SimulationModel or inherited class object")
Expand Down

0 comments on commit b1805ec

Please sign in to comment.