simcdm
in R packagesvignettes/simcdm-in-packages.Rmd
simcdm-in-packages.Rmd
The design of simcdm
allows the package to be included
in other R packages using either the R or C++
functions. The next section details provides with how to incorporate
either the R or C++ functions into a new R
package or standalone C++ file.
Note, if you are not familiar with compiled code in R please feel free to use the traditional way to import the R functions.
To use simcdm
’s R functions
only in your own R package, modify the
package’s DESCRIPTION
file by adding an imports
declaration.
Imports: simcdm
Inside of the package’s NAMESPACE
file, make sure to
use:
import(simcdm)
If you are using roxygen2
to manage the packages
NAMESPACE
file, add the following tag and re-run the
roxygenize()
function.
#' @import simcdm
Within a C++ file in src/
, then add:
#include <RcppArmadillo.h>
#include <simcdm.h>
// [[Rcpp::depends(simcdm, RcppArmadillo)]]
// [[Rcpp::export]]
::mat example_dina_sim(const arma::mat &alphas, const arma::mat &Q,
armaconst arma::vec &ss, const arma::vec &gs) {
::mat dina_items = simcdm::sim_dina_items(alphas, Q, ss, gs);
arma
return dina_items;
}
To use C++ functions available in simcdm
within
your R package, modify your package’s DESCRIPTION
file by
adding:
LinkingTo: Rcpp, RcppArmadillo (>= 0.9.200), simcdm
Imports:
Rcpp (>= 1.0.0)
Reference the simulation functions using simcdm
namespace like so:
#include <simcdm.h>
// [[Rcpp::export]]
::mat example_rrum_sim(const arma::mat &Q, const arma::mat &rstar,
armaconst arma::vec &pistar, const arma::mat &alpha) {
::mat rrum_items = simcdm::sim_rrum_items(Q, rstar, pistar, alpha);
arma
return rrum_items;
}