ako
ako

Reputation: 3689

R: readBin and writeBin on linux

I want to write (and later retrieve) data in binary format. I am trying to get a minimal example to work at least to the level of the smell test (read input should look like written output), but I haven't gotten it just, and consistently right. My machine is a linux with little endian, but since that is constant here, I ommitted it from the calls. I was also not sure if it was better to specify the size argument in the write, or leave it out. At any rate, the loaded input doesn't look like out:

out<-seq(1,50,2)

##write
write<-file('~/output.txt','wb')
writeBin(out,con=write,size=4)
close(write)

##read
read<-file('~/output.txt','rb')
readBin(con=read,what=numeric(),n=length(out))
# [1] 3.200001e+01 3.276801e+04 1.048576e+06 1.677722e+07 1.006633e+08 4.026532e+08     1.610613e+09 6.442452e+09 1.503239e+10 3.006478e+10 6.012955e+10 1.202591e+11
close(read)

Upvotes: 0

Views: 1255

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368241

Here is a worked example:

R> dat <- seq(1,50,2)
R> dat
 [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
R> 

We just write the object to a binary connection -- omitting your third parameter:

R> con <- file("/tmp/foo.bin", "wb")
R> writeBin(dat, con)
R> close(con)
R> 

And can then read back:

R> con <- file("/tmp/foo.bin", "rb")
R> dat2 <- readBin(con, what="numeric", n=length(dat))
R> dat2
 [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
R>
R> all.equal(dat, dat2)
[1] TRUE

However, you need to store the length. I have used an internal file format which first writes a fixed (known) number of integers about rows, cols, "format number", ... and then read / writes a total of rows * cols numerics. We also wraps this into a gzip-ed file connection.

You could even generalize this to writing columns as in a data frame -- but if R is your only reader / writer then the already exisiting serialization via save() is better.

Upvotes: 7

Related Questions