From b1805ec45fc84f9e2ff172ffe3a85a58c09ee70a Mon Sep 17 00:00:00 2001 From: Sean Haythorne Date: Tue, 16 Mar 2021 17:18:44 +1030 Subject: [PATCH] simulator function assigned via string (allows functions external to the package to pass into parallel simulation calls) --- R/ModelSimulator.R | 3 +++ tests/testthat/test_simulation_manager.R | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/R/ModelSimulator.R b/R/ModelSimulator.R index 1fc9066..1fd4a72 100644 --- a/R/ModelSimulator.R +++ b/R/ModelSimulator.R @@ -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) diff --git a/tests/testthat/test_simulation_manager.R b/tests/testthat/test_simulation_manager.R index ff28b9c..341deb9 100644 --- a/tests/testthat/test_simulation_manager.R +++ b/tests/testthat/test_simulation_manager.R @@ -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")