using C function from other package in Rcpp

前端 未结 3 635
北恋
北恋 2020-12-05 14:54

I\'m trying to call a C routine from the cubature package in a c++ function to perform multidimensional integration.

The basic R example I\'m trying to reproduce is

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 15:39

    Didn't see this question earlier, and it looks like @Romain addressed it.

    For completeness, a working example of how to do this when all parties play along is provided by the xts and RcppXts packages. In xts, we do this (for about ten functions) in the (source) file inst/include/xtsAPI.h:

    SEXP attribute_hidden xtsLag(SEXP x, SEXP k, SEXP pad) {     
        static SEXP(*fun)(SEXP,SEXP,SEXP) = NULL;         
        if (fun == NULL)                                  
            fun = (SEXP(*)(SEXP,SEXP,SEXP)) R_GetCCallable("xts","lagXts");   
        return fun(x, k, pad);                               
    }  
    

    along with the usual business of R_registerRoutines and R_RegisterCCallable.

    In RcppXts this is picked up (in an Rcpp Module) as

    function("xtsLag", 
             &xtsLag,    
             List::create(Named("x"), Named("k"), Named("pad")),   
             "Extract the coredata from xts object");
    

    which works pretty well. Someone reprimanded me to write the xts side more compactly (as the if NULL is spurious) which I will get to ... eventually.

提交回复
热议问题