-
Notifications
You must be signed in to change notification settings - Fork 0
Ensure that multiple different models can be used in the same R session. #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,10 +106,24 @@ Model <- setRefClass("Model", | |
|
|
||
| # Run script that defines initialization functions. | ||
| source(paths$inits_file, local = TRUE) | ||
| initParms <<- initParms | ||
| initStates <<- initStates | ||
|
|
||
| Outputs <<- Outputs | ||
| # Associate initParms for this model with the initParms function defined | ||
| # in inits_file. | ||
| r_command_string <- paste0("initParms <<- initParms_", mName) | ||
| r_expression <- parse(text = r_command_string) | ||
| eval(r_expression) | ||
|
|
||
| # Associate initStates for this model with the initStates function defined | ||
| # in inits_file. | ||
| r_command_string <- paste0("initStates <<- initStates_", mName) | ||
| r_expression <- parse(text = r_command_string) | ||
| eval(r_expression) | ||
|
|
||
| # Associate Outputs for this model with the Outputs variable defined in | ||
| # inits_file. | ||
| r_command_string <- paste0("Outputs <<- Outputs_", mName) | ||
| r_expression <- parse(text = r_command_string) | ||
| eval(r_expression) | ||
|
Comment on lines
+124
to
+126
|
||
|
|
||
dfkapraun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| parms <<- initParms() | ||
| Y0 <<- initStates(parms) | ||
|
|
@@ -125,10 +139,13 @@ Model <- setRefClass("Model", | |
| runModel = function(times, ...) { | ||
| "Perform a simulation for the Model object using the \\code{deSolve} function \\code{ode} for the specified \\code{times}." | ||
| # Solve the ODE system using the "ode" function from the package "deSolve". | ||
| derivs_name <- paste0("derivs_", mName) | ||
| initforc_name <- paste0("initforc_", mName) | ||
| initmod_name <- paste0("initmod_", mName) | ||
| out <- ode(Y0, times, | ||
| func = "derivs", parms = parms, dllname = paths$dll_name, | ||
| initforc = "initforc", initfunc = "initmod", nout = length(Outputs), | ||
| outnames = Outputs, ... | ||
| func = derivs_name, parms = parms, dllname = paths$dll_name, | ||
| initforc = initforc_name, initfunc = initmod_name, | ||
| nout = length(Outputs), outnames = Outputs, ... | ||
| ) | ||
|
|
||
| # Return the simulation output. | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern of building strings, parsing, and evaluating is repeated three times. Consider extracting this into a helper function to reduce duplication and improve maintainability. For example:
assign_model_function <- function(name, mName) { eval(parse(text = paste0(name, ' <<- ', name, '_', mName))) }