This is a toy R package to serve as an example for statistical confusing classes.
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 foo
(package/foo/R/foo.R)
that calls either C or FORTRAN depending on an optional argument type
.
The functions that it calls are
foo.f
called via the R function.Fortran
bar.c
called via the R function.C
baz.c
called via the R function.Call
(which allows manipulating R objects of all types in C, but which is harder to use than the.C
interface)
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.
It 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.f
called via the R function.Fortran
qux.c
called via the R function.C
quux.c
called via the R function.Call
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(foo)
, 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?
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
functions pbeta
, qbeta
, dbeta
, rbeta
that according to the comments
in Rmath.h
are for the beta distribution and also pnbeta
and so forth
that are for the noncentral beta distribution. We probably want rbeta
.
So we look at the source for that
Although it is not totally clear from the comments, it seems that the argments 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.