Unserialize objects from in-memory ASCII instead of from a file connection
Does this fit your needs?
It follows the general strategy in your Approach 2. The only difference is that it uses as.character()
to convert the serialized object to a character vector before passing it to toJSON()
, and then uses as.raw(as.hexmode())
to convert it back to a raw vector "on the other side". (I've marked the two edited lines with comments reading ## <<- Edited
.)
library(forecast)## SERVER: estimates initial model and writes JSON to socketmodel <- auto.arima(AirPassengers, trace = TRUE)fc <- as.data.frame(forecast(model))serialized <- as.character(serialize(model, NULL)) ## <<- Editedclass(serialized)json_out <- list(data = AirPassengers, model = serialized, fc = fc)json_out <- jsonlite::toJSON(json_out)## CLIENT: keeps estimated model, updates data, writes to socketjson_in <- jsonlite::fromJSON(json_out)json_in$data <- window(AirPassengers, end = 1949 + (1/12 * 14))## SERVER: reads new JSON and applies model to new datadata <- json_in$datamodel_0 <- as.raw(as.hexmode(json_in$model)) ## <<- Editedunserialize(model_0)## Series: AirPassengers ## ARIMA(0,1,1)(0,1,0)[12] ## ## Coefficients:## ma1## -0.3184## s.e. 0.0877## ## sigma^2 estimated as 137.3: log likelihood=-508.32## AIC=1020.64 AICc=1020.73 BIC=1026.39