Skip to content

Fortran ACCESS='DIRECT' for importing Binary Files

2 messages · Jared Stabach, Dirk Eddelbuettel

#
Is there a command similar to Fortran's Access='DIRECT' command for reading
binary files in R?  I am interested in writing a program to access specific
parts of a binary file, where many years of results are bound together in
one file.

An option would be to call this Fortran function from within R.

Thanks for any thoughts.

Jared
#
On 16 April 2014 at 14:58, Jared Stabach wrote:
| Is there a command similar to Fortran's Access='DIRECT' command for reading
| binary files in R?  I am interested in writing a program to access specific
| parts of a binary file, where many years of results are bound together in
| one file.

I have no idea what 

  Fortran's Access='DIRECT'

is or does, and suspect this question belongs onto r-devel instead .

That said, I may have devised such a simple binary file format for our use at
work where we frequently need to read large matrices (and typically less
frequently write them in batch jobs).

So I write out a header with a few fixed ints about dimension and type, and
then just have one large read of rows * cols elements of size double.  And
the nice thing about R is that you can do all of this in R code (!!) via

    gzfile()            to open a compressed file
    readBin()           to read int or double

so no need to go to C or Fortran.  See the docs on help(connection) and
help(readBin).   (And yes, the format is accessible from C/C++ or other
languages such as Python too provided they can read binary blobs from gzip'ed
file)

Dirk