R can't deal with null-strings () in figures, does anybody understand how to handle this? More concrete, I wish to store complex R objects inside a database utilizing an ODBC or JDBC connection. Since complex R objects aren't easily to become planned to dataframes, I want another possible ways to store such objects. An item might be for instance:
library(kernlab) data(iris) model <- ksvm(Species ~ ., data=iris, type="C-bsvc", kernel="rbfdot", kpar="automatic", C=10)
Because >model< can't be saved directly inside a database, I personally use the serialize() function to retrieve a binary representation from the object (to be able to store it inside a BLOB column):
serialModel <- serialize(model, NULL)
Now I must store this via ODBC/JDBC. To do this, I want a string representation from the object to be able to send a question towards the database, e.g. Place INTO. Since it makes sense a vector of type raw vector, I have to convert it:
stringModel <- rawToChar(serialModel)
And there's the issue:
Error in rawToChar(serialModel) : embedded nul in string: 'X\n\0\0\0\002\0\002\v\0......
R can't cope with in strings. Does anybody comes with an idea how you can bypass this restriction? Or perhaps is there most likely a completly different approach to do this goal?
Thanks ahead of time
stringModel <- as.character(serialModel)
for any character representation from the raw bit codes. rawToChar will attempt to transform the raw bit codes, which isn't what you would like within this situation.
The resulting stringModel could be converted afterwards to the initial model by :
newSerialModel <- as.raw(as.hexmode(stringModel)) newModel <- unserialize(newSerialModel) all.equal(model,newModel)  TRUE
Concerning the writing of binary types to databases through RODBC : for today, the vignette of RODBC reads (p.11) :
Binary types can presently simply be read as a result, and they're came back as column of sophistication "ODBC binary" that is a listing of raw vectors.
A totally different approach is always to simply keep creation of
capture.output(dput(model)) together with a descriptive title after which reconstitute it with
assign(). See comments below regarding the requirement for capture.output().
> dput(Mat1) structure(list(Weight = c(7.6, 8.4, 8.6, 8.6, 1.4), Date = c("04/28/11", "04/29/11", "04/29/11", "04/29/11", "05/01/11"), Time = c("09:30 ", "03:11", "05:32", "09:53", "19:52")), .Names = c("Weight", "Date", "Time"), row.names = c(NA, -5L), class = "data.frame") > y <- capture.output(dput(Mat1)) > y <- paste(y, collapse="", sep="") # Needed because capture output breaks into multiple lines > dget(textConnection(y)) Weight Date Time 1 7.6 04/28/11 09:30 2 8.4 04/29/11 03:11 3 8.6 04/29/11 05:32 4 8.6 04/29/11 09:53 5 1.4 05/01/11 19:52 > new.Mat <- dget(textConnection(y))