Someone has used serialize()
to stick an R object into SQL Server, in a VARBINARY column.
How can I unserialize()
it out?
Here's the code that gets the data:
library(RODBC)
dbhandle <- odbcDriverConnect("MyConnection String")
x = sqlQuery(dbhandle, sprintf('select SomevarbinaryColumn from MyTAble WHERE the_key = 8675309'))
If I try this:
unserialize(x$SomevarbinaryColumn[1])
I get
Error in unserialize(x$SomevarbinaryColumn[1]) :
'connection' must be a connection
Confusingly, the first parameter to unserialize()
is named connection
. The documentation says "unserialize reads an object (as written by serialize) from connection or a raw vector.". What kind of connection might be provided here? Instead, though, I think I'm trying to pass a raw vector, but I can't figure out how to convert the RODBC result to a raw vector.
I've tried to figure out what type x$SomevarbinaryColumn[1]
is so I can convert it to something that unserialize()
likes:
print(typeof(x$SomevarbinaryColumn[1]))
gives
[1] "list"
the RODBC docs say that varbinary will come back as "a list of raw vectors."
How do I get the raw vector that unserialize()
wants from this returned list?