[R] .Fortran()

John Bjornar Bremnes J.B.Bremnes at dnmi.no
Tue Oct 9 18:01:35 CEST 2001


Prof Brian Ripley wrote:
> 
> On Tue, 9 Oct 2001, John Bjornar Bremnes wrote:
> 
> > Prof Brian Ripley wrote:
> > >
> > > On Mon, 8 Oct 2001, John Bjornar Bremnes wrote:
> > >
> > > > I am trying to call some Fortran subroutines using .Fortran(). These
> > > > subroutines have arguments of type 'character', 'real', 'integer', and
> > > > 'integer*2'. Is it impossible to call  subroutines expecting both
> > > > 'character' and 'real' as input?
> > >
> > > No, but it is impossible to call those expecting 'integer*2'
> >
> > Ok, it seems that I can easily avoid using 'integer*2'
> >
> > > See ?.Foreign for what is supported, and note the comments there about
> > > character types: they work on some compilers and not on others.
> >
> > Is g77 one of those where it does not work? I use R-1.3.0 on linux (PC).
> 
> A properly working version of g77 should work on some platforms (including
> Linux), but not others (including Windows).
> 
> > I have tried the following simple example:
> >
> >       subroutine mysub(filein, m, x, n)
> > c
> >       integer        m, n, i
> >       character*255  filein
> >       real           x(n)
> > c
> >       write(6,*) 'file: ', filein(1:m)
> >       do i=1,n
> >          x(i) = sqrt(1.0*i)
> >       enddo
> >       end
> >
> > % R CMD SHLIB mysub.f 
> > g77   -fPIC  -g -O2 -c mysub.f -o mysub.o
> > gcc -shared  -o mysub.so mysub.o -L/usr/local/lib -lg2c -lm
> > -L/usr/local/lib -L. -L/usr/lib/gcc-lib/i386-linux/2.95.2 -lm
> >
> > > dyn.load("mysub.so")
> > > .Fortran( "mysub", as.character("myfile"), as.integer(6), as.single(rep(0,10)), as.integer(10), DUP=T )
> >  file: myfile
> > Segmentation fault (core dumped)
> 
> That works for me (RH6.2, gcc 3.0.1, R 1.3.1).
>  file: myfile
> [[1]]
> [1] "myfile"
> 
> [[2]]
> [1] 6
> 
> [[3]]
>  [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
>  [9] 3.000000 3.162278
> attr(,"Csingle")
> [1] TRUE
> 
> [[4]]
> [1] 10
> 
> BTW, no one said Fortran I/O would work, and it sometimes is a problem.
> 
> > % R
> > > dyn.load("mysub.so")
> > > .Fortran( "mysub", as.character("myfile"), as.integer(6), as.single(rep(0,10)), as.integer(10), DUP=F )
> > Error: character variables must be duplicated in .C/.Fortran
> 
> > Any comments?
> 
> Do you have one of the many broken Linux versions of g77?
> 

I have now tried versions 2.95.2 and 3.0 with no success. I will ask my
system administrator to install the latest gcc version (3.0.1?). Maybe I
should update R to 1.3.1 also. BTW where is the default compiler options
set for 'R CMD SHLIB'? 

In 
"Writing R Extensions" on page 23 it says "It is not possible to pass
numeric vectors as float * or REAL if DUP=TRUE". It should be DUP=FALSE,
or?


John Bjornar Bremnes
Norwegian Meteorological Institute
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list