Having an issue getting multiple predicts set up in the same RShiny application. I have 2 different models, one for each work area I am working with (TRN, STR) that I want to have separate predicts set up for that reference their respective models. Apologies for the messy code, and I only included the server portion of the Rshiny code and not all of the dashboarding info. I am not quite sure how to have separate inputs for the 2 different tabs for the separate models.
Thanks in advance!
# R Shiny server
server <- shinyServer(function(input,TRNinput,output,TRNoutput) {
#Prediction model
#React value when using the action button
a <- reactiveValues(result = NULL)
# a_trn <- reactiveValues(result = NULL)
observeEvent(input$cal, {
#Copy of the test data without the dependent variable
test_pred <-select(STRtestShiny,Workcenter,Grade,Length,Diameter)
#Dataframe for the single prediction
values = data.frame(Workcenter = input$p_workcenter,
Length = input$p_length,
Grade = input$p_grade,
Diameter = input$p_diameter)
#Include the values into the new data
test_pred <- rbind(test_pred,values)
#Single preiction using the randomforest model
a$result <- round(predict(STRModelValidate,
newdata = test_pred[nrow(test_pred),]),
digits = 0)
})
#OBSERVE EVENT TURNER
observeEvent(TRNinput$cal, {
#Copy of the test data without the dependent variable
test_pred_trn <-select(TRNtestShiny,Workcenter,Grade,Length,Diameter)
#test_pred$Workcenter<- str_replace(test_pred$Workcenter, "STRAIGHTENER 1 ", "STRAIGHTENER 1 LOAD")
#test_pred$Workcenter<- str_replace(test_pred$Workcenter, "STRAIGHTENER 2 ", "STRAIGHTENER 2 LOAD")
#test_pred$Workcenter<- str_replace(test_pred$Workcenter, "STRAIGHTENER 3 ", "STRAIGHTENER 3 LOAD")
#Dataframe for the single prediction
values_trn = data.frame(Workcenter = TRNinput$p_workcenter,
Length = TRNinput$p_length,
Grade = TRNinput$p_grade,
Diameter = TRNinput$p_diameter)
#Include the values into the new data
test_pred <- rbind(test_pred_trn,values_trn)
#Single preiction using the randomforest model
a_trn$result <- round(predict(TRNModelValidate,
newdata = test_pred_trn[nrow(test_pred_trn),]),
digits = 0)
})
output$value <- renderText({
#Display the prediction value
paste(a$result)
})
TRNoutput$value <- renderText({
#Display the prediction value
paste(a_trn$result)
})
output$range <- renderText({
#Display the range of prediction value using the MAE value
input$cal
isolate(sprintf('(%s) - (%s)',
round(a$result - STRRSS_Predictions, digits = 0),
round(a$result + STRRSS_Predictions, digits = 0)))
})
TRNoutput$range <- renderText({
#Display the range of prediction value using the MAE value
TRNinput$cal
isolate(sprintf('(%s) - (%s)',
round(a_trn$result - TRNRSS_Predictions, digits = 0),
round(a_trn$result + TRNRSS_Predictions, digits = 0)))
})
})
shinyApp(ui, server)
I attempted this code above and the error I received was related to the new input/output variables not having default values. I set them up no different to the first set so I was unsure how to tackle this problem