是否可以通过eventReactive()

时间:2019-09-30 20:22:59

标签: r shiny shinydashboard shiny-reactivity shinyapps

我正在尝试R仪表盘。我想通过Travis和Shinyapps.io在线发布它。

这是我要在线托管的应用程序:https://loicpalma.shinyapps.io/Test_Shiny/ 可以在这里找到代码:https://github.com/loicpalma/Shiny_App

我的应用程序在本地运行良好,但是当我尝试执行以下特定代码时,日志上出现"out of memory"错误:

plot_all_ROCS <- eventReactive(input$go5, {

    set.seed(1337)

    test_svm_plot = test
    predictions_svm <- predict(svm_classifier(),newdata = test_svm_plot, probability=T)
    svm_predict_obj <- mmdata(as.numeric(predictions_svm),test_svm_plot$Class)
    svm_performance <- evalmod(svm_predict_obj)

    test_xgb_plot = test
    predictions_xgb= predict(xgb_classifier()
                             , newdata = as.matrix(test_xgb_plot[, colnames(test_xgb_plot) != "Class"])
                             , ntreelimit = xgb_classifier()$bestInd)
    xgb_predict_obj <- mmdata(as.numeric(predictions_xgb),test_xgb_plot$Class)
    xgb_performance = evalmod(xgb_predict_obj)

    test_knn = test
    predictions_knn <- predict(knn_classifier(),newdata = test_knn, type="prob")
    predictions_knn <- predictions_knn[,"1"]
    knn_predict_obj <- mmdata(as.numeric(predictions_knn),test_knn$Class)
    knn_performance = evalmod(knn_predict_obj)

    test_glm = test
    predictions_logreg <- predict(logreg_classifier(),newdata = test_glm, type = "response")
    logreg_predict_obj <- mmdata(predictions_logreg,test_glm$Class)
    logreg_performance = evalmod(mdat = logreg_predict_obj) 

    svm_df <- fortify(svm_performance)
    logreg_df <- fortify(logreg_performance)
    knn_df <- fortify(knn_performance)
    xgb_df <- fortify(xgb_performance)


    svm_df$classifier <- "SVM"
    logreg_df$classifier <- "LOGIT"
    knn_df$classifier <- "KNN"
    xgb_df$classifier <- "XGB"

    performance_df <- rbind(svm_df, logreg_df, knn_df, xgb_df)

    roc <- performance_df[performance_df$curvetype == "ROC",]

    ggplot() +
      # green plot
      geom_line(data=roc, aes(x=x, y=y,colour=classifier)) + 
      geom_abline(intercept=0, slope=1, linetype = "dashed") +
      ylab("Sensitivity") +
      xlab("1-Specificity") +
      ggtitle("ROC Comparison") +
      labs(colour = "Type of Model:")
  })

实际上,我知道我收到此"out of memory"错误,因为限制设置为1GB(免费版),并且我在submitButton中向Shinyapps.io提出了过多要求。

但是,我需要再次对所有模型进行预测,因为似乎我以前找不到存储它们的方法。让我解释一下:

以下是knn模型的示例:

 knn_classifier <- eventReactive(input$go4, {

   set.seed(1337)
   classifier_knn <- knn3(Class ~ .
                          , data = train_smote_maison
                          , k = input$k)
  })


  output$knn <- renderPrint({
    print(knn_classifier())

  })


  output$conf_knn <- renderPlot({
    set.seed(1337)
    test_knn = test
    predictions_knn <- predict(knn_classifier(),newdata = test_knn, type="prob")
    predictions_knn_conf <- predictions_knn[,"1"]
    conf_knn = cbind(test_knn,predictions_knn_conf)
    colnames(conf_knn)[colnames(conf_knn)=="predictions_knn_conf"] <- "predicted"
    plot_confusion_matrix(conf_knn, "K-Nearest-Neighbors")


  })

  output$plot_ROC_knn <- renderPlot({
    set.seed(1337)
    test_knn = test
    predictions_knn <- predict(knn_classifier(),newdata = test_knn, type="prob")
    predictions_knn <- predictions_knn[,"1"]
    knn_predict_obj <- mmdata(as.numeric(predictions_knn),test_knn$Class)
    knn_performance <- evalmod(knn_predict_obj)

    plot(knn_performance, "ROC")


  })

  output$plot_PRC_knn <- renderPlot({
    set.seed(1337)
    test_knn = test
    predictions_knn <- predict(knn_classifier(),newdata = test_knn, type="prob")
    predictions_knn <- predictions_knn[,"1"]
    knn_predict_obj <- mmdata(as.numeric(predictions_knn),test_knn$Class)
    knn_performance <- evalmod(knn_predict_obj)

    plot(knn_performance, "PRC")



  })

最后,我想要这样的东西:

knn_classifier <- eventReactive(input$go4, {

  set.seed(1337)
  classifier_knn <- knn3(Class ~ .
                         , data = train_smote_maison
                         , k = input$k)
  test_knn = test
  predictions_knn <- predict(knn_classifier(),newdata = test_knn, type="prob")
  predictions_knn_conf <- predictions_knn[,"1"]
  conf_knn = cbind(test_knn,predictions_knn_conf)
  colnames(conf_knn)[colnames(conf_knn)=="predictions_knn_conf"] <- "predicted"
  knn_performance <- evalmod(knn_predict_obj)
  knn_df <- fortify(knn_performance)

})


output$knn <- renderPrint({
  print(knn_classifier())

})


output$conf_knn <- renderPlot({


  plot_confusion_matrix(conf_knn, "K-Nearest-Neighbors")


})

output$plot_ROC_knn <- renderPlot({

  plot(knn_performance, "ROC")


})

output$plot_PRC_knn <- renderPlot({

  plot(knn_performance, "PRC")



})

为了将变量knn_df保存在全局环境中,以便在我想对ROC进行模型比较时不必再次计算预测。

我的问题如下:是否有一种方法可以将来自eventReactive()的knn_df作为数据帧或.rds存储,这样我在最后不必计算它想一次绘制所有ROC?

0 个答案:

没有答案