[R] Calling C function from Fortran
Gilles GUILLOT
gilles.guillot at inapg.inra.fr
Wed Jun 15 16:04:57 CEST 2005
The example in the R doc
and the hints from Shusong Jin , Ingmar Visser and Reid Huntsinger
(thanks all three) refer to the case where the function does not have
arguments.
I'm still looking for a proper sequence of commands
to call C functions with arguemnts from R.
Imagine I want to evaluate the gamma function.
I want to use the C function called by R.
(I guess it is the one corresponding to the source code
I found in the directory R-2.1.0/src/nmath/gamma.c
of the source distribution).
The following programs do not work (it returns fancy values)
#include <R.h>
#include <Rmath.h>
void F77_SUB(mygammac)(double x, double y) { y = gammafn(x); }
subroutine mygammaf(x,y)
double precision x,y
call mygammac(x,y)
end
called in R through
x <- 3
y <- -999
res <- .Fortran("mygammaf",
as.double(x),
as.double(y))
While changing the C code into
#include <R.h>
#include <Rmath.h>
void F77_SUB(mygammac)(double *x, double *y) { *y = gammafn(*x); }
seems to work fine.
But R-2.1.0/src/nmath/gamma.c does not need a pointer ?
What is wrong whit he first set of lines ?
What is the correct way to call the C function in R-2.1.0/src/nmath/gamma.c ?
Thanks in advance
Gilles
More information about the R-help
mailing list