繁体   English   中英

R Shiny 应用程序错误 eval 中的错误:找不到对象“年龄”

[英]R Shiny app error Error in eval: object 'age' not found

我正在尝试使用 Shiny 创建一个应用程序,它使用分类模型 (svm) 来预测输出。 但我不断收到此错误: eval 中的错误:找不到对象“年龄”

用户界面和服务器代码:

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)

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

当我将Output <- data.frame(Prediction=predict(model,test), round(predict(model,test,type="prob"), 3))替换为Output <- data.frame(test) ,它起作用了美好的。 因此,错误发生在predict(model,test)

没有提供model.rds对象,这就是我所能看到的。

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

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"),

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM