how can I handle vectors without knowing the type in Rcpp

前端 未结 2 1150
你的背包
你的背包 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: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 
    using namespace Rcpp ;
    
    template 
    Vector first_two_impl( Vector xin){
        Vector 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 
    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 
    // [[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 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
    

提交回复
热议问题