I have an R
script which creates a model, serialize it and store it in a models
collection inside a test
mongo database:
我有一个创建模型、序列化模型并将其存储在测试Mongo数据库内的Models集合中的R脚本:
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:
这个应用程序似乎运行得很好,但是当我从Mongo检索模型(使用应用程序存储的模型)来执行预测时:
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.
因此,从R控制台进行存储似乎工作得很好,但在使用SHINY完成存储时会失败。你知道怎么解决这个问题吗?谢谢。
更多回答
The issue is neither with shiny
nor with mongodb
, it's with serialization / deserialization.
问题既不是Slight的问题,也不是MongoDB的问题,而是序列化/反序列化的问题。
Upon de-serializing the randomForest
model object, the environment contains a dummy
value:
在对随机森林模型对象进行反序列化时,环境包含一个伪值:
mdl <- getDocumentFromCollection(mongo_con,modelName)
mdl <- myUnserializationFunc(mdl[[1]][[1]])
attr(mdl$terms, ".Environment")
# $dummy
# [1] 0
which leads to prediction error:
这会导致预测误差:
predict(mdl, newdata=iris)
# Error in eval(predvars, data, env) :
# invalid 'enclos' argument of type 'list'
Let's replace the environment properly (modify the myUnserializationFunc()
?) and the prediction will work fine:
让我们正确地替换环境(修改myUnSerializationFunc()?)这一预测将会很好地发挥作用:
attr(mdl$terms, ".Environment") <- .GlobalEnv
attr(mdl$terms, ".Environment") # check
# <environment: R_GlobalEnv>
# now predict
predict(mdl, newdata=iris)
# 1 2 3 4 5 6 7 8 ...
# 5.102515 4.766670 4.666158 4.804332 5.055100 5.382859 4.891974 5.051596
# ...
更多回答
我是一名优秀的程序员,十分优秀!