如何在R Shiny应用程序中获得输出结果以对数据进行逻辑回归

时间:2019-02-18 12:53:34

标签: r shiny

我正在尝试创建一个R闪亮的应用程序,该应用程序对输入数据执行逻辑回归,并将结果与​​结果列并附加到输入数据中,输入数据为四列,而我的第五列为预测结果。 / p>

谢谢!

我是一个初学者,尝试学习R和R闪亮的应用程序,我已经修改并尝试了以下代码,这些代码是我从Fisseha Berhane https://datascience-enthusiast.com/R/shiny_ML.html复制来的

结果列将预测健身房的客户选择1个月还是3个月套餐。

library(caret)
library(shiny)
library(LiblineaR)
library(readr)
library(ggplot2)
load("C:/Users/Sampath/Desktop/PROJECT/LogisticRegression.rda")

shinyServer(function(input, output) {
    options(shiny.maxRequestSize = 800*1024^2)   
    output$sample_input_data_heading = renderUI({   
    inFile <- input$file1

    if (is.null(inFile)){
        return(NULL)
    }else{
        tags$h4('Sample data')
    }
  })

  output$sample_input_data = renderTable({    
      inFile <- input$file1

      if (is.null(inFile)){
          return(NULL)
      }else{
      input_data =readr::read_csv(input$file1$datapath, col_names = TRUE)

      colnames(input_data) = c("age", "Gender","height","weight","Label")

      input_data$Label = as.factor(input_data$Label )

      levels(input_data$Label) <- c("1","3")
      head(input_data)
    }
  })

  predictions<-reactive({

    inFile <- input$file1

    if (is.null(inFile)){
      return(NULL)
    }else{
      withProgress(message = 'Predictions in progress. Please wait ...', {
        input_data =  readr::read_csv(input$file1$datapath, col_names = TRUE)

        colnames(input_data) = c("age", "Gender","height","weight","Label")

        input_data$Label = as.factor(input_data$Label )
        input_data$age=as.integer(input_data$age)
        levels(input_data$Label) <- c("1","3")

        #mapped = feature_mapping(input_data)

        #df_final = cbind(input_data, mapped)
        prediction = predict(classifier,input_data[c(1:4)])

        input_data_with_prediction = cbind(input_data,prediction )
        input_data_with_prediction

      })
    }
  })


 output$sample_prediction_heading = renderUI({  
    inFile <- input$file1

    if (is.null(inFile)){
      return(NULL)
    }else{
      tags$h4('Sample predictions')
    }
  })

  output$sample_predictions = renderTable({ 
    predictions


  })

  })

  # Downloadable csv of predictions ----

  output$downloadData <- downloadHandler(
    filename = function() {
      paste("input_data_with_prediction", ".csv", sep = "")
    },
    content = function(file) {
      write.csv(predictions(), file, row.names = FALSE)
    })

})

预期输出是第5列,显示预期结果 但是我一直在出错

错误1:-找不到对象“ X”

error2:-无法将类“ c(“ reactiveExpr”,“ reactive”)“强制转换为data.frame

0 个答案:

没有答案