Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert rcpp variables into standard C++ variables

My input looks like this:

Rcpp::NumericMatrix data(dataMatrix);
Rcpp::NumericVector xSize(dataXsize);
Rcpp::NumericVector ySize(dataYsize);
Rcpp::NumericVector tIndexes(testIndexes);
Rcpp::NumericVector cIndexes(controlIndexes);

And the external library I'm trying to call has this signature

WilcoxonTest(float * _data, int _dataXsize, int _dataYsize, vector<int> * _testIndexes, vector<int> * _controlIndexes);

How can I convert the Rcpp data types into C++ standard data types?

Note:

 float * _data

Is a matrix of floating point values. The library assumes its something like this:

 float * _data = new float[dataXsize * dataYsize];
like image 957
user1047833 Avatar asked Jan 26 '14 11:01

user1047833


1 Answers

To get a std::vector<float> out of your NumericMatrix, you can use:

std::vector<float> dat = Rcpp::as<std::vector<float> >(data);

The numbers in this vector will be stored as they are stored in the matrix in R (e.g. matrix(1:4, nrow=2) will result in a vector 1 2 3 4). If you need the transpose, I would suggest calling t from within R.

All that remains for your problem is converting the vector into a C-style raw pointer. This can be done with:

float* _data = &dat[0];

Some words of warning about grabbing raw vectors out of std::vector objects: if you later added objects to dat or deleted dat, the _data pointer could become invalid.

You can grab the other parameters through similar use of the Rcpp::as function:

int _dataXsize = Rcpp::as<int>(xSize);
int _dataYsize = Rcpp::as<int>(ySize);
std::vector<int> _testIndexes = Rcpp::as<std::vector<int> >(tIndexes);
std::vector<int> _controlIndexes = Rcpp::as<std::vector<int> >(cIndexes);
like image 123
josliber Avatar answered Oct 16 '22 20:10

josliber