Saturday 4 July 2015

R : Serialize object

Serialization is the process of converting object state into sequence of bytes. de-Serialization is a process to bring back the object’s state from bytes. You can serialize object using serialize function.

Syntax
serialize(object, connection, ascii, xdr = TRUE,
          version = NULL, refhook = NULL)


Parameter
Description
object
R object to serialize
connection
Represents open connection
ascii
If ascii is TRUE or NA, then ascii representation writeen, else binary one
xdr
A logical value: if a binary representation is used, should a big-endian one (XDR) be used?
version
The workspace format version to use. NULL specifies the current default version (2). Versions prior to 2 are not supported, so this will only be relevant when there are later versions.
refhook
A hook function to handle reference objects.


> data <- 1:50
> data
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> 
> x <- serialize(data, NULL)
> x
  [1] 58 0a 00 00 00 02 00 03 02 00 00 02 03 00 00 00 00 0d 00 00 00 32 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00 09 00 00 00 0a 00 00 00 0b 00 00 00
 [70] 0c 00 00 00 0d 00 00 00 0e 00 00 00 0f 00 00 00 10 00 00 00 11 00 00 00 12 00 00 00 13 00 00 00 14 00 00 00 15 00 00 00 16 00 00 00 17 00 00 00 18 00 00 00 19 00 00 00 1a 00 00 00 1b 00 00 00 1c 00 00 00 1d
[139] 00 00 00 1e 00 00 00 1f 00 00 00 20 00 00 00 21 00 00 00 22 00 00 00 23 00 00 00 24 00 00 00 25 00 00 00 26 00 00 00 27 00 00 00 28 00 00 00 29 00 00 00 2a 00 00 00 2b 00 00 00 2c 00 00 00 2d 00 00 00 2e 00
[208] 00 00 2f 00 00 00 30 00 00 00 31 00 00 00 32
> 
> 
> x <- unserialize(x)
> x
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment