当我在预测函数中使用反应对象时,R Shiny显示错误

时间:2019-02-28 13:27:25

标签: r shiny

我找不到错误的地方。我认为预测函数无法正常工作,并且它以适当的值显示new_depression,然后在答案对象中显示出new_depression的特征是从拟合中指定的。它怎么不起作用,可能是预测功能不能正常工作。请帮忙!!!

图书馆(闪亮)

    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel( 
      radioButtons(inputId="first", label="a",  choices = list("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4),selected = 1),
                    radioButtons(inputId="second", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                    radioButtons(inputId="third", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                    radioButtons(inputId="fourth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                    radioButtons(inputId="fifth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                    radioButtons(inputId="sixth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                    radioButtons(inputId="seventh", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                    radioButtons(inputId="eighth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                    radioButtons(inputId="ninth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                    radioButtons(inputId="tenth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                    radioButtons(inputId="eleventh", label="a", c("Yes" = 1,"No" = 0)),
                    radioButtons(inputId="twelfth", label="a", c("Yes" = 1,"No" = 0)),
                    radioButtons(inputId="thirteenth", label="a", c("Yes" = 1,"No" = 0)),
                    radioButtons(inputId="fourteenth", label="a", c("Yes" = 1,"No" = 0)),
                    radioButtons(inputId="fifteenth", label="a", c("Yes" = 1,"No" = 0)),
                    radioButtons(inputId="sixteenth", label="a", c("Yes" = 1,"No" = 0)),
                    submitButton("Submit")),
      mainPanel(
                    tableOutput(outputId= "ans"),
                    uiOutput(outputId= "abc")

      ))
    )

    server <- function(input, output){ 

      new_depression <- reactive({ data.frame("ASadness"=input$first,
                                   "Loconcen"=input$second,
                                   "Asleep"=input$third,
                                   "Aappet"=input$fourth,
                                   "Loenergy"=input$fifth,
                                   "Foguilt"=input$sixth,
                                   "Asbehav"=input$seventh,
                                   "Sthough"=input$eighth,
                                   "Ppains"=input$ninth,
                                   "Eactivity"=input$tenth,
                                   "Wloss"=input$eleventh,
                                   "Ssupport"=input$twelfth,
                                   "Etdsthin"=input$thirteenth,
                                   "Dmaking"=input$fourteenth,
                                   "Fhopilln"=input$fifteenth,
                                   "Addiction"=input$sixteenth)
      })


        library(caret)
        depression_model <- read.csv("C:/Users/Project/web/depression_with_nd_p.csv" , header=TRUE , stringsAsFactors = F) 



        str(depression_model)
        head(depression_model )

        set.seed(3233)
        intrain <- createDataPartition(y = depression_model$Depression, p= 0.7, list = FALSE)
        training <- depression_model[intrain,]
        testing <- depression_model[-intrain,]

        dim(training)
        dim(testing)

        anyNA(depression_model)
        summary(depression_model)

        training[["Depression"]] = factor(training[["Depression"]])

        trctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3)
        set.seed(3233)

        svm_Linear <- train(Depression ~., data = training, method = "svmLinear",
                            trControl=trctrl,
                            preProcess = c("center", "scale"),
                            tuneLength = 10)

        svm_Linear
        summary(svm_Linear)

        test_pred <- predict(svm_Linear, newdata = testing)
        test_pred

        tab <- table(Predicted = test_pred, Actual = testing$Depression )
        tab

        1 - sum(diag(tab))/sum(tab)

        #I think the problem is here in new_depression
        answers <- reactive({
          predict(svm_Linear, newdata=new_depression())
        })

      #This works ok and it also shows the features of my dataset with the selected 
      #attributes I choose using radio buttons
      output$ans <- renderTable(new_depression())
      #Here it shows error that features of my dataset  were specified with 
      #different types from the fit
      output$abc <- renderUI({answers()})
    }


    shinyApp(ui, server)  

1 个答案:

答案 0 :(得分:0)

我认为您的问题与闪亮无关。尝试运行您的代码时出现此错误:

Warning: Error in : variables ‘ASadness’, ‘Loconcen’, ‘Asleep’, ‘Aappet’, ‘Loenergy’, ‘Foguilt’, ‘Asbehav’, ‘Sthough’, ‘Ppains’, ‘Eactivity’, ‘Wloss’, ‘Ssupport’, ‘Etdsthin’, ‘Dmaking’, ‘Fhopilln’, ‘Addiction’ were specified with different types from the fit

[没有可用的堆栈跟踪]

这看起来不像是一个错误。我搜索了该错误,这是第一个搜索结果:Error: variables were specified with different types from the fit

在这个答案中,似乎他们声称使用“标度”可能是罪魁祸首。