2

Currently, I convert data table into rda format by using save() like so

save(data_table,file="data.rda") 

But I don't want to write to disk to get .rda format of the variable. Instead, I would like to have the output(rda bytes) be written straight into a variable.

If it were in other programming languages, It would be something like

Switching from

FileStream fs = new FileStream("data.rda","w") Save(data_table,fs) 

to

byte[] buffer = new byte[data_table.getBytes()] Save(data_table,buffer); 

Is there a way to do something like this in R?

2
  • 1
    What exactly do you mean? Commented Mar 11, 2016 at 20:55
  • I just want a byte array of that rda without having to save to disc and read it back. does it makes any sense? Commented Mar 11, 2016 at 20:57

1 Answer 1

4

This is possible with raw connections

rc <- rawConnection(raw(0),"wb") save(iris,file=rc) v1 <- rawConnectionValue(rc) close(rc) head(v1,80) #> [1] 52 44 58 32 0a 58 0a 00 00 00 02 00 03 01 01 00 02 03 00 00 #> [21] 00 04 02 00 00 00 01 00 04 00 09 00 00 00 04 69 72 69 73 00 #> [41] 00 03 13 00 00 00 05 00 00 00 0e 00 00 00 96 40 14 66 66 66 #> [61] 66 66 66 40 13 99 99 99 99 99 9a 40 12 cc cc cc cc cc cd 40 

Compare to writing to a temporary file and reading back

save(iris,file="temp.rda",compress=FALSE) v2 = readBin("temp.rda", raw(), file.info("temp.rda")[1,"size"]) identical(v1,v2) #> [1] TRUE 

Note that writing to file by default uses compression and writing to a raw connection by default does not, hence the compress=FALSE argument.


See also serialize, which may be more suitable, depending on your purpose

v3 <- serialize(iris,NULL) 

Note that identical(v1,v3)==FALSE. Indeed, v3 is identical instead to using saveRDS in place of save above. The encoding/content is similar

head(v3,80) #> [1] 58 0a 00 00 00 02 00 03 01 01 00 02 03 00 00 00 03 13 00 00 #> [21] 00 05 00 00 00 0e 00 00 00 96 40 14 66 66 66 66 66 66 40 13 #> [41] 99 99 99 99 99 9a 40 12 cc cc cc cc cc cd 40 12 66 66 66 66 #> [61] 66 66 40 14 00 00 00 00 00 00 40 15 99 99 99 99 99 9a 40 12 

And it is easy to recover the data

identical(unserialize(v3),iris) #> [1] TRUE 
Sign up to request clarification or add additional context in comments.

1 Comment

to those who want to have the v1 or v3 compressed. use gz <- memCompress(v1, type = "xz").

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.