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