I have an R script which creates a model, serialize it and store it in a models collection inside a test mongo database:
library(mongolite)
mongo_host="localhost"
mongo_port=27017
url_path = sprintf("mongodb://%s:%s", mongo_host, mongo_port)
mongo_database="test"
mongo_collection <- "models"
mongo_con<-mongo(collection = mongo_collection
,url = paste0(url_path,"/",mongo_database))
mySerializationFunc<-function(value){
return (base64enc::base64encode(serialize(value, NULL,refhook = function(x) "dummy value")))
}
myUnserializationFunc<-function(value){
return (unserialize(value,refhook = function(chr) list(dummy = 0L)))
}
insertDocumentIntoCollection <- function(connection,object) {
str<-paste0('{"modelName": "',object$modelName,'", "objectModel" :',paste0('{"$binary":{"base64":"',mySerializationFunc(object$objectModel),'","subType": "0"}}}'))
connection$insert(str)
}
getDocumentFromCollection<-function(connection,modelName){
strConditions=paste0('{"modelName":"',modelName,'"}')
strSelect=paste0('{"objectModel":true,"_id":false}')
return(connection$find(query=strConditions,fields=strSelect))
}
modelName<-"irisTestAll"
lst<-list()
lst$modelName<-modelName
lst$objectModel<-randomForest::randomForest(as.formula("Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width"),iris)
# Store the model in mongoDB
insertDocumentIntoCollection(mongo_con,lst)
Then, I can retrieve the model, unserialize it and perform predictions:
# Retrieve the model
mdl<-getDocumentFromCollection(mongo_con,modelName)
# By using "mdl[[1]][[1]]" we get allways the first model
mdl<-myUnserializationFunc(mdl[[1]][[1]])
predict(mdl,iris)
Now, I have created the shiny version of the creation of the model (exactly the same code):
library(shiny)
library(mongolite)
mongo_host="localhost"
mongo_port=27017
url_path = sprintf("mongodb://%s:%s", mongo_host, mongo_port)
mongo_database="test"
mongo_collection <- "models"
mongo_con<-mongo(collection = mongo_collection
,url = paste0(url_path,"/",mongo_database))
mySerializationFunc<-function(value){
return (base64enc::base64encode(serialize(value, NULL,refhook = function(x) "dummy value")))
}
insertDocumentIntoCollection <- function(connection,object) {
str<-paste0('{"modelName": "',object$modelName,'", "objectModel" :',paste0('{"$binary":{"base64":"',mySerializationFunc(object$objectModel),'","subType": "0"}}}'))
connection$insert(str)
}
ui <- fluidPage(
actionButton("aa","Generate model")
)
server <- function(input, output, session){
observeEvent(input$aa,{
lst<-list()
lst$modelName<-"irisTestAll"
lst$objectModel<-randomForest::randomForest(as.formula("Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width"),iris)
# Store the model in mongoDB
insertDocumentIntoCollection(mongo_con,lst)
})
}
shinyApp(ui, server)
The app seems to work fine, however when I retrieve the model (model stored using the app) from mongo to perform predictions:
modelName<-"irisTestAll"
# Retrieve the model
mdl<-getDocumentFromCollection(mongo_con,modelName)
# By using "mdl[[1]][[1]]" we get allways the first model
mdl<-myUnserializationFunc(mdl[[1]][[1]])
predict(mdl,iris)
I get this error:
Error in eval(predvars, data, env) :
invalid 'enclos' argument of type 'list'
So, it seems that storing from R console works fine but fails when it is done using shiny.
Any idea how to solve it?
Thanks.
The issue is neither with
shinynor withmongodb, it's with serialization / deserialization.Upon de-serializing the
randomForestmodel object, the environment contains adummyvalue:which leads to prediction error:
Let's replace the environment properly (modify the
myUnserializationFunc()?) and the prediction will work fine: