[R] reading data saved with writeBin() into anything other than R

Mike Miller mbmiller+l at gmail.com
Tue Apr 22 05:28:59 CEST 2014

On Tue, 22 Apr 2014, William Dunlap wrote:

>> For me that other software would probably be Octave.  I'm interested if 
>> anyone here has read in these files using Octave, or a C program or 
>> anything else.
> I typed 'octave read binary file' into google.com and the first hit was 
> the Octave help file for its fread function.  In C fread is also a good 
> way to go (C and Octave have different argument lists for their fread 
> functions.) In the Linux shell you can use the od command.

Thanks!  My mistake was that I was searching using "R" and "writebin" in 
my search string which limited my results too severely.  I actually 
figured that out before your message came in and felt a little 
embarrassed, and that has only gotten worse.  But you did give me 
something cool that I didn't know:

> % R --quiet
>> con <- gzcon(file("/tmp/file.gz", "wb")) # your gzcon("/tmp/file.gz", "wb") resulted in an error message
>> writeBin(c(121:130,129:121), con, size=2)
>> close(con)
>> q("no")
> % zcat /tmp/file.gz | od --format d2
> 0000000    121    122    123    124    125    126    127    128
> 0000020    129    130    129    128    127    126    125    124
> 0000040    123    122    121
> 0000046

That's really neat.  With my data, I can do this to return the original 

zcat file.gz | od -vtd2 -w15392 -An > matrix.txt

It is quite fast, too:

$ time -p zcat D1.gz | od -vtd2 -w15392 -An >/dev/null
real 6.08
user 6.86
sys 0.08

If I had realized how little my writeBin() output files had to do with R, 
I probably wouldn't have posted here, but I'm glad I did.

FYI -- I was able to use fread() in Octave on the uncompressed version of 
the file, but it isn't handling the zipped version as expected.  That's an 
Octave problem, so I'll deal with them on that one.  I might not have zlib 
compiled in, or maybe they still have a bug in that function.



More information about the R-help mailing list