0
library(shiny)


ui <- fluidPage(

  titlePanel("Linear model DARP"),


  sidebarLayout(
  sidebarPanel(

     sliderInput(inputId = "area",
                 "select the service region area:",
                 min= 170,
                 max= 8000,
                 value=1001),
     sliderInput(inputId = "crit..peak",
                 label="Choose Peak demand:",
                 min=10,
                 max=150,
                 value=39)
  ),


  mainPanel(
     tableOutput("table")
  )
  )
  )


server <- function(input, output) {

   output$table <- renderTable({

    df_ln<-read.csv("F:/Project/Programme/ML/DAR Machine Learning TR Part A/train_darp_ln.csv")
Linearmodel_DARP<-lm(veh~area+crit..peak,data = df_ln)
 new_demand1<-data.frame(area=input$area)
 new_demand2<-data.frame(crit..peak=input$crit..peak
 fleetsize<-predict(Linearmodel_DARP,newdata=c(new_demand1,new_demand2))
 round(exp(fleetsize),0)
})
}

shinyApp(ui = ui, server = server) I am getting error object crit..peak is not found when running the app The app should take two inputs from the user through the slider and based on the multiple regression it will give a prediction of the predict command please help as I need to do it soon for a project

structure(list(area = c(2217.7, 6537.4, 1705.5, 5634, 1260.5, 
4797.7), density = c(0.13753, 0.016826, 0.18469, 0.021477, 0.25862, 
0.027305), crit..CV = c(0.63954, 0.81437, 0.49909, 0.33935, 0.39148, 
0.17489), crit..peak = c(49L, 26L, 41L, 20L, 39L, 18L), TW = c(21L, 
47L, 54L, 48L, 17L, 41L), L = c(569L, 576L, 391L, 390L, 458L, 
392L), s = c(7L, 3L, 3L, 6L, 3L, 2L), speed = c(18L, 26L, 20L, 
30L, 24L, 33L), circuity = c(1.3284, 1.1494, 1.4597, 1.2725, 
1.0486, 1.0792), cap = c(9L, 9L, 5L, 8L, 5L, 7L), mrt = c(1.5452, 
2.3743, 1.5962, 2.6065, 2.1278, 2.6228), veh = c(4.605170186, 
3.433987204, 4.718498871, 3.951243719, 4.060443011, 3.526360525
), veh.hrs = c(6.665569062, 5.523778231, 6.496186582, 5.71857256, 
5.816843267, 5.256713817), veh.km = c(9.555940819, 8.781874769, 
9.491918855, 9.119769942, 8.994897097, 8.753221378)), .Names = c("area", 
"density", "crit..CV", "crit..peak", "TW", "L", "s", "speed", 
"circuity", "cap", "mrt", "veh", "veh.hrs", "veh.km"), row.names = c(NA, 
 6L), class = "data.frame")
P Initiate
  • 79
  • 5
  • Please post a minimal sample of your data in the text of your question. Read this to see how: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – divibisan Apr 24 '18 at 14:14
  • Yes, crit..peak is a variable name in data frames.I want to build a regression model using the original data and want to predict from the user input two variables i.e area and crit..peak – P Initiate Apr 24 '18 at 14:54

2 Answers2

0

Ok, so your problem is probably due to the way you try to make your new data frame. You made two separate 1 dimensional data frames and then concatenated them, which generated a list of dataframes. To make a data frame with two or more variables, define them in your data frame definition or use cbind to join the data frames together:

 new_demand <- data.frame(area = input$area,
                          crit..peak = input$crit..peak)
 fleetsize <- predict(Linearmodel_DARP, newdata = new_demand)

This should solve your problem. In the future, when you get errors like error object ... is not found, the first thing to do is check that the objects you're generating are what you think they are. The class function would have told you that c(new_demand1, new_demand2) is a list not a data.frame

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • Yeah your solution helped Thanks – P Initiate Apr 28 '18 at 08:09
  • Can you tell me how to reactively input the resonse variable and get the prediction based on the selected variable in the same model – P Initiate Apr 28 '18 at 08:10
  • `Linearmodel_DARP<-lm(df_ln[[input$response_var]]~area+crit..peak,data = df_ln)` – divibisan Apr 30 '18 at 15:31
  • To select the response variable by passing a character string provided by some responsive input object, you need to use ``[[]]`` notation, rather than just providing the bare variable name – divibisan Apr 30 '18 at 15:33
  • `data(cars); lm(speed~dist,data=cars)` gives the same result as: `data(cars); lm(cars[['speed']]~dist,data=cars)` – divibisan Apr 30 '18 at 15:34
0

The error could be because of read.csv as it's missing header = T.

Let's try this chunk of code

server <- function(input, output) {
  output$table <- renderTable({
    df_ln <- read.csv("F:/Project/Programme/ML/DAR Machine Learning TR Part A/train_darp_ln.csv", header = T)
    Linearmodel_DARP <- lm(veh~area+chrit..peak, data = df_ln)
    new_demand1 <- data.frame(area=input$area)
    new_demand2 <- data.frame(crit..peak=input$crit..peak)
    fleetsize <- predict(Linearmodel_DARP, newdata=c(new_demand1, new_demand2))
    round(exp(fleetsize), 0)
  })
}
Prem
  • 11,775
  • 1
  • 19
  • 33