Reputation: 49448
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")
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.
What's the correct Rcpp
way of doing this?
Upvotes: 14
Views: 2231
Reputation: 17642
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.
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 )
*/
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 ;
}
The template parameter T
only needs:
int
operator[](int)
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
Upvotes: 13
Reputation: 368221
You need to pick a type (ie do not use signature="SEXP"
[ oh and you should look into Attributes anyway ]).
Or you keep the SEXP
type, and dispatch internally. See for example this post on the 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.
Upvotes: 0