Convert std::vector to Rcpp matrix

后端 未结 2 1757
南笙
南笙 2020-12-31 05:48

This is an Rcpp conversion related Q. I\'m looking to convert a long std::vector into a Rcpp matrix object, but want to know if there is an easy conversion format. Naturally

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 06:09

    You chose a good approach. In the OpenMP context, you have to stay away from the single-threaded R. So std::vector is good.

    And yes, we have constructors from std::vector to Rcpp::NumericVector and Rcpp::NumericMatrix (which is after all just a vector with a dimensions attribute) as well as the Rcoo:Integer* variants, as well as the usual as<> and wrap converters.

    There should be plenty of examples in the Rcpp unit tests, the Rcpp examples, and of course on the Rcpp Gallery site.

    Edit: Here is an overly simple example:

     R> cppFunction('NumericVector phil(int n) { std::vector x(n);
     +              return wrap(x); }') 
     R> phil(4)  
     [1] 0 0 0 0    
     R>    
    

提交回复
热议问题