Skip to content

[Rcpp-devel] How to use functions from a R library in .cpp file

3 messages · Yixuan Qiu, Li Li

#
Hi,


I am trying to use the function "pmnorm" from R package "mnormt" in a .cpp file.

I was wondering if anyone could help me with the following code so that I can use "pmnorm" somewhere in my sample function "epsilonij".


#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace arma;

// [[Rcpp::export]]

double epsilonij (double nu_i_lj, Rcpp::NumericVector w, ){
  ....
}


Thanks.


Li
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150123/6dbf8bba/attachment-0001.html>
#
Hello,
There is an example showing the usage of Environment and Function in the
Rcpp Quick Reference Guide that is distributed with the package.
For your problem, it's something as follows:

NumericVector test()
{
    Environment pkg("package:mnormt");
    Function pmnorm = pkg["pmnorm"];
    NumericVector x = NumericVector::create(1, 0, 1);
    NumericVector mean = NumericVector::create(0, 0, 0);
    NumericMatrix cov(3, 3);
    cov(0, 0) = cov(1, 1) = cov(2, 2) = 1;
    return pmnorm(x, mean, cov);
}



Best,
Yixuan



2015-01-22 19:29 GMT-05:00 Li Li <llis at unm.edu>:

  
    
#
Dear Yixuan,

Thank you very much.

I embedded the following into the program and it worked. The only thing changed was that I added "Rcpp::? so it could read in a cpp file.

Rcpp::NumericVector test()
{
    Rcpp::Environment pkg("package:mvtnorm");
    Rcpp::Function pmvnorm = pkg["pmvnorm"];
    Rcpp::NumericVector lower = Rcpp::NumericVector::create(1, 0, 1);
    Rcpp::NumericVector upper = Rcpp::NumericVector::create(2, 1, 2);

    Rcpp::NumericVector mean = Rcpp::NumericVector::create(0, 0, 0);
    Rcpp::NumericMatrix cov(3, 3);
    cov(0, 0) = cov(1, 1) = cov(2, 2) = 1;
    return pmvnorm(lower,upper, mean, cov);
}

Best,

Li
On Jan 22, 2015, at 5:44 PM, Yixuan Qiu <yixuan.qiu at cos.name<mailto:yixuan.qiu at cos.name>> wrote:
NumericVector test()
{
    Environment pkg("package:mnormt");
    Function pmnorm = pkg["pmnorm"];
    NumericVector x = NumericVector::create(1, 0, 1);
    NumericVector mean = NumericVector::create(0, 0, 0);
    NumericMatrix cov(3, 3);
    cov(0, 0) = cov(1, 1) = cov(2, 2) = 1;
    return pmnorm(x, mean, cov);
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150123/11a32175/attachment.html>