[英]r shiny: How do I save a model object in one reactive function to be used in another?
I have made an machine learning application that will train a randomforest model on some uploaded data, and give a prediction of a specified single obsevation based on some selected inputs widgets
, once an actionbutton()
is pressed.我已经制作了一个机器学习应用程序,它将在一些上传的数据上训练一个随机森林 model,并在按下
actionbutton()
后根据一些选定的inputs widgets
对指定的单个观察值进行预测。
Both the function for training of the model " randomForest()
" and function for prediction " predict()
" is run, everytime the actionbutton()
is pressed.每次按下操作按钮时,都会运行用于训练 model“
randomForest()
”的 function 和用于预测“ predict()
”的actionbutton()
。
I have come to realize that this is not very effecient use of the computational power, since the randomForest()
function usually does not need to be run more than once, and can take a while to run depending on the size of the uploaded dataset and model specifications.我开始意识到这不是非常有效地使用计算能力,因为
randomForest()
function 通常不需要运行多次,并且可能需要一段时间才能运行,具体取决于上传数据集的大小和model 规格。
To fix this issue, I have create another actionbutton()
which sole purpose is to train the model, so it dont have to be retrained everytime I want to make a prediction.为了解决这个问题,我创建了另一个
actionbutton()
,其唯一目的是训练 model,所以每次我想进行预测时都不必重新训练。 Once a model is trained, it is saved as a 'model object'.训练 model 后,将其保存为“模型对象”。
The predict()
function requires a 'model object' as an input, which is where the problem arise. predict()
function 需要一个“模型对象”作为输入,这就是问题所在。 I can't seem to figure out how to refer to the 'model object' in such a way that the prediction function can accept it as an input.我似乎无法弄清楚如何以预测 function 可以接受它作为输入的方式引用“模型对象”。
This is how I create the model object by the press of an actionbutton
:这就是我通过按下操作按钮创建 model object 的
actionbutton
:
model <- observeEvent(input$trainmodel,{
df <- selected_df()
model <- randomForest(eval(parse(text=paste(names(df)[1],"~ ."))), data = df, ntree = 500, mtry = 5, importance = TRUE)
})
And this is roughly how I refer to the 'model object' in the reactive()
code chunk of the predict()
function.这大致就是我在
predict()
function 的reactive()
代码块中引用“模型对象”的方式。
model <- model()
Prediction <- predict(model,test)
However, this doesn't seem to work as I get the error message:但是,这似乎不起作用,因为我收到错误消息:
Warning: Error in model: could not find function "model"
警告:model 中的错误:找不到 function“型号”
Keep in mind that all of this works if I define the 'model object' inside the reactive()
bracket, but this would ofcourse cause the very problem I was trying to fix in the first place.请记住,如果我在
reactive()
括号内定义“模型对象”,所有这些都可以工作,但这当然会导致我最初试图解决的问题。
So my question is:所以我的问题是:
How do I specify the model object, so it can be used in other functions such as predict()
?如何指定 model object,以便它可以用于其他函数,例如
predict()
?
------------------------------EDIT----------------------------------- - - - - - - - - - - - - - - - 编辑 - - - - - - - - - - ----------------
Is it possible that the problem is caused by the fact that both the dataframe and the model object is defined in the model()
functionen?问题是否可能是由于 dataframe 和 model object 都在
model()
函数中定义的事实引起的? -If so, how do I fix that? - 如果是这样,我该如何解决? I need the dataframe defined to train the model.
我需要定义 dataframe 来训练 model。
Try eventReactive
instead and make sure you return the object at the end改用
eventReactive
并确保最后返回 object
model <- eventReactive(input$trainmodel,{
req(selected_df())
df <- selected_df()
randomForest(eval(parse(text=paste(names(df)[1],"~ ."))), data = df, ntree = 500, mtry = 5, importance = TRUE)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.