Using simcdm in R packages
Author: James Joseph Balamuta
Date: 2019-03-09

# Using `simcdm` in R packages

#### James Joseph Balamuta

#### 2019-03-09

# Package usage

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.

## R Package Usage

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

## C++ Usage

### C++ Standalone Usage

Within a C++ file in `src/`, then add:

    #include <RcppArmadillo.h>
    #include <simcdm.h>

    // [[Rcpp::depends(simcdm, RcppArmadillo)]]

    // [[Rcpp::export]]
    arma::mat example_dina_sim(const arma::mat &alphas, const arma::mat &Q,
                               const arma::vec &ss, const arma::vec &gs) {

       arma::mat dina_items = simcdm::sim_dina_items(alphas, Q, ss, gs);

       return dina_items;
    }

### C++ Package

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]]
    arma::mat example_rrum_sim(const arma::mat &Q, const arma::mat &rstar,
                               const arma::vec &pistar, const arma::mat &alpha) {

       arma::mat rrum_items = simcdm::sim_rrum_items(Q, rstar, pistar, alpha);

       return rrum_items;
    }