[R] .Fortran() again

Martin Maechler maechler at stat.math.ethz.ch
Thu Apr 21 18:59:44 CEST 2005


>>>>> "simon" == simon chou <sentientc at gmail.com>
>>>>>     on Thu, 21 Apr 2005 18:36:17 +0800 writes:

    simon> Hi, First ,please excuse my poor English. 

no problem - we have read much worse ;-)

  <...........>


    simon> Another way I have tried uses .Fortran (). The fortran code were.
    simon> 1.f

    simon> subroutine readmm5(mm5file,IFLAG,MIF)
    simon> integer iflag,MIF(50,20),MRF(20,20)
    simon> CHARACTER*80 mm5file,MIFC(50,20),MRFC(20,20)
    simon> OPEN (11,FILE=mm5file,FORM='UNFORMATTED')
    simon> READ(11) IFLAG
    simon> IF ( IFLAG .EQ. 0 ) THEN
    simon> READ(mm5file) MIF,RMRF,MIFC,MRFC
    simon> endif
    simon> end

    simon> $R CMD SHLIB 1.f
    simon> $R
    >> library(foreign)
    >> dyn.load("1.so")
    >> .Fortran("readmm5",as.character("mmout"),as.integer(1),as.matrix(nrow=50,ncol=20,data=as.integer(rep(0,50))))

    simon> This works alright until starts to read the 50 by 20 integer array.
    simon> The bit about using as.matrix() to read the array do seem suspicious.
{indeed!}

    simon> I do not know what is the proper way to read it.

Instead of your as.matrix(..) you could use matrix(),
also you should *assign* the result of .Fortran()
and you should use your space keybar to make things more
readable.

This leads to
    r <- .Fortran("readmm5",
                  file =  as.character("mmout"),
		  iflag = as.integer(1),
		  mif = matrix(as.integer(0), nrow=50,ncol=20))

where r is list where  r[["mif"]] should be your desired matrix.
Alternatively, in your case,
    mif <- .Fortran("readmm5",
	            file =  as.character("mmout"),
		    iflag = as.integer(1),
		    mif = matrix(as.integer(0), nrow=50,ncol=20))[["mif"]]

Hoping this helps further,
Martin




More information about the R-help mailing list