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

Sameer D'Costa sameerdcosta at gmail.com
Thu May 19 13:12:06 CEST 2016


Hi,

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.

Rgds
Sameer

On Thu, May 19, 2016 at 5:46 AM Paul Saary <rcpp at paulsaary.de> 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>>.
>
> Currently my function looks like this:
>
> List handleRObject(SEXP Robj){
>         std::vector<vector<double>> internalMatrix;
>
>         // convert Robj to internalMatrix
>         // thats where I am stuck
>
>         // do some stuff and return List
>         return(wrap(someoutput))
> }
>
> I tried many differnt things (using as, switch statements, etc...), but
> I do not seem to fully grasp on how tonhandle this problem, as none of
> them worked.
>
> I can transform a NumericMatrix using this code, but could not addapt
> this to a more general approach.
>
>         // use rMatrix as input
>         int nc = rMatrix.ncol();
>         internalMatrix.resize(nc);
>         for( int i=0; i<nc; i++){
>             NumericMatrix::Column col = rMatrix(_,i) ;
>             internalMatrix[i].assign( col.begin() , col.end() ) ;
>         }
>
> I though, maybe someone can give me a hint on how to solve this?
>
> Bests,
>
> Paul
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160519/b53f7efc/attachment.html>


More information about the Rcpp-devel mailing list