1  mrgx::assign()

1.0.1 Trivial example

The assign() function lets you write data generated in the model code to the model environment. The arguments follow the order in base::assign():

  • "name" - the name of the object you want to write
  • value - the value to assign to name
  • self - the model self object

As a trivial example, we sum up TIME through the duration of the simulation, writing out cumtime at every step.

// inline/assign-simple.solv
 
[ plugin ] mrgx

[ error ] 

static local_double cumtime = 0; 
cumtime += TIME;

mrgx::assign("cumtime", cumtime, self);
mod <- mread("inline/assign-simple.solv")
. Building assign-simple_solv ... done.
out <- mrgsim(mod)

To get teh value of cumtime, use env_get():

env_get(out, "cumtime")
. [1] 300

The model object holds the environment we are writing to and the output object contains a copy of the model object. So you can call env_get() on either the model object (mod) or the output object (out). If you already coerced the simulated output to a data frame, you can always go back and fetch your assigned data through the model object. In this sense, it is probably safer to just always loo in the model object.

Note, also, that we are writing cumtime out at every single data set time step, but we only get to see the final value. It would be more efficient if we only wrote the data once the problem was finished. Read through the next example to see how to do this.

1.0.2 More involved

In this example, we will collect simulation outputs in an object inside the model as the simulation progresses. Once the simulation is finished, we will (manually) create a data frame of the output and assign() it into the model environment.

mod <- mread("model/assign.solv")
. Building assign_solv ... done.

The key part of the code is found below. In $ERROR, we will “push” the current values of TIME and DV into the back of the collection object. Then, when we detect that we are on the last (final) row of the problem, we’ll (i) create a data frame with all the data and (ii) assign() it to the model environment.

[ error ] 

Time.push_back(TIME);
Dv.push_back(A/V);

if(FINAL_ROW) {
  Rcpp::DataFrame data = Rcpp::DataFrame::create(
    Rcpp::Named("TIME") = Time, 
    Rcpp::Named("DV") = Dv
  );
  mrgx::assign("data", data, self);
}
[ plugin] mrgx evtools

[ global ]
evt::regimen reg; 
std::deque<double> Time;
std::deque<double> Dv;

[ param ] CL = 1, V = 20

[ pkmodel ] cmt = "A", advan = 1

[ error ] 
if(NEWIND <=1) {
  reg.init(self);
  reg.ii(24); 
  reg.amt(100); 
  reg.until(168); 
  evt::ev obs = evt::tgrid(0, 240, 1);
  self.push(obs);
}

reg.execute();

Time.push_back(TIME);
Dv.push_back(A/V);

if(FINAL_ROW) {
  Rcpp::DataFrame data = Rcpp::DataFrame::create(
    Rcpp::Named("TIME") = Time, 
    Rcpp::Named("DV") = Dv
  );
  mrgx::assign("data", data, self);
}
out <- mrgsim(mod, end = -1, add = c(0,240))

out
. Model:  assign_solv 
. Dim:    2 x 3 
. Time:   0 to 240 
. ID:     1 
.     ID time       A
. 1:   1    0 100.000
. 2:   1  240   1.177
data <- env_get(mod, "data")

head(data)
.   TIME       DV
. 1    0 0.000000
. 2    0 5.000000
. 3    1 4.756147
. 4    2 4.524187
. 5    3 4.303540
. 6    4 4.093654
ggplot(data, aes(TIME,DV)) + geom_line()