[R-sig-DB] Saving R-objects to a database

Herve Pages hp@ge@ @end|ng |rom |hcrc@org
Fri Oct 3 04:17:19 CEST 2008


Hi Christian,

Christian Ruckert wrote:
[...]
> 
> (Un-)encode with rawToChar now worked for a small object, but for bigger 
> objects the string is truncated.

You need to serialize with ascii=TRUE otherwise you get a raw vector with
lots of nul bytes that you won't be able to turn into a text without losing
part of it:
   - rawToChar() will truncate the text at the first nul byte
   - intToUtf8(as.integer()) will silently ignore those nul bytes.
This is because embedded nuls are not supported in character vectors anymore
(which is a good thing).

I'm not 100% sure but I think you won't get any nul bytes if you use
serialize( , ascii=TRUE). Try this:

   objToText <- function(object)
       rawToChar(serialize(object, NULL, ascii=TRUE))

   textToObj <- function(text)
       unserialize(charToRaw(text))

 > x0 <- list(1, 2, 3)
 > objToText(x0)
[1] "A\n2\n133120\n131840\n19\n3\n14\n1\n1\n14\n1\n2\n14\n1\n3\n"
 > textToObj(objToText(x0))
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

I've not tried this on big objects though.

Note that, despite serialize() man page claiming that ascii=FALSE will produce
a more compact binary representation of the object, my experience turns out
to be the opposite.

Cheers,
H.




More information about the R-sig-DB mailing list