Skip to content
Prev 52106 / 63424 Next

Calling C implementations of rnorm and friends

On Fri, Jul 1, 2016 at 6:13 AM, Luis Usier
<luis.henrique.usier at gmail.com> wrote:
There may be ways to call unexported functions that R CMD check will
not detect, but you should not look for ways to violate the spirit of
the policy.  Work-arounds include asking the package maintainer to
export the functionality you want, or you can copy the desired
functionality to your package (with attribution, of course).

In this particular case, you can probably get what you want via the
Rmath library.  It provides C-level access to all the distribution
functions (and more).  You would need something like this in your C
code:

#include <Rmath.h>
GetRNGstate();
double rn = rnorm(0.0, 1.0);
PutRNGstate()

Once Dirk recovers from useR!, he'll tell you how this is all
super-easy if you just use Rcpp. ;)
See the sections "Functions that call compiled code" and "Compiled
code in a base package" from this answer:
http://stackoverflow.com/a/19226817/271616

That should give you a few pointers on where/how to look.