Mercia Carolina Wentzel
September 24 2018
This presentation forms part of the final assignment of the Developing Data Products course offered by Johns Hopkins University at Coursera.org.
I have used the R dataset named mtcars to illustrate:
a Shiny user interface that uses a slider to obtain car weight as input - see code on third slide.
a Shiny server that predicts fuel consumption based on the selected car weight - see code on fourth slide.
The Shiny Fuel Consumption app has duly been deployed here:
library(shiny)
shinyUI(
fluidPage(
headerPanel(
"Shiny fuel consumption app"
),
sidebarPanel(
sliderInput(
inputId = 'selected_wt',
label = 'Slide to a car weight (1000 pounds) to predict fuel consumption (miles per gallon)',
value = 1.513, min = 1.513, max = 5.424, step = 0.001
),
submitButton('Submit')
),
mainPanel(
h3("Current car weight selection"), verbatimTextOutput("selected_wt"), h5("1000 pounds"), h3(""),
h3("Predicted fuel consumption"), verbatimTextOutput("predicted_mpg"), h5("miles per gallon"), h3(""),
plotOutput('plot')
)
)
)
library(shiny)
library(datasets)
shinyServer(
function(input, output) {
output$selected_wt <- renderPrint({
input$selected_wt
})
output$predicted_mpg <- renderPrint({
predict_mpg(input$selected_wt)
})
output$plot <- renderPlot({
plot(
mtcars$wt, mtcars$mpg, type="p",
main = "Car Weight and Fuel Consumption",
xlab = "Car Weight (1000 pounds)",
ylab = "Fuel Consumption (miles per gallon)") +
abline(lm(formula = "mpg ~ wt", data = mtcars))
})
}
)
predict_mpg <- function(carweight_lbs) {
fit <- lm(mpg ~ wt, data = mtcars)
est <- predict(fit, data.frame(wt = carweight_lbs), interval = "predict")
est[1,"fit"]
}
This presentation was created using RStudio Presenter.
Thank you