rrcpprinside

How to copy Rcpp::DateVector to std::vector<boost::gregorian::date>


I want to pass data from zoo object into my program in C++ using Rinside, but I don't know how to pass date.

Here is an example

 RInside R(argc, argv);              // create an embedded R instance


std::string cmd = "suppressMessages(library(zoo)); "
                  "z <- zoo(rnorm(10), as.Date('2000-01-01') - 0:10);"; 

R.parseEvalQ(cmd); 

 std::vector<double> v = Rcpp::as< std::vector< double > >(R.parseEval("coredata(z)"));
Rcpp::DateVector   d ( (SEXP) R.parseEval("index(z)") );

std::vector<boost::gregorian::date> dt =   //How assign d to dt ?

Solution

  • You need simple converters such as this in the RcppBDT package:

    template <> boost::gregorian::date as( SEXP dtsexp ) {
        Rcpp::Date dt(dtsexp);
        return boost::gregorian::date(dt.getYear(), dt.getMonth(), dt.getDay());
    }
    

    which you then need to vectorise. Alternative, maybe use integer vector (with days since epoch).

    Edit: An there is an entire Rcpp Gallery post devoting to this, as well as several more dealing in related topics.