Rcpp debug - 致命错误:Datetime.h:没有这样的文件或目录; xtsAPI.h:没有这样的文件或目录

时间:2015-04-17 18:00:51

标签: c++ r datetime xts rcpp

我正在使用Rcpp来处理Datetime和xts数据。但是,我在第2行和第2行都收到错误No such file or directory错误。以下3个代码:

#include <Rcpp.h>
#include <Datetime.h>
#include <xtsAPI.h>
// [[Rcpp::depends(xts)]
using namespace Rcpp;
using namespace std;

这是我得到的错误:

fatal error: Datetime.h: No such file or directory; 
fatal error: xtsAPI.h: No such file or directory;

1 个答案:

答案 0 :(得分:4)

使用#include <Rcpp/Datetime.h>代替#include <Datetime.h>并确保已安装RcppXts - 然后您应该能够使用这两个库:

if(!"RcppXts" %in% installed.packages()[,1]) {
  install.packages("RcppXts")
}

#include <Rcpp.h>
#include <Rcpp/Datetime.h>
#include <xtsAPI.h>
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(xts)]]

/*
 * http://gallery.rcpp.org/articles/accessing-xts-api/
 */

// [[Rcpp::export]]
Rcpp::NumericVector createXts(int sv, int ev) {

    Rcpp::IntegerVector ind = Rcpp::seq(sv, ev);     // values

    Rcpp::NumericVector dv(ind);               // date(time)s are real values
    dv = dv * 86400;                     // scaled to days
    dv.attr("tzone")    = "UTC";         // the index has attributes
    dv.attr("tclass")   = "Date";

    Rcpp::NumericVector xv(ind);               // data her same index
    xv.attr("dim")         = Rcpp::IntegerVector::create(ev-sv+1,1);
    xv.attr("index")       = dv;
    Rcpp::CharacterVector klass  = Rcpp::CharacterVector::create("xts", "zoo");
    xv.attr("class")       = klass;
    xv.attr(".indexCLASS") = "Date";
    xv.attr("tclass")      = "Date";
    xv.attr(".indexTZ")    = "UTC";
    xv.attr("tzone")       = "UTC";

    return xv;

}

// [[Rcpp::export]]
Rcpp::NumericVector rbindXts(Rcpp::NumericMatrix ma, Rcpp::NumericMatrix mb, bool dup=true) {
  Rcpp::NumericMatrix mc = xtsRbind(ma, mb, Rcpp::wrap(dup));
  return mc;
}


// [[Rcpp::export]]
Rcpp::LogicalVector match_date(Rcpp::Datetime d, Rcpp::DatetimeVector dv) {

  Rcpp::LogicalVector lv(dv.size());

  std::transform(dv.begin(), dv.end(), lv.begin(), 
                 [&](Rcpp::Datetime dIn) -> bool {
                   return dIn == d;
                 });
  return lv;
}

/*** R
D <- Sys.time()
Dv <- seq.POSIXt(from = D - 3600*24*3, to = D + 3600*24*3, by="day")

match_date(D, Dv)
# [1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE

x1 <- createXts(2,5)
x2 <- createXts(4,9)

rbindXts(x1, x2)
#            [,1]
# 1970-01-03    2
# 1970-01-04    3
# 1970-01-05    4
# 1970-01-06    5
# 1970-01-07    6
# 1970-01-08    7
# 1970-01-09    8
# 1970-01-10    9
*/