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

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

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

#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;

使用 #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
*/