Best way to use c++ code from R package FOO in package BAR

后端 未结 3 804
星月不相逢
星月不相逢 2021-02-06 01:20

I am trying to define a function using Rcpp for speedup. The situation is as follows:

  • I have a package FOO with a lot of C++ code (my own package and currently not
3条回答
  •  忘掉有多难
    2021-02-06 02:20

    The RcppXts package does just that for a bunch of functions from the well-known xts package.

    Edit: Here is some code from xts.

    First, xts::src/init.c does the registration via a dozen or so declarations like

    R_RegisterCCallable("xts","do_is_ordered",(DL_FUNC) &do_is_ordered);
    

    Second, xts::inst/include/xtsApi.h provides a header for client packages with eg

    SEXP attribute_hidden xtsIsOrdered(SEXP x, SEXP increasing, SEXP strictly) {
        static SEXP(*fun)(SEXP,SEXP,SEXP) = NULL;
        fun = (SEXP(*)(SEXP,SEXP,SEXP)) R_GetCCallable("xts","do_is_ordered");
        return fun(x, increasing, strictly);
    }
    

    Third, in a client package such as RcppXts we define this (using Rcpp Modules) as

    function("xtsIsOrdered",
             &xtsIsOrdered,
             List::create(Named("x"),
                          Named("increasing") = true,
                          Named("strictly") = true),
             "Tests whether object is (strictly) (increasing) ordered");
    

    which exposes it to R. We could equally well call the C function xtsIsOrdered from C++ code.

    I removed the incorrect earlier comment that functions have to conform to 'SEXP in, SEXP out'.

提交回复
热议问题