Question

I have been doing some test on Rcpp package and it works great.

I have a little problem when passing some CharacterVector from R to C++.

RcppExport SEXP testArray(SEXP Rarr){

        Rcpp::CharacterVector cx(Rarr);
        Rcpp::LogicalVector r(1);
        std::string test = "a";
        r[0] = (*(const char*)cx[0] == *(const char*)test.c_str());
        const char * temp = (const char*)cx;


        return Rcpp::wrap(temp.size());

    }

is it possible to get the size of "Rcpp::CharacterVector cx" based on code above?

something like : cx.size() or length(cx)?

Regards,

Andy

Was it helpful?

Solution

Yes, .size() it is:

R> library(inline)
R> 
R> vecsize <- cxxfunction(signature(x="character"), plugin="Rcpp", body='
+   Rcpp::CharacterVector cx = Rcpp::CharacterVector(x);
+   return Rcpp::wrap( cx.size() );
+ ')
R> vecsize(c("a", "b", "c"))
[1] 3
R> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top