[R] Calling R (GAMM) from Fortran

Katharine Mullen kate at few.vu.nl
Fri Nov 20 03:40:38 CET 2009


Yes, as Charlie said you can write a small C wrapper that calls R.  The C
wrapper can then be called from Fortran.

For some examples:

see the package minpack.lm
(http://cran.r-project.org/web/packages/minpack.lm/index.html); the C
wrappers are the functions fcn_lmdif and fcn_lmder.c.

Maybe more transparently:  see the package minqa on R-forge
(https://r-forge.r-project.org/src/contrib/minqa_1.0.tar.gz)

There an objective function in Fortran looks like this:
SUBROUTINE CALFUN (N,X,F)
      call resfun(N, X, F);
      RETURN
      END
and resfun is a simple C function that evaluates a call to an R function.

On Thu, 19 Nov 2009, Charlie Sharpsteen wrote:

> On Thu, Nov 19, 2009 at 12:14 PM, Paul Warren Simonin
> <Paul.Simonin at uvm.edu> wrote:
> >
> >
> >   Hello,
> >
> >     I am currently working on a modeling project using Fortran to run
> > large repetitive loops (many DO loops). As part of this process I
> > would like to use a model fit in R and currently stored as an R
> > object. This is a rather complex model, a GAMM, and I am curious
> > whether there is a way to call this model from Fortran. I am not sure
> > "call" is correct terminology, but I would basically like to use this
> > GAMM to make a prediction as part of each DO loop. Is this possible? I
> > have found instructions, etc. for calling Fortran from R but not vice
> > versa.
> >
> >     Thank you for any information or advice. General advice to
> > address this situation is also welcome.
> >
> >   Thanks again!
> >
> >   -Paul Simonin
>
>
> The R library and interpreter are written in C and available using a C
> interface.  Therefore, it  would be theoretically possible to call
> this interface from Fortran-- however there are details that probably
> make a direct call impossible in practice.  This is because
> interfacing with R requires passing representations of R objects
> (SEXPs) which are far removed from the basic C variable types
> supported by standardized C--Fortran interfaces.
>
>  It may be that the only plausible way to approach this problem may
> involve writing a set of bindings in C that interface with R and
> extract the information you are interested in and then express it
> using basing C variables that Fortran can understand.  With such a
> system in place your Fortran program would call a set of routines
> written in C that called routines from the R libraries and then
> reformatted the results and returned them to Fortran.
>
> This is a conclusion based on my limited experience-- not any formal
> expertise.  There are definitely others on this list that could
> provide a better qualified answer.
>
> Good luck!
>
> -Charlie
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list