简体   繁体   English

如何在Rcpp中创建向量列表?

[英]How do I create a list of vectors in Rcpp?

I'm writing an Rcpp module an would like to return as one element of the RcppResultSet list a list whose elements are vectors. 我正在编写一个Rcpp模块,希望作为RcppResultSet列表的一个元素返回一个列表,其元素是向量。 Eg, .Call("myfunc")$foo should be something like: 例如, .Call("myfunc")$foo应该是这样的:

[[1]]
[1] 1

[[2]]
[1] 1 1

[[3]]
[1] 1 1 1

(the exact numbers are not important here). (这里确切的数字并不重要)。 The issue is that I don't know the right Rcpp way of doing this. 问题是我不知道正确的Rcpp方式。 I tried passing a vector<vector<int> > but this constructs a matrix by silently taking the length of the first vector as the width (even if the matrix is ragged!). 我尝试传递一个vector<vector<int> >但这通过静默地将第一个向量的长度作为宽度来构造矩阵(即使矩阵是粗糙的!)。 I've tried constructing an RcppList but have a hard time casting various objects (like RcppVector ) safely into SEXP s. 我已经尝试构建一个RcppList但很难安全地将各种对象(如RcppVector )转换为SEXP

Anyone have tips on best practices for dealing with complicated structures such as lists of vectors in Rcpp? 任何人都有关于处理复杂结构的最佳实践的提示,例如Rcpp中的向量列表?

[ Nice to see this here but Romain and I generally recommend the rccp-devel list for question. [很高兴看到这里,但Romain和我一般推荐rccp-devel列表提问。 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). RcppResultSet是旧版经典 API的一部分,而很多工作已经进入了我们所谓的 API(从0.7。*版本开始)。 Have a look at the current Rcpp page on CRAN and the list of vignettes -- six and counting. 看看CRAN上的当前Rcpp页面和小插图列表 - 六个和计数。

With new API you would return something like 使用新API,您将返回类似的内容

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 所有在一个语句中(可能使用显式的Rcpp::wrap()调用),创建R中的内容

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. 并且Rcpp::List也应该能够列出列表列表......虽然我不确定我们是否有这样的单元测试---但是在500多个单元测试中有很多例子。

As it happens, I spent the last few days converting a lot of RQuantLib code from the classic API to the new API. 碰巧的是,我花了最后几天将大量RQuantLib代码从经典API转换为新API。 This will probably get released once we get version 0.8.3 of Rcpp out (hopefully in a few days). 一旦我们得到Rcpp的 0.8.3版本(希望在几天内),这可能会被释放。 In the meantime, you can look at the RQuantLib SVN archive 在此期间,您可以查看RQuantLib SVN存档

I would tend to use a compressed variation of Dirk's solution: 我倾向于使用Dirk解决方案的压缩变体:

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. 另外,回到原始问题, vector< vector<int> >会将自身包装到整数向量列表中,而不是矩阵。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM