Yinning Zhang
12/28/2018
Please find my App at https://zhangyinning.shinyapps.io/Prediction/. For this Shiny App, I use the dataset of cars, which comes from the dataset library. I generate a linear regression model and created an interactive plot to predict the result. The code for Server.R covers the following parts:
Create the regression model:
library(shiny)
model1 <- lm(dist ~ speed, data=cars)
model1pred <- reactive({
speedInput <- input$sliderSpeed
predict(model1, newdata = data.frame(speed = speedInput))
})
Create the plot.
renderPlot({
speedInput <- input$sliderSpeed
plot(cars$speed, cars$dist, xlab = "Speed", ylab = "Stopping Distance", bty = "n", pch = 16, xlim = c(0, 30), ylim = c(0, 150))
abline(model1, col = "red", lwd = 2)
legend(25, 250, "Linear Model Prediction", pch = 16, col = "red", bty = "n", cex=1.2)
points(speedInput, model1pred(), col = "red", pch =16, cex = 2)
})
<!–html_preserve–>
<!–/html_preserve–>The ui file get the input of speed which is used to predict Stopping Distance. Also, it shows the plot and the predicted result. Here is the code for the input Sidebar
sliderInput("sliderSpeed",
"Driving Speed",
min = 4,
max = 25,
value = 20)
<!–html_preserve–>