[Rd] Calling C function from Fortran

From: Gilles GUILLOT <gilles.guillot_at_inapg.inra.fr>
Date: Wed 15 Jun 2005 - 11:41:37 GMT

<div class="mail">
<address class="headers">
<span id="from">
<dfn>From</dfn>: Gilles GUILLOT <gilles.guillot_at_inapg.inra.fr>
<span id="date"><dfn>Date</dfn>: Wed 15 Jun 2005 - 11:41:37 GMT</span>


Hi all,

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)

<a href="http://tolstoy.newcastle.edu.au/R/devel/05/04/0693.html#1301qlink1">#include <R.h>
<em class="quotelev1">#include <Rmath.h>
</em><br>void F77_SUB(mygammac)(double x, double y) { y = gammafn(x); }

   subroutine mygammaf(x,y)

   double precision x,y

   call mygammac(x,y)


called in R through

x <- 3

y <- -999

res <- .Fortran("mygammaf",


While changing the C code into
<a href="http://tolstoy.newcastle.edu.au/R/devel/05/04/0693.html#1301qlink2">#include <R.h>
<em class="quotelev1">#include <Rmath.h>
</em><br>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



R-devel@r-project&#46;<!--nospam-->org mailing list
<a href="https://stat.ethz.ch/mailman/listinfo/r-devel">https://stat.ethz.ch/mailman/listinfo/r-devel</a>
<span id="received"><dfn>Received on</dfn> Wed Jun 15 21:44:23 2005</span>
Received on Wed Jun 15 21:44:23 2005

This archive was generated by hypermail 2.1.8 : Mon 24 Oct 2005 - 23:17:04 GMT