[R-pkg-devel] R_registerRoutines, etc.

Duncan Murdoch murdoch.duncan at gmail.com
Mon Apr 24 01:36:44 CEST 2017


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.


> I have a (very vague) understanding that in C one uses variables of int
> type (taking the values 0, for FALSE, and 1, for TRUE) as logical variables.

It's really "nonzero for TRUE", hence the test above.

>
> On that understanding I took a punt and replaced "void *" by "int *" for
> the logical type variable.  The package built and passed
>
>      "R CMD check --as-cran"
>
> so it seems that this is OK.  Is this the Right Thing To Do?  Are there
> any (obvious?) lurking perils?

Lots of lurking perils.  You got lucky; don't count on it.

Duncan Murdoch

>
> Thanks.
>
> cheers,
>
> Rolf
>



More information about the R-package-devel mailing list