January 23, 2016

Use Case

  • "What temperature is it Mom?"
  • "70 degrees"
  • "Is it windy?"
  • "No idea."
  • "That's ok, I have this handy app that Amanda made!"

ui.R

library(shiny)
shinyUI(fluidPage(
        titlePanel("Predict Wind Speed from Temperature"),
        sidebarLayout(
                sidebarPanel(
                        numericInput("TempInput",
                                     "What is the temperature in degrees F?",
                                     70, min = 50, max = 100, step = 1),
                        checkboxInput("showModel", "Show Model", value = TRUE),
                        checkboxInput("showInt", "Show Prediction", value = TRUE),
                        submitButton("Go!")
                        ),
                mainPanel(
                        plotOutput("plot1"),
                        h3("Predicted Wind:"),
                        textOutput("pred")
                )
        )
))

server.R: Part 1

library(shiny)
library(ggplot2)
shinyServer(function(input, output) {
        lm <- lm(Wind ~ Temp, data = airquality)
        pred <- reactive({
                TempIn <- input$TempInput
                predict(lm, newdata = data.frame(Temp = TempIn))
        })
        output$plot1 <- renderPlot({
                TempIn <- input$TempInput
                g <- ggplot(data = airquality, aes(x = Temp, y = Wind)) +
                        geom_point()
                print(g)
                if(input$showModel){
                        g <- g + geom_smooth(method = "lm")
                        print(g)
                }

server.R: Part 2

                if(input$showInt){
                        g <- g + geom_vline(xintercept = input$TempInput,
                                            color="red")
                        print(g)
                }
                output$pred <- renderText({
                        pred()
                })
        })
})

Final App