如何在不知道 Rcpp 类型的情况下处理向量 [英] how can I handle vectors without knowing the type in Rcpp
问题描述
我想在 Rcpp
中复制以下 R 函数:
I want to replicate the following R function in Rcpp
:
fR = function(x) x[1:2]
fR(c(1,2,3))
#[1] 1 2
fR(c('a','b','c'))
#[1] "a" "b"
对于固定输出类型,我可以这样做:
I can do it for a fixed output type like so:
library(inline)
library(Rcpp)
fint = cxxfunction(signature(x = "SEXP"), '
List xin(x);
IntegerVector xout;
for (int i = 0; i < 2; ++i) xout.push_back(xin[i]);
return xout;', plugin = "Rcpp")
但这仅适用于整数,如果我尝试用 List
(或 GenericVector
,它们是相同的)替换 xout
类型) - 它适用于任何输入类型,但我返回的是 list
而不是向量.
But this will only work for integers, and if I try replacing the xout
type with List
(or GenericVector
, which are the same) - it works with any input type, but I get back a list
instead of a vector.
执行此操作的正确 Rcpp
方法是什么?
What's the correct Rcpp
way of doing this?
推荐答案
不要在 Rcpp
类型上使用 push_back
.当前实现 Rcpp 向量的方式需要每次复制所有数据.这是一项非常昂贵的操作.
Don't use push_back
on Rcpp
types. The way Rcpp vectors are currently implemented this requires copying all of the data each time. This is a very expensive operation.
我们有 RCPP_RETURN_VECTOR 用于调度,这要求您编写一个模板函数,将 Vector 作为输入.
We have RCPP_RETURN_VECTOR for dispatching, this requires that you write a template function taking a Vector as input.
#include <Rcpp.h>
using namespace Rcpp ;
template <int RTYPE>
Vector<RTYPE> first_two_impl( Vector<RTYPE> xin){
Vector<RTYPE> xout(2) ;
for( int i=0; i<2; i++ ){
xout[i] = xin[i] ;
}
return xout ;
}
// [[Rcpp::export]]
SEXP first_two( SEXP xin ){
RCPP_RETURN_VECTOR(first_two_impl, xin) ;
}
/*** R
first_two( 1:3 )
first_two( letters )
*/
只需sourceCpp这个文件,这也将运行调用两个功能.实际上,模板可以更简单,这也可以:
Just sourceCpp this file, this will also run the R code which calls the two functions. Actually, the template could be simpler, this would work too:
template <typename T>
T first_two_impl( T xin){
T xout(2) ;
for( int i=0; i<2; i++ ){
xout[i] = xin[i] ;
}
return xout ;
}
模板参数T
只需要:
- 一个带有
int
的构造函数 - 一个
operator[](int)
- A constructor taking an
int
- An
operator[](int)
或者,这可能是 dplyr 向量访问者的工作.
Alternatively, this might be a job for dplyr vector visitors.
#include <dplyr.h>
// [[Rcpp::depends(dplyr,BH)]]
using namespace dplyr ;
using namespace Rcpp ;
// [[Rcpp::export]]
SEXP first_two( SEXP data ){
VectorVisitor* v = visitor(data) ;
IntegerVector idx = seq( 0, 1 ) ;
Shield<SEXP> out( v->subset(idx) ) ;
delete v ;
return out ;
}
访问者让您可以在向量上执行一组操作,而不管其保存的数据类型如何.
visitors let you do a set of things on a vector regardless of the type of data it holds.
> first_two(letters)
[1] "a" "b"
> first_two(1:10)
[1] 1 2
> first_two(rnorm(10))
[1] 0.4647190 0.9790888
这篇关于如何在不知道 Rcpp 类型的情况下处理向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!