How do I create a list of vectors in Rcpp?

别说谁变了你拦得住时间么 提交于 2019-11-28 04:31:40

[ Nice to see this here but Romain and I generally recommend the rccp-devel list for question. Please post there going forward as the project is not yet that large it warrants to have questions scattered all over the web. ]

RcppResultSet is part of the older classic API whereas a lot of work has gone into what we call the new API (starting with the 0.7.* releases). Have a look at the current Rcpp page on CRAN and the list of vignettes -- six and counting.

With new API you would return something like

return Rcpp::List::create(Rcpp::Named("vec") = someVector,
                          Rcpp::Named("lst") = someList,
                          Rcpp::Named("vec2") = someOtherVector);

all in one statement (and possibly using explicit Rcpp::wrap() calls), creating what in R would be

list(vec=someVector, lst=someList, vec2=someOtherVector)

And Rcpp::List should also be able to do lists of lists of lists... though I am not sure we have unit tests for this --- but there are numerous examples in the 500+ unit tests.

As it happens, I spent the last few days converting a lot of RQuantLib code from the classic API to the new API. This will probably get released once we get version 0.8.3 of Rcpp out (hopefully in a few days). In the meantime, you can look at the RQuantLib SVN archive

I would tend to use a compressed variation of Dirk's solution:

using namespace Rcpp ;
return List::create( 
   _["vec"]  = someVector, 
   _["lst"]  = someList, 
   _["vec2"] = someOtherVector
 ) ;

Also, to come back to the original question, vector< vector<int> > should wrap itself to a list of integer vectors, not a matrix. See:

require( Rcpp )
require( inline )
require( RUnit )

fx <- cxxfunction( , '

    std::vector< std::vector<int> > v ;

    std::vector<int> x1(1) ; v.push_back( x1 );
    std::vector<int> x2(2) ; v.push_back( x2 );
    std::vector<int> x3(3) ; v.push_back( x3 );

    return wrap( v ) ;

', plugin = "Rcpp" ) 

I get :

> fx() 

[[1]]
[1] 0

[[2]]
[1] 0 0

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