Caret and Shiny R giving error

0 votes

I'm trying to develop a simple app in shiny. It throws this error:

Warning: Error in [.data.frame: undefined columns selected Stack trace (innermost first): 70: [.data.frame 69: [ 68: sweep 67: predict.preProcess 66: predict 65: probFunction 64: predict.train 63: predict 62: predict 61: is.data.frame 60: data.matrix 59: observerFunc [#17] 4: 3: do.call 2: print.shiny.appobj 1: ERROR: [on_request_read] connection reset by peer

code:

require(shiny)
require(plyr)
require(dplyr)
require(ggplot2)
require(caret)
require(xgboost)

require(titanic)
df=na.omit(titanic_train)
y=data.matrix(select(df, Survived))
y[y==0]="N"
y[y==1]="Y"
x=data.matrix(select(df, Pclass, Age, SibSp, Parch, Fare))

tCtrl <- trainControl(method = "repeatedcv", number = 3, repeats=3, summaryFunction = twoClassSummary, verbose=TRUE, classProbs = TRUE)
fit_xgbTree= train(x, y, method = "xgbTree" , family= "binomial", trControl = tCtrl, metric = "ROC", preProc = c("center", "scale"))

ui = pageWithSidebar(
  headerPanel("Titanic"),
  sidebarPanel(
    radioButtons("Pclass", "Passenger Class", choices=c("1", "2", "3"),selected = "1", inline = TRUE,width = NULL),
    sliderInput("Age", "Passenger Age", min=0, max=80, value=30),
    radioButtons("SibSp", "SibSp", choices=c("0", "1", "2", "3", "4", "5")),
    radioButtons("Parch", "Parch", choices=c("0", "1", "2", "3", "4", "5", "6")),
    sliderInput("Fare", "Passenger Fare", min=0, max=520, value=35)
  ),
  mainPanel(
    dataTableOutput('testTable'),
    textOutput('outputBox')
  )
)

server=function(input, output){

  values <- reactiveValues()

  newEntry <- observe({ # use observe pattern

    x=as.data.frame(matrix(0, nrow=1, ncol=5))
    colnames(x)=c("Pclass", "Age",    "SibSp", "Parch",  "Fare")

    x[1,1]=as.numeric(input$Pclass)
    x[1,2]=input$Age
    x[1,3]=as.numeric(input$SibSp)
    x[1,4]=as.numeric(input$Parch)
    x[1,5]=input$Fare


    pred <- data.matrix(predict(object=fit_xgbTree, x, type="prob")[,2])
    isolate(values$df <- x)
    #isolate(values$df2 <- x)
  })

  output$testTable <- renderDataTable({values$df})
}

shinyApp(ui=ui, server=server)
Dec 5, 2018 in Data Analytics by Ali
• 11,360 points
1,282 views

1 answer to this question.

0 votes

try this server.R code:

server=function(input, output){

  values <- reactiveValues()

  newEntry <- observe({ # use observe pattern

    x=as.data.frame(matrix(0, nrow=1, ncol=6))
    colnames(x)=c("Pclass", "Age",    "SibSp", "Parch",  "Fare", "SurvProb")

    x[1,1]=as.numeric(input$Pclass)
    x[1,2]=input$Age
    x[1,3]=as.numeric(input$SibSp)
    x[1,4]=as.numeric(input$Parch)
    x[1,5]=input$Fare

    pred <- data.matrix(predict(object=fit_xgbTree, x[-length(x)], type="prob")[,2])
    x[1,6] <- round(pred,2)

    isolate(values$df <- x)
    #isolate(values$df2 <- x)
  })

  output$testTable <- renderDataTable({values$df})
}
answered Dec 5, 2018 by Maverick
• 10,840 points

Related Questions In Data Analytics

0 votes
1 answer

Error saying 'shinyServer(function(input,output){ output`$histogram`" while creating dashboard using shiny R

Try this for your server.R library(shiny) library(shinydashboard) shinyServer(function(input,output){ output$`$histogram`<- ...READ MORE

answered Nov 30, 2018 in Data Analytics by Maverick
• 10,840 points
1,436 views
0 votes
1 answer

Error saying "ERROR: Expected tag to be of type header" while creating a dashboard using shiny R

sliderInput("bins","number of breaks",1,100,50), this line should come under ...READ MORE

answered Nov 30, 2018 in Data Analytics by Maverick
• 10,840 points
4,550 views
0 votes
1 answer

Dynamically select element from a list and work with it - Shiny R

Follow these steps: Import the data into R Check ...READ MORE

answered Dec 5, 2018 in Data Analytics by Maverick
• 10,840 points
3,743 views
+1 vote
1 answer

Error saying "could not find function "shinyUI"" in shiny R

Its a small spelling mistake that you've ...READ MORE

answered Nov 28, 2018 in Data Analytics by Maverick
• 10,840 points
2,602 views
+1 vote
1 answer

Error saying "could not find function dashboardPage" in shiny R

Include this line in the code: Library(shinydashboard) READ MORE

answered Nov 29, 2018 in Data Analytics by Maverick
• 10,840 points
3,617 views
0 votes
1 answer
0 votes
1 answer

I'm trying to start rattle on R Studio and end up with an error

You need the package RGtk2 for rattle to ...READ MORE

answered Nov 26, 2018 in Data Analytics by Maverick
• 10,840 points
4,180 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP