[R] Using .Fortran in R - how can I use file i/o flexibly OR arrays of variable length as arguments?

Peter Green m@pjg @end|ng |rom br|@to|@@c@uk
Fri Feb 4 11:21:11 CET 2022


I have a legacy Fortran code many thousands of lines long, a 
free-standing program that I now want to adapt with minimum re-writing 
to drive an R package, using .Fortran to pass control to a version of 
that legacy code re-written as a subroutine. BUT the output I want to 
return to R is copious, and variable in length, the length being 
computed within the Fortran code (it is a variable-dimension 
simulation). I'm looking for a solution general enough to include in a 
package that would pass CRAN checks.

I can think of two broad approaches

(1) to write this output to files in the Fortran code, to be read back 
later in R.  Of course I know about realpr, etc., but this is pretty 
ugly - apparently there's no binary i/o and no way to direct to a named 
file.

(2) to  dynamically allocate memory in the Fortran code for this output, 
and find a way for the address of this memory to be available in the 
result returned to R by .Fortran. The nearest to a possible solution 
that I can find in "Writing R extensions" involve use of C concepts, 
poorly explained, and I am not sure whether and how these could be used 
in my context.

Advice or information please!

-- 
Peter Green, p.j.green using bristol.ac.uk, peter.green using uts.edu.au
-- 
Emeritus Professor of Statistics & Professorial Research Fellow, University
of Bristol, Bristol BS8 1UG, UK
Phone: +44 117 428 4845; Fax: +44 117 928 7999
-- 
Distinguished Professor of Statistics, University of Technology, Sydney
Room: CB07.05.051; PO Box 123, Broadway NSW 2007, Australia
Phone: +61 2 9514 1742; Fax: +61 2 9514 2260



More information about the R-help mailing list