2013-01-11 11 views
5

XTS_API dla C dostarczonego przez pakiet xts-0.9-1 nie może być bezpośrednio użyty w C++.Jak używać pakietu C api xts w Rcpp

Na przykład, jeśli ktoś napisać

#include <Rcpp.h> 
extern "C" { 
#include <xts.h> 
} 

using namespace Rcpp; 

RcppExport SEXP get_xts_index(SEXP x, SEXP value) { 
    BEGIN_RCPP 

    return SET_xtsIndexClass(x, value); 

    END_RCPP 
} 

Nie będzie następujący błąd czas kompilator:

  • error: expected identifier before ‘)’ token
  • error: ‘install’ was not declared in this scope
  • error: ‘getAttrib’ was not declared in this scope
  • error: ‘setAttrib’ was not declared in this scope
  • error: ‘xts_IndexvalueSymbol’ was not declared in this scope

Jak wywołać xts_API dla C?

Odpowiedz

5

Jaką wersję Xts posiadasz? Następujące roboty dla mnie:

library(xts) 
library(inline) 

inc <- ' 
extern "C" { 
#define class xts_class 
#include <xts.h> 
#undef class 
} 


inline SEXP install(const char* x) { 
    return Rf_install(x); 
} 

inline SEXP getAttrib(SEXP a, SEXP b) { 
    return Rf_getAttrib(a, b); 
} 


inline SEXP setAttrib(SEXP a, SEXP b, SEXP c) { 
    return Rf_setAttrib(a, b, c); 
} 

#include <Rcpp.h> 
' 

src <- ' 
    return GET_xtsIndexClass(x); 
' 

Sys.setenv("PKG_CXXFLAGS"="-I/usr/local/lib/R/site-library/xts/include") 
xtsfun <- cxxfunction(signature(x="ANY"), body=src, inc=inc, plugin="Rcpp") 

Które mogę uruchomić:

R> xtsfun <- cxxfunction(signature(x="ANY"), body=src, inc=inc, plugin="Rcpp") 
R> foo <- xts(1:5, order.by=Sys.time()+0:4) 
R> xtsfun(foo) 
[1] "POSIXct" "POSIXt" 
R> 

include ustawienie flagi musi być uogólnione, ale to jest coś, co moglibyśmy pracować jeśli podszedł do rcpp- lista rozwijana.

Edytuj: Zacząłem eksperymentować z pakietem dodatków, który łączy się z (obecnie nieco bardziej ograniczonym) API xts; zobacz w repozytorium SVN Rcpp na R-Forge. Dodałem też nową odpowiedź do Galerii Rcpp, która jest show how to access xts components z kodu C++. Istnieje wiele lepszych sposobów na uzyskanie dostępu do atrybutów (przy użyciu API Rcpp), które są tutaj używane (w oparciu o R API C).

Edit 2: Jest teraz nowy pakiet RcppXts że pomaga w tym.

+1

+1 za aktywność pozaszkolną – GSee

1

Poniższy poradnik dotyczy tworzenia pakietów R.

Kluczem jest dodanie niezbędnej funkcji inline i makra, aby uczynić XTS_API kompatybilnym z C++.

extern "C" { 
#define class xts_class 
#include <xts.h> 
#undef class 
} 


inline SEXP install(const char* x) { 
    return Rf_install(x); 
} 

inline SEXP getAttrib(SEXP a, SEXP b) { 
    return Rf_getAttrib(a, b); 
} 


inline SEXP setAttrib(SEXP a, SEXP b, SEXP c) { 
    return Rf_setAttrib(a, b, c); 
} 

#include <Rcpp.h> 

RcppExport SEXP get_xts_index(SEXP x, SEXP value) { 
    BEGIN_RCPP 

    return GET_xtsIndexClass(x); 

    END_RCPP 
} 

Powyższy kod powinien działać dla prawie wszystkich xts_API oprócz SET_xtsIndexClass.

Kompilator nadal będzie raportować error: ‘xts_IndexvalueSymbol’ was not declared in this scope.

Oto moje rozwiązanie, ale nie wiem, czy to prawda, czy nie.

Otwarte <xts package root>/include/xts.h i zmienić

#define SET_xtsIndexClass(x,value)  setAttrib(x, xts_IndexvalueSymbol, value) 

do

#define SET_xtsIndexClass(x,value)  setAttrib(x, xts_IndexClassSymbol, value) 

myślę, że to literówka.