mrgsolve
Loading...
Searching...
No Matches
mrgx functions

Functions

Rcpp::Environment mrgx::get_envir (const databox &self)
 Return the model environment.
double mrgx::rnorm (const double mean, const double sd, const double lower, const double upper)
double mrgx::rlognorm (const double mean, const double sd, const double lower, const double upper)
template<typename _T___>
_T___ mrgx::get (const std::string name, const databox &self)
template<typename _T___>
void mrgx::assign (const std::string x, const _T___ &value, const databox &self)
template<typename _T___>
_T___ mrgx::get (const std::string name)
template<typename _T___>
_T___ mrgx::get (const std::string package, const std::string name)
template<typename _T___>
_T___ mrgx::readRDS (const std::string filename)
Rcpp::Function mrgx::mt_fun ()

Detailed Description

Extra C++ functions provided by mrgsolve. To use these functions, use $PLUGIN mrgx in your model file. Using mrgx will also invoke the Rcpp plugin.

Function Documentation

◆ assign()

template<typename _T___>
void mrgx::assign ( const std::string x,
const _T___ & value,
const databox & self )

Assign an object to the model environment.

Example:

$PLUGIN mrgx
$GLOBAL Rcpp::NumericVector ans(100);
$PREAMBLE
for(R_xlen_t i = 0; i < ans.size(); ++i) {
ans[i] = R::rnorm(0,1);
}
$PK
if(FINAL_ROW) {
mrgx::assign("vec", ans, self);
}
void assign(const std::string x, const _T___ &value, const databox &self)
Definition mrgx.h:167
#define FINAL_ROW
Definition modelheader.h:130
Definition mrgx.h:35
Parameters
xa variable name to be assigned in the model evironment.
valuea value to be assigned to x in the model environment.
selfthe model object; the assignment happens in self.envir.

◆ get() [1/3]

template<typename _T___>
_T___ mrgx::get ( const std::string name)

Get an R object from the global environment.

Example:

Rcpp::NumericVector x = mrgx::get<Rcpp::NumericVector>("x");
_T___ get(const std::string name, const databox &self)
Definition mrgx.h:136

Note: for this to work, x must be a numeric vector in .GlobalEnv.

Parameters
namename of the R object to get
Returns
an object from the global environment

◆ get() [2/3]

template<typename _T___>
_T___ mrgx::get ( const std::string name,
const databox & self )

Get an R object from the model environment.

Example:

$PLUGIN mrgx
$GLOBAL
Rcpp::NumericMatrix mat;
$PREAMBLE
mat = mrgx::get<Rcpp::NumericMatrix>("mymatrix", self);
$ENV
mat <- dmat(1,2,3)

Note: for this to work, you must define a numeric matrix called mat in $ENV.

Parameters
namename of the R object to get
selfthe model data object
Returns
an object from the model environment

◆ get() [3/3]

template<typename _T___>
_T___ mrgx::get ( const std::string package,
const std::string name )

Get an R object from a package namespace. This is typically used to get a function from a specific package.

Example:

To get a function, use this code:

$PLUGIN mrgx
$GLOBAL
Rcpp::Function Rnorm = mrgx::get<Rcpp::Function>("stats", "rnorm");
Rcpp::Function Seq = mrgx::get<Rcpp::Function>("base", "seq");
$ERROR
if(FINAL_ROW) {
mrgx::assign("double", Rnorm(10), self);
mrgx::assign("int", seq(10), self);
}
Parameters
packagename of the package
namename of the object to get
Returns
an object from the package namespace

◆ get_envir()

Rcpp::Environment mrgx::get_envir ( const databox & self)

Return the model environment.

With each mrgsolve model object, there is an R environment that can be used to maintain arbitrary R objects, potentially for use in the model.

Parameters
selfthe model databox object
Returns
the model environment

◆ mt_fun()

Rcpp::Function mrgx::mt_fun ( )

An empty R function. This is typically used as a placeholder when declaring an Rcpp::Function object in $GLOBAL. This pattern of declaring in $GLOBAL and (re)defining in $PREAMBLE could be used for efficiency when the function must be called many times.

$PLUGIN mrgx
$ENV
NORM <- function(n, mu, sd) rnorm(n, mu, sd)
$GLOBAL
Rcpp::Function fun = mrgx::mt_fun();
$PREAMBLE
$MAIN
Rcpp::NumericVector ans = fun(1000, 33, 3);
if(FINAL_ROW) {
mrgx::assign("vec", ans, self);
}
double rnorm(const double mean, const double sd, const double lower, const double upper)
Definition mrgx.h:71
Rcpp::Function mt_fun()
Definition mrgx.h:284
Returns
the function mt_fun from the mrgsolve namespace.

◆ readRDS()

template<typename _T___>
_T___ mrgx::readRDS ( const std::string filename)

Read an RDS file.

$PLUGIN mrgx
$ENV
file <- file.path(tempdir(), "file-read-rds.RDS")
x <- list(a = 1.23, b = c(1,2,3,99))
saveRDS(object = x, file = file)
$PREAMBLE
std::string file = mrgx::get<std::string>("file", self);
Rcpp::List y = mrgx::readRDS<Rcpp::List>(file);
capture a = y["a"];
capture b = Rcpp::as<Rcpp::NumericVector>(y["b"])[3];
_T___ readRDS(const std::string filename)
Definition mrgx.h:250
double capture
Definition housemodel-mread-source.cpp:25
Parameters
filenamethe name of the RDS file to read.
Returns
an object saved in the RDS file

◆ rlognorm()

double mrgx::rlognorm ( const double mean,
const double sd,
const double lower,
const double upper )

Simulate random lognormal variate between lower and upper boundaries. An error is generated if a variate between lower and upper bounds cannot be generated in 50 tries.

Example:

$MAIN
double WT = mrgx::rlnorm(log(80),1,40,140);
const double & WT
Definition housemodel-mread-source.cpp:74
Parameters
meannormal distribution mean
sdnormal distribution standard deviation
lowerlower bound for variates
upperupper bound for variates
Returns
the simulated variate

◆ rnorm()

double mrgx::rnorm ( const double mean,
const double sd,
const double lower,
const double upper )

Simulate random normal variate between lower and upper boundaries. An error is generated if a variate between lower and upper bounds cannot be generated in 50 tries.

Example:

$MAIN
double WT = mrgx::rnorm(80,40,40,140);
Parameters
meannormal distribution mean
sdnormal distribution standard deviation
lowerlower bound for variates
upperupper bound for variates
Returns
the simulated variate