Отладка Rcpp - фатальная ошибка: Datetime.h: нет такого файла или каталога; xtsAPI.h: нет такого файла или каталога

Я использую Rcpp для обработки данных Datetime и xts. Однако я получаю сообщение об ошибке No such file or directory в обеих строках 2 и 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;

person Alvin    schedule 17.04.2015    source источник


Ответы (1)


Используйте #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
*/
person nrussell    schedule 17.04.2015
comment
Тщательно с ответом. - person Dirk Eddelbuettel; 17.04.2015