R闪亮的应用错误评估错误:找不到对象“年龄”

时间:2020-10-01 16:11:56

标签: r shiny shinyapps

我正在尝试使用Shiny创建应用程序,该应用程序使用分类模型(svm)来预测输出。但是我一直收到以下错误:评估错误:未找到对象“年龄”

UI和服务器代码:

library(shiny)
library(data.table)
library(caret)
library(caretEnsemble)
library(shinythemes)

model <- readRDS("model.rds")
    

ui <- pageWithSidebar(
  headerPanel('Heart disease predictor'),
  sidebarPanel(
    tags$label(h3('Input parameters')),
    numericInput("age", label = "Age", value = 40),
    numericInput("sex", label = "Sex", value = 1),
    numericInput("chest.pain", label = "Chest pain", value = 1),
    numericInput("resting.BP", label = "resting Bp", value = 120),
    numericInput('cholesterol', label = 'Cholesterol', value = 170),
    numericInput('fasting.sugar', label = 'Fasting Sugar', value = 1),
    numericInput('ECG.at.rest', label = 'ECG at rest', value=1),
    numericInput('max.hear.rate', label = 'Max heart rate', value = 120),
    numericInput('exercisal.angina', label = 'Excercise induced angina', value = 0),
    numericInput('ST.segments', label = 'ST segments', value = 1.5),
    numericInput('slope.of.ST', label = 'slope of ST segment', value = 1),
    numericInput('number.of.vessels', label = 'number of coronary artery', value = 3),
    numericInput('thalassemia', label = 'thalassemia', value = 2),
    
    actionButton("submitbutton", "Submit", class = "btn btn-primary")
  ),
  mainPanel(
    tags$label(h3('Status/Output')), 
    verbatimTextOutput('contents'),
    tableOutput('tabledata')
    )
)


server<- function(input, output) {
  datasetInput <- reactive({  
    df <- data.frame(
      Name = c("Age",
               "Sex",
               "Chest pain",
               "resting Bp",
               "Cholesterol",
               "Fasting Sugar",
               "ECG at rest",
               "Max heart rate",
               "Excercise induced angina",
               "ST segments",
               "slope of ST segment",
               "number of coronary artery",
               "thalassemia"),
      Value = as.character(c(input$age,
                             input$sex,
                             input$chest.pain,
                             input$resting.BP,
                             input$cholesterol,
                             input$fasting.sugar,
                             input$ECG.at.rest,
                             input$max.hear.rate,
                             input$exercisal.angina,
                             input$ST.segments,
                             input$slope.of.ST,
                             input$number.of.vessels,
                             input$thalassemia)),
      stringsAsFactors = FALSE)
    
    target<- 0
    df <- rbind(df, target)
    input <- transpose(df)
    write.table(input,"input.csv", sep=",", quote = FALSE, row.names = FALSE, col.names = FALSE)
    
    test <- read.csv(paste("input", ".csv", sep=""), header = TRUE)
    
    Output <- data.frame(Prediction=predict(model,test), round(predict(model,test,type="prob"), 3))
    print(Output)
    
  })
  
  # Status/Output Text Box
  output$contents <- renderPrint({
    if (input$submitbutton>0) { 
      isolate("Calculation complete.") 
    } else {
      return("Server is ready for calculation.")
    }
  })
  
  # Prediction results table
  output$tabledata <- renderTable({
    if (input$submitbutton>0) { 
      isolate(datasetInput()) 
    } 
  })
  
}

shinyApp(ui = ui, server = server)

我是这个领域的新手,这是我正在创建的第一个应用程序。试图通过闪亮的教程,但仍然无法解决此错误。 我做错了什么?

2 个答案:

答案 0 :(得分:0)

当我将Output <- data.frame(Prediction=predict(model,test), round(predict(model,test,type="prob"), 3))替换为Output <- data.frame(test)时,效果很好。因此,错误发生在predict(model,test)

我没有看到提供的model.rds对象。

答案 1 :(得分:0)

经过一些更改后,我解决了该错误:

server<- function(input, output) {
  datasetInput <- reactive({  
    df <- data.frame(
      Name = c("Age",
               "Sex",
               "Chest pain",
               "resting Bp",
               "Cholesterol",
               "Fasting Sugar",
               "ECG at rest",
               "Max heart rate",
               "Excercise induced angina",
               "ST segments",
               "slope of ST segment",
               "number of coronary artery",
               "thalassemia"),

在这里,名称应与受过训练的数据列完全匹配,我做到了它已成功执行。