Search code examples
c++rcpp

Using Rccp as a C++ STL without using using R


I understand that Rcpp is an R package for integration between C++ and R. I have a code which uses Rcpp for this. But I am looking to use the C++ code directly. Is it possible to get Rcpp as any usual C++ STL so that I can use the functions of Rcpp as it is (eg: the as in Rcpp)?


Solution

  • Sure:

    R> cppFunction("double mysum(std::vector<double> x) { \
                       double s = std::accumulate(x.begin(), x.end(), 0.0); return s;}")
    R> mysum(c(0.1, 0.2, 0.3))
    [1] 0.6
    R> 
    

    There are plenty of STL examples in the documentation, the Rcpp Gallery and other places as e.g. my talks. Or of course the Rcpp book.