2013-01-11 21 views
5

में xts पैकेज के सी एपीआई का उपयोग कैसे करें xts-0.9-1 पैकेज द्वारा प्रदान किए गए सी के लिए xts_API सीधे सी ++ में उपयोग नहीं किया जा सकता है।आरसीपीपी

उदाहरण के लिए

, अगर एक लिखने

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

निम्न हो जाएगा संकलक समय त्रुटि:

  • 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

कैसे सी के लिए xts_API आह्वान करने के लिए?

उत्तर

5

आपके पास xts का कौन सा संस्करण है? मेरे लिए निम्नलिखित काम करता है:

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") 

कौन सा मैं चला सकते हैं:

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> 

को शामिल ध्वज सेटिंग सामान्यीकृत किए जाने की आवश्यकता है, लेकिन है कि कुछ अगर आप rcpp- लिए आया था हम पर काम कर सके है डेवेल सूची।

संपादित करें: मैंने एक ऐड-ऑन पैकेज के साथ प्रयोग करना शुरू किया जो एक्सटीएस (वर्तमान में कुछ और सीमित) एपीआई के साथ इंटरफेस करता है; आर-फोर्ज पर आरसीपीपी एसवीएन रेपो में देखें। मैंने आरसीपीपी गैलरी में एक नया जवाब भी जोड़ा जोसी ++ कोड से। वहाँ ज्यादा बेहतर तरीके विशेषताओं को पाने के लिए तो यहां इस्तेमाल किया (आर के सी एपीआई के आधार पर) (Rcpp एपीआई का उपयोग कर) कर रहे हैं।

संपादित करें 2: अब एक नया पैकेज RcppXts है जो इससे मदद करता है।

+1

अतिरिक्त बहिर्वाहिक गतिविधि के लिए +1 – GSee

1

निम्नलिखित मार्गदर्शिका आर पैकेज विकास के लिए है।

कुंजी इनलाइन फ़ंक्शन और मैक्रो को जोड़ने के लिए xts_API को 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 
} 

ऊपर कोड SET_xtsIndexClass को छोड़कर लगभग सभी xts_API के लिए काम करना चाहिए।

कंपाइलर अभी भी error: ‘xts_IndexvalueSymbol’ was not declared in this scope की रिपोर्ट करेगा।

मेरा समाधान यहां है लेकिन मुझे नहीं पता कि यह सही है या नहीं।

<xts package root>/include/xts.h और बदल

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

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

मुझे लगता है कि यह एक गलती है तो खुला है।

संबंधित मुद्दे