I'm writing an Rcpp module an would like to return as one element of the RcppResultSet list a list whose elements are vectors. E.g., .Call("myfunc")$foo
should be something like:
我正在编写一个Rcpp模块,希望作为RcppResultSet列表中的一个元素返回一个元素是向量的列表。例如:. 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. 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!). I've tried constructing an RcppList
but have a hard time casting various objects (like RcppVector
) safely into SEXP
s.
(确切的数字在这里并不重要)。问题是我不知道正确的Rcpp方法。我试过传递一个向量 <矢量
Anyone have tips on best practices for dealing with complicated structures such as lists of vectors in Rcpp?
任何人都有关于处理复杂结构的最佳实践的技巧,比如Rcpp中的向量列表?
2 个解决方案
#1
39
[ 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. ]
很高兴看到这个,但罗曼和我通常推荐rccp-devel列表。当项目还没有那么大的时候,请张贴在那里,并保证有问题散布在网上。]
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.
RcppResultSet是旧的经典API的一部分,而大量的工作已经进入了我们所称的新API(从0.7开始)。*版本)。看一下CRAN的当前Rcpp页面,以及“vignettes”的列表——6个,然后计数。
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.
列表还应该可以列出列表的列表…虽然我不确定我们是否有单元测试,但是在500+单元测试中有很多例子。
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
当它发生时,我花了几天时间将许多RQuantLib代码从经典API转换为新的API。一旦我们得到Rcpp的0.8.3版本(希望在几天内),这可能会得到释放。同时,您可以查看RQuantLib SVN存档。
#2
21
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
#1
39
[ 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. ]
很高兴看到这个,但罗曼和我通常推荐rccp-devel列表。当项目还没有那么大的时候,请张贴在那里,并保证有问题散布在网上。]
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.
RcppResultSet是旧的经典API的一部分,而大量的工作已经进入了我们所称的新API(从0.7开始)。*版本)。看一下CRAN的当前Rcpp页面,以及“vignettes”的列表——6个,然后计数。
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.
列表还应该可以列出列表的列表…虽然我不确定我们是否有单元测试,但是在500+单元测试中有很多例子。
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
当它发生时,我花了几天时间将许多RQuantLib代码从经典API转换为新的API。一旦我们得到Rcpp的0.8.3版本(希望在几天内),这可能会得到释放。同时,您可以查看RQuantLib SVN存档。
#2
21
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