I want to replicate the following R function in Rcpp
:
我想在Rcpp中复制以下R函数:
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")
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.
但这只适用于整数,如果我尝试用List(或GenericVector,它们是相同的)替换xout类型 - 它适用于任何输入类型,但我返回列表而不是向量。
What's the correct Rcpp
way of doing this?
Rcpp这样做的正确方法是什么?
2 个解决方案
#1
9
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类型上使用push_back。当前实现Rcpp向量的方式需要每次都复制所有数据。这是一项非常昂贵的操作。
We have RCPP_RETURN_VECTOR for dispatching, this requires that you write a template function taking a Vector as input.
我们有RCPP_RETURN_VECTOR用于调度,这要求您编写一个模板函数,将Vector作为输入。
#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 )
*/
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:
只需sourceCpp这个文件,这也将运行调用这两个函数的R代码。实际上,模板可能更简单,这也可以工作:
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 ;
}
The template parameter T
only needs:
模板参数T只需要:
- A constructor taking an
int
- 采用int的构造函数
- An
operator[](int)
- 运算符[](int)
Alternatively, this might be a job for dplyr vector visitors.
或者,这可能是dplyr矢量访问者的工作。
#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
#2
0
You need to pick a type (ie do not use signature="SEXP"
[ oh and you should look into Attributes anyway ]).
你需要选择一种类型(即不要使用signature =“SEXP”[哦,无论如何你应该查看属性])。
Or you keep the SEXP
type, and dispatch internally. See for example this post on the Rcpp Gallery.
或者您保留SEXP类型,并在内部发送。例如,请参阅Rcpp Gallery上的这篇文章。
Edit: And C is of course statically typed. These very switches depending on the type are all over the R sources too. No free lunch here.
编辑:C当然是静态类型。这些非常依赖于类型的开关也遍布R源。这里没有免费午餐。
#1
9
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类型上使用push_back。当前实现Rcpp向量的方式需要每次都复制所有数据。这是一项非常昂贵的操作。
We have RCPP_RETURN_VECTOR for dispatching, this requires that you write a template function taking a Vector as input.
我们有RCPP_RETURN_VECTOR用于调度,这要求您编写一个模板函数,将Vector作为输入。
#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 )
*/
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:
只需sourceCpp这个文件,这也将运行调用这两个函数的R代码。实际上,模板可能更简单,这也可以工作:
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 ;
}
The template parameter T
only needs:
模板参数T只需要:
- A constructor taking an
int
- 采用int的构造函数
- An
operator[](int)
- 运算符[](int)
Alternatively, this might be a job for dplyr vector visitors.
或者,这可能是dplyr矢量访问者的工作。
#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
#2
0
You need to pick a type (ie do not use signature="SEXP"
[ oh and you should look into Attributes anyway ]).
你需要选择一种类型(即不要使用signature =“SEXP”[哦,无论如何你应该查看属性])。
Or you keep the SEXP
type, and dispatch internally. See for example this post on the Rcpp Gallery.
或者您保留SEXP类型,并在内部发送。例如,请参阅Rcpp Gallery上的这篇文章。
Edit: And C is of course statically typed. These very switches depending on the type are all over the R sources too. No free lunch here.
编辑:C当然是静态类型。这些非常依赖于类型的开关也遍布R源。这里没有免费午餐。