[Rcpp-devel] Transform arbitrary R object into std::vector<vector<double>>

Dirk Eddelbuettel edd at debian.org
Thu May 19 14:07:26 CEST 2016


On 19 May 2016 at 10:46, Paul Saary wrote:
| Hello,
| 
| I have been trying to solve this problem for quite some time now, so I
| thought I'd ask here.
| 
| I am trying to write a Cpp function, accepting multiple input types,
| such as matrices and dataframes.
| 
| I understood that I could do this by accepting any SEXP object.
| 
| The core problem now is to transform these object into something my
| software can use, which would be an object of type
| std::vector<vector<double>>.

If you always use vector< vector< double > > then I would start with
Rcpp::NumericMatrix, or arma::mat.

Do you know if you prefer rows or columns? It is pretty easy to pick off
either rows and columns, but you may save yourself a transpose.

Lastly, maybe consider switching your code to arma::mat instead of vector<
vector< double > > which is really a fudge to make up for the lack of proper
Matrix classes in C++ itself.
 
On 19 May 2016 at 11:12, Sameer D'Costa wrote:
| Have you considered using the function as.matrix? It is an R function but you
| can call it from within your Rcpp code as well. Once you have a matrix you can
| use your matrix solution. 

I'd consider Rcpp::Function as a last resort. You do not need it here.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list