[R-pkg-devel] R_registerRoutines, etc.

Rolf Turner r.turner at auckland.ac.nz
Mon Apr 24 03:35:37 CEST 2017

On 24/04/17 12:39, Duncan Murdoch wrote:
> On 23/04/2017 7:53 PM, Rolf Turner wrote:
>> On 24/04/17 11:36, Duncan Murdoch wrote:
>>> On 23/04/2017 6:18 PM, Rolf Turner wrote:
>>>> On 23/04/17 23:05, Duncan Murdoch wrote:
>>>>>>> Looks like
>>>>>>> extern void F77_NAME(mnnd)(double *, double *, int *, double *,
>>>>>>> double *);
>>>>>>> to me.
>>>> One more (I hope it's the last!) question:
>>>> One of my subroutines has an argument of type *logical*.  There is no
>>>> logical type in C.  So, since I am perforce using C-speak, I cannot
>>>> change "void *" to "void logical".
>>> For questions like this, see Writing R Extensions section 5.2. "Logical"
>>> in R is "int *" in C, and "INTEGER" in Fortran.
>>> This is important:  *there is no way to pass a Fortran "LOGICAL" from R
>>> to Fortran*.
>>> The issue is that different Fortran compilers store LOGICAL in different
>>> ways.  Some are equivalent to int with 0 for FALSE, all else for TRUE,
>>> but not all are.  So don't rely on it.
>>> If you need to call a Fortran subroutine that takes a LOGICAL argument,
>>> you need to write another Fortran subroutine that takes an INTEGER
>>> argument.  Then compute "arg NE 0" (or whatever the Fortran version is
>>> for testing not equal to zero) and pass that to the original routine.
>> Hoo-boy!!!  Some serious code-rewriting called for!
>> Thanks for setting me straight on this.
>> Is it possible that R CMD check could be set up to issue an error, or at
>> least a warning, if it finds "logical" or "as.logical" in the argument
>> lists of calls to .Fortran()?
> The problem is at the other end.  Passing an R logical is fine; it just
> turns into an INTEGER on its way to Fortran.

Yes, but surely this is an indication that the Fortran code being called 
"expects" a logical and thereby might screw up if its way of
converting integers to logicals were problematic.

> I doubt if it is feasible to detect the problem in Fortran, but static
> analysis keeps getting better, so I could be wrong now or in a while.

If it is, or should become, feasible, it seems to me that it should be 
done.   OTOH I gather from other posts that the .Fortran() interface 
will eventually be removed, so the problem (in this form) will go away.

The *real* problem will then be to write C wrappers for all our Fortran 
routines that are (directly) called by R functions.  And this is a task 
that is impossible for people like me who go catatonic when they see C 
code.  So at that point I will just give up and go to the pub.




Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

More information about the R-package-devel mailing list