Search code examples
crexternal

How to call dpois_raw C stats routine from R


I'm trying to figure out a way to call stats package's "dpois_raw" routine rather than the "dpois" wrapper using .Call .External or whatever means.

"dpois_raw" is not listed in the package environment (stats:::C_*) nor when I do getDLLRegisteredRoutines("stats") so I am probably out of luck, but I wonder whether someone R/C expert knows of a workaround.


Solution

  • The dpois_raw routine is provided by the Rmath.h header, and it doesn't appear to actually be exposed as part of the stats package (or otherwise); however, it is made available to C / C++ code through the Rmath.h header.

    The simplest way to expose it would be with your own C / C++ code exposing that code, e.g. (code stub)

    #include <R.h>
    #include <Rmath.h>
    
    SEXP my_dpois_raw(<...>) {
       double result = dpois_raw(<...>);
       return result;
    }
    

    This routine would then be callable from R with something like

    .Call("my_dpois_raw", <...>)
    

    See Hadley's r-pkgs section on using compiled code in R packages for some more information on including C / C++ code in an R package.