model/mrgx-get-1.solv
[ plugin] mrgx
[ env ]
set.seed(1023)
numbers <- rnorm(10)mrgx::get()Every mrgsolve model object contains a R environment where you can maintain R objects, typically for use in your model. We will use mrgx::get() inside the model to get access to that object.
First, let’s create a toy model, with an object seeded in the model environment.
model/mrgx-get-1.solv
[ plugin] mrgx
[ env ]
set.seed(1023)
numbers <- rnorm(10)mod <- mread("model/mrgx-get-1.solv"). Building mrgx-get-1_solv ... done.
This object now has a numeric vector named numbers in the model environment.
env_get(mod, "numbers"). [1] -0.6765114 1.6417261 -0.2369903 0.9579535 -0.8514430 -0.3593212
. [7] 0.8978122 -1.4875644 -1.1101949 1.5163280
Now, let’s get() that object in a [ table ] block.
model/mrgx-get-2.solv
[ plugin] mrgx
[ env ]
set.seed(1023)
numbers <- rnorm(10)
[ table ]
Rcpp::NumericVector numbers =
mrgx::get<Rcpp::NumericVector>("numbers", self);
if(self.irown < numbers.size()-1) {
capture number = numbers.at(self.irown);
}In this example, we grab a numeric vector from the model environment and copy some of the numbers into the simulated output.
mod <- mread("model/mrgx-get-2.solv"). Building mrgx-get-2_solv ... done.
mrgsim(mod, end = 10). Model: mrgx-get-2_solv
. Dim: 11 x 3
. Time: 0 to 10
. ID: 1
. ID time number
. 1: 1 0 -0.6765
. 2: 1 1 1.6417
. 3: 1 2 -0.2370
. 4: 1 3 0.9580
. 5: 1 4 -0.8514
. 6: 1 5 -0.3593
. 7: 1 6 0.8978
. 8: 1 7 -1.4876
You can also update an object in the model environment to a different value and simulate again.
mod <- env_update(mod, numbers = runif(30, 2, 4))
mrgsim(mod). Model: mrgx-get-2_solv
. Dim: 25 x 3
. Time: 0 to 24
. ID: 1
. ID time number
. 1: 1 0 2.181
. 2: 1 1 3.035
. 3: 1 2 3.999
. 4: 1 3 2.536
. 5: 1 4 3.382
. 6: 1 5 2.669
. 7: 1 6 3.481
. 8: 1 7 3.268
or use base::assign:
vec <- -rnorm(30, 82, 10)
assign("numbers", envir = env_get_env(mod), vec)
mrgsim(mod). Model: mrgx-get-2_solv
. Dim: 25 x 3
. Time: 0 to 24
. ID: 1
. ID time number
. 1: 1 0 -95.11
. 2: 1 1 -92.86
. 3: 1 2 -75.55
. 4: 1 3 -77.11
. 5: 1 4 -63.52
. 6: 1 5 -99.72
. 7: 1 6 -93.39
. 8: 1 7 -102.29
You can also get an object from the global environment.
model/mrgx-get-3.solv
[ plugin] mrgx
[ table ]
Rcpp::NumericVector magic_n =
mrgx::get<Rcpp::NumericVector>("magic_number");
capture magic = magic_n.at(0);magic_number <- 98765
mod <- mread("model/mrgx-get-3.solv"). Building mrgx-get-3_solv ... done.
mrgsim(mod). Model: mrgx-get-3_solv
. Dim: 25 x 3
. Time: 0 to 24
. ID: 1
. ID time magic
. 1: 1 0 98765
. 2: 1 1 98765
. 3: 1 2 98765
. 4: 1 3 98765
. 5: 1 4 98765
. 6: 1 5 98765
. 7: 1 6 98765
. 8: 1 7 98765
Note that this model requires you to have magic_number defined in the global environment at run time. While you can do this, it would be better if you defined magic_number in the model environment, so the definition stays with the model.
In this example, we get() some functions from base and assign an integer vector back into the global environment.
model/mrgx-get-4.solv
[ plugin] mrgx
[ table ]
if(FINAL_ROW) {
Rcpp::Function seq = mrgx::get<Rcpp::Function>("base", "seq");
Rcpp::Function asgn = mrgx::get<Rcpp::Function>("base", "assign");
Rcpp::IntegerVector idx = seq(100);
asgn("indices", idx, Rcpp::Environment::global_env());
}Check that indices doesn’t exist.
exists("indices"). [1] FALSE
Now load and simulate from the model.
mod <- mread("model/mrgx-get-4.solv"). Building mrgx-get-4_solv ... done.
out <- mrgsim(mod)During the model run, we’ve called base::seq() to generate a sequence of integers, then called base::assign() to assign to an indices object in the global environment.
exists("indices"). [1] TRUE
indices. [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
. [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
. [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
. [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
. [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
. [91] 91 92 93 94 95 96 97 98 99 100
Again, assigning to the global environment probably isn’t the safest thing to do. We included and environment in the model to give you a safe place to assign and get R objects during the model run.