Convert rcpp variables into standard C++ variables

萝らか妹 提交于 2019-12-07 03:38:53

问题


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];

回答1:


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);


来源:https://stackoverflow.com/questions/21362844/convert-rcpp-variables-into-standard-c-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!