[R] Conerned about Interfacing R with Fortran

Berwin A Turlach berwin at maths.uwa.edu.au
Fri Mar 27 04:18:58 CET 2009

G'day Maura,

On Thu, 26 Mar 2009 18:21:01 +0100
<mauede at alice.it> wrote:

> I am reading the manual sections illustrating how to call a Fortran
> subroutine from R. I feel uneasy at the explicit statement about
> ".Fortran" interface working with Fortran 77. I would like to call a
> Fortran-90 subroutine from my R script. Is that supported at all ?

Read the completely manual. :)  It is pretty easy to use
acroread, or other PDF readers, to search for "Fortran" in the PDF
file; the HTML version should also be searchable from your browser.

In chapter 1 (page 7 of the PDF 2.8.1 version) of the Writing R
Extensions manual, you will find:

  [...] providing support for C, C++, FORTRAN 77, Fortran
  9x at footnote{Note that Ratfor is not supported. If you have Ratfor
  source code, you need to convert it to FORTRAN.  Only FORTRAN-77
  (which we write in upper case) is supported on all platforms, but most
  also support Fortran-95 (for which we use title case).  If you want to
  ship Ratfor source files, please do so in a subdirectory of @file{src}
  and not in the main subdirectory.}, Objective C [...]

and later in chapter 1, there is a complete section (namely 1.2.3) on
F95 code:

  @subsection Using F95 code

  @R{} currently does not distinguish between FORTRAN 77 and Fortran
  90/95 code, and assumes all FORTRAN comes in source files with
  extension @file{.f}.  Commercial Unix systems typically use a F95
  compiler, but only since the release of @code{gcc 4.0.0} in April 2005
  have Linux and other non-commercial OSes had much support for F95.
  Only wih @R{} 2.6.0 did the Windows port adopt a Fortran 90 compiler.

  This means that portable packages need to be written in correct
  FORTRAN 77, which will also be valid Fortran 95.  See
  @uref{http://developer.r-project.org/Portability.html} for reference
  resources.  In particular, @emph{free source form} F95 code is not

  On some systems an alternative F95 compiler is available: from the
  @code{gcc} family this might be @command{gfortran} or @command{g95}.
  Configuring @R{} will try to find a compiler which (from its name)
  appears to be a Fortran 90/95 compiler, and set it in macro @samp{FC}.
  Note that it does not check that such a compiler is fully (or even
  partially) compliant with Fortran 90/95.  Packages making use of
  Fortran 90/95 features should use file extension @file{.f90} or
  @file{.f95} for the source files: the variable @code{PKG_FCFLAGS}
  specifies any special flags to be used.  There is no guarantee that
  compiled Fortran 90/95 code can be mixed with any other type of code,
  nor that a build of @R{} will have support for such packages.

Section 5.5 (Creating shared objects) also mentions Fortran 9x:

  Shared objects for loading into @R{} can be created using @command{R
  CMD SHLIB}.  This accepts as arguments a list of files which must be
  object files (with extension @file{.o}) or sources for C, C++, FORTRAN
  77, Fortran 9x, Objective C or Objective C++ (with extensions
  @file{.c}, @file{.cc} or @file{.cpp} or @file{.C}, @file{.f},
  @file{.f90} or @file{.f95}, @file{.m}, and @file{.mm} or @file{.M},
  respectively), or commands to be passed to the linker.  See @kbd{R CMD
  SHLIB --help} (or the @R{} help for @code{SHLIB}) for usage

Thus, it seems that calling Fortran 90 code from R is possible on
some platforms and, presumably, on those where it is possible this is
done via the .Fortran interface; although the Writing R Extensions
manual does not seem to say so explicitly.

OTOH, the help file for .Fortran states:

     Use '.Fortran' with care for compiled Fortran 9x code: it may not
     work if the Fortran 9x compiler used differs from the Fortran
     compiler used when configuring R, especially if the subroutine
     name is not lower-case or includes an underscore.




=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability        +65 6516 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore     
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba

More information about the R-help mailing list