[R] R and Fortran in Windows
Dan Stram
stram at usc.edu
Thu Dec 2 21:56:52 CET 2004
I just joined the list and appologize if this has been answered before
but I am trying to interface between R and the Compaq Visual Fortran
compiler version 6.6 for Windows.
I found the following instructions on the web -- and an example. When I
follow these directions exactly. R 2.0.0 crashes. Has anyone had any
experience with this?
Below are the instructions that I located:
Thanks
Dan Stram
Professor
Department of Preventive Medicine
Division of Genetic Epidemiology and Biostatistics
University of Southern California
1540 Alcazar Street, Suite 220
Los Angeles, CA 90033
tel 323-442-1817
fax 323-442-2349
email stram at usc.edu
web http://www-rcf.usc.edu/~stram
These notes were written by J.R.M. Hosking.
Using Fortran routines from R with CVF
1. Take a working Fortran subroutine, and put its name in an
ATTRIBUTES DLLEXPORT directive to ensure that the routine name
is exported from the DLL that will be built. Example:
SUBROUTINE MYSUB(X,N,XMEAN)
CDEC$ ATTRIBUTES DLLEXPORT :: MYSUB
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
DOUBLE PRECISION X(N)
XMEAN=0D0
DO 10 J=1,N
XMEAN=XMEAN+X(J)
10 CONTINUE
XMEAN=XMEAN/N
RETURN
END
2. Compile and link the routine using the CVF options /assume:underscore
and /dll. E.g., supposing that the routine is in a file rtest.f,
at the command prompt type
df rtest.f /assume:underscore /dll
This will create a file rtest.dll file that should be kept, and
.obj, .lib and .exp files that are not needed for R and can be deleted.
(/assume:underscore is needed because R expects that a routine name
exported from a DLL will have an underscore appended to it, but CVF
does not do this by default.)
3. Now from an R command prompt, use dyn.load to load the DLL and
.Fortan("MYSUB",...) to call the function. Example:
> dyn.load("E:\\rtest.dll")
> is.loaded(symbol.For("MYSUB"))
[1] TRUE
> x<-1:6
> .Fortran("MYSUB",as.double(x),as.integer(length(x)),xmean=double(1))
[[1]]
[1] 1 2 3 4 5 6
[[2]]
[1] 6
$xmean
[1] 3.5
>
More information about the R-help
mailing list