[R] R-Fortran question (multiple subroutines)
Berwin A Turlach
berwin at maths.uwa.edu.au
Mon Oct 25 14:01:06 CEST 2010
G'day all,
On Mon, 25 Oct 2010 06:52:15 -0400
Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
> Remko Duursma wrote:
[...]
> > I.e, my code looks something like this:
> >
> > subroutine f(x,y,z)
> >
> > call g(x,y,z)
> >
> > end
> >
> > subroutine g(x,y,z)
> >
> > z = x*y
> >
> > end
> >
> >
> > calling this from R shows that subroutine g is not called. The code
> > compiled as executable works fine.
>
> There are no such limitations imposed by R. I'd suggest your
> diagnosis of the problem is wrong. If you can't spot the problem,
> please post a real example (simplified if possible, but not as much
> as the one above).
Actually, it turns out that this example is simplified enough. :)
I put this snippet into a file, compiled it via "R CMD SHLIB", loaded
it into R and then was very surprised about the result of ".Fortran("f",
x=1.1, y=2.2, z=0.0)".
Eventually it dawned to me, Remko needs to read the table in section
5.2 of "Writing R Extensions". The simple fix in this case is to put
a "double precision x, y, z" at the beginning of each subroutine. The
default precision in FORTRAN is not double precision, that's why the
code seems to work when compiled as stand alone but not when called
from R.
In general, I would recommend to start each subroutine with "implicit
none" (o.k., I know that this is not standard FORTRAN77 but most
compiler support that construct; the alternative, that would confrom
with the FORTRAN77 standard, is to declare everything to be implicitly
of type character and then let the fun begin) and then to explicitly
declare all variables to be of the appropriate type.
HTH.
Cheers,
Berwin
More information about the R-help
mailing list