[R] Calling C function from Fortran

From: Gilles GUILLOT <gilles.guillot_at_inapg.inra.fr>
Date: Thu 16 Jun 2005 - 00:04:57 EST


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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jun 16 00:08:25 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:41 EST