Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert raw vector to R object

How do you convert a raw vector back to an R object without writing to disk? I want to read a stream of base64 data and convert it to its R object representation. Here is an example - how would I get back the lm object from the raw vector?

## some rdata -- writes to temp file!
mod <- lm(mpg ~ cyl, data=mtcars)
f1 <- tempfile()
save(mod, file=f1, compress="bzip2")

library(base64enc)
r1 <- readBin(f1, "raw", n=file.info(f1)[1, "size"])
r2 <- base64decode(base64encode(file(f1, "rb")))  # emulate input base64
identical(r1, r2)

## I can get mod back by writing to file and loading, but how to just
## load from a raw vector?
rm(mod)  # get rid of mod
f2 <- tempfile()
writeBin(r2, f2)
load(f2)  # mod is back
like image 659
Rorschach Avatar asked Mar 20 '16 21:03

Rorschach


People also ask

What is a raw vector in R?

A raw vector is used to represent a "raw" sequence of bytes. Each byte is a value between 0 and 255. There are no NA values. Raw vectors are printed using hexadecimal (base 16) notation, so the (decial) value code10 is printed as 0a.

What does raw do in R?

raw creates a raw vector of the specified length. Each element of the vector is equal to 0 . Raw vectors are used to store fixed-length sequences of bytes.

What is a raw variable in R?

The raw data type holds raw bytes, so it is a very unusual data type. For instance, you could transform a character object or a integer numeric value to a raw object with the charToRaw and intToBits functions, respectively.

How do I convert to numeric vector in R?

To convert a character vector to a numeric vector, use as. numeric(). It is important to do this before using the vector in any statistical functions, since the default behavior in R is to convert character vectors to factors.


2 Answers

Inside my RcppRedis package I use the RApiSerialize package (which is based on base R code initially borrowed in the Rhpc package) to do these conversions on the fly:

R> mod <- lm(mpg ~ cyl, data=mtcars)     # your example
R>
R> library(RApiSerialize)
R> modraw <- serializeToRaw(mod)         # serialized
R> str(modraw)                           # really just a raw vector now
 raw [1:6819] 58 0a 00 00 ...
R> 

So at this point you can do whatever you want with the raw vector. Write it to disk, write it to a database (as we do with RcppRedis), ....

But importantly, you also get your model back:

R> summary( unserializeFromRaw(modraw) )

Call:
lm(formula = mpg ~ cyl, data = mtcars)

Residuals:
   Min     1Q Median     3Q    Max 
-4.981 -2.119  0.222  1.072  7.519 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   37.885      2.074   18.27  < 2e-16 ***
cyl           -2.876      0.322   -8.92  6.1e-10 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.21 on 30 degrees of freedom
Multiple R-squared:  0.726, Adjusted R-squared:  0.717 
F-statistic: 79.6 on 1 and 30 DF,  p-value: 6.11e-10

R> 
like image 81
Dirk Eddelbuettel Avatar answered Oct 27 '22 22:10

Dirk Eddelbuettel


For R-level access use unserialize(serialize(mod, NULL)) to round-trip from an R object to a raw vector and back.

like image 35
Martin Morgan Avatar answered Oct 27 '22 20:10

Martin Morgan