how can I handle vectors without knowing the type in Rcpp

前端 未结 2 1144
你的背包
你的背包 2020-12-05 11:44

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\"
<         


        
相关标签:
2条回答
  • 2020-12-05 12:16

    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.

    0 讨论(0)
  • 2020-12-05 12:23

    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:

    • A constructor taking an int
    • An 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
    
    0 讨论(0)
提交回复
热议问题