[R] How to read a direct access file by connecting fortran with R ?

Kaptue Tchuente, Armel armel.kaptue at sdstate.edu
Fri Apr 19 13:44:23 CEST 2013

Dear Suzen,

Thank you for your reply.

I don't know why but it looks like my previous mail was truncated.
Here is the R function (described in the step #4 in my previous mail) that I wrote. It contains .Fortran but like I said earlier, it didn't work.
Read_binary<-function(n_samples,line,type) {


-----Original Message-----
From: mehmet.suzen at gmail.com [mailto:mehmet.suzen at gmail.com] On Behalf Of Suzen, Mehmet
Sent: Friday, April 19, 2013 3:17 AM
To: Kaptue Tchuente, Armel
Subject: Re: [R] How to read a direct access file by connecting fortran with R ?

On 19 April 2013 07:05, Kaptue Tchuente, Armel <armel.kaptue at sdstate.edu> wrote:
> Hello all,
> I would like to read the specific line number row of a direct access file (which is stored as a n_row*n_col matrix of elements kind=p) without reading all the preceding lines (i.e 1,2,..,row-1).
> Is there a function in R that can perform this task?
> To solve my issue, I tried without to call Fortran from R by doing the following steps:
> I) I wrote a subroutine in fortran called read_binary.f90
> ************************************
> Subroutine read_binary (n_col,row,img)
>    integer*1    :: im1(n_col)
>    integer*1    :: im2(n_col)
>    integer*1    :: im2(n_col)
>    open(10,file=binary_file,access=direct,action=read,status='old',recl=n_col*p)
>    if(p==1) then
>      read(10,rec=row) img1
>       img=img1
>    else if(p==2) then
>       read(10,rec=row) img2
>       img=img2
>    else
>      read(10,rec=row) img3
>       img=img3
>    endif
>    close(10)
> End subroutine read_binary
> ************************************
> II) R CMD SHLIB read_binary.f90
> III) dyn.load("read_libray.so")
> IV) Then in R,  I tried many variant of the following function but I 
> wasn't able to store the line number row into the vector img

At this stage you need to use .Fortran;

  .Fortran("read_binary", as.integer(n_samples),  as.intger(line), img=as.single(img));

See ?.Fortran

More information about the R-help mailing list