foo is a toy R package to serve as an example for statistical
confusing classes. (Actually now two packages, see the end of this README.)
It illustrates calling C or Fortran from R.
Each function that calls C from R needs both the C function and an R function
that calls it (so users don't have to know about this). To illustrate this
we have an R function
that calls either C or FORTRAN depending on an optional argument
The functions that it calls are
foo.fcalled via the R function
bar.ccalled via the R function
baz.ccalled via the R function
.Call(which allows manipulating R objects of all types in C, but which is harder to use than the
All of these functions do the same completely boring task (squaring the elements of a vector). The point is just to show computation moving from R to C and back.
This package also illustrates using the R uniform and nonuniform random number generators inside C or FORTRAN called from R. Again all three interfaces are illustrated
quf.fcalled via the R function
qux.ccalled via the R function
quux.ccalled via the R function
The FORTRAN one is a bit tricky because there is no R FORTRAN API for calling
the random number generators, so we write stub C functions that look like
FORTRAN functions to FORTRAN (a C function
fred called from FORTRAN must be
wrapped with a macro
F77_SUB(fred), see Section 6.6 of the book Writing R Extensions). Then we call these stub functions from
FORTRAN and these stub functions call the correct functions from the R C API.
These stub functions are in
The other question is how does one know that the function in the R C API
that generates beta random variates is called
rbeta? And how does one
know what the arguments of this function mean?
Section 6.3 and
Section 6.7.1 of the book Writing R Extensions are the authoritative
documentation of the R API for random number generation and for the
R "distribution functions" (like
rnorm except that "norm" can be replaced by something else,
but I find these so sketchy that I also look at the headers and source code.
You can find all of the include files for R (the entire public API) by doing
R CMD config --cppflags
Take off the
-I (a C compiler flag) and the rest is the directory where
all the include files are found. Look in them for functions that have beta
in the name. It turns out that there are a lot of them in the file
which can be found on-line or in the R source (if you have it). We see
rbeta that according to the comments
Rmath.h are for the beta distribution and also
pnbeta and so forth
that are for the noncentral beta distribution. We probably want
(actually Section 6.7.1 of the book Writing R Extensions says that
rbeta is the name of the C function we want to call).
So we look at the source for that
Although it is not totally clear from the comments, it seems that the arguments are the two shape parameters and each call returns one beta random variate So that's what we need to know to use this function.
foo illustrates the way you write this stuff as a beginner
(at writing R packages, not a beginner at R).
fooRegister illustrates the way you write this stuff as an expert.
The difference is registration of native routines.
Comparing the two packages shows what is required to do it the hard way
(the expert way). The expert way really is better, but it is not recommended
CRAN package checks now complain if you do not do this registration of
native routines as illustrated in R package