简体   繁体   English

r shiny: How do I save a model object in one reactive function to be used in another?

[英]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.

相关问题 如何获得对象的反应值并将其传递给闪亮的R中的另一个函数 - how do I get reactive value of an object and pass it to another function in shiny R 如何在 r Shiny 中使用反应式代码创建可重复的函数? - How do I create a repeatable function with reactive code in r shiny? 如何在 R shiny 中保存以前的无功值? - How can I save previous reactive values in R shiny? 在 R shiny 中,如何为反应式 object 渲染 plot? - In R shiny, how do you render a plot for a reactive object? R:In Shiny如何修复应用于“反应性”类对象的“xtable”的适用方法 - R: In Shiny how do I fix no applicable method for 'xtable' applied to an object of class “reactive” 是否可以将反应性函数用于r Shining中的另一个反应性函数? - Is it possible to use reactive function into another reactive function in r shiny? 在适用于 R 的 Shiny 应用程序中,如何延迟响应式的触发? - In Shiny apps for R, how do I delay the firing of a reactive? 如何在R / Shiny中构建反应式数据框? - How do I build a reactive dataframe in R / Shiny? 在 R Shiny 我如何检查反应的值并打印消息 - In R Shiny how do I check the value of a reactive and print a message 如何在R的Shiny应用程序中计时反应功能 - How to time reactive function in Shiny app in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM