27/05/2019

R Markdown

This Shiny app is a predictor of mpg based on cylinder numbers(cyl) and horse power (hp) from mtcars dataset. It is needed to enter parameters on the left side, and on the right side there will be a prediction, and a plot of a linear regression. The project consists of two files: ui.R and server.R

ui.R insight

ui.R file consists of a sidebar part and main part. The side part consists of two parts, - a numeric input part for the number of hp with a step of 1 (defaulted to 145 due to mean being around 146) - and an optional cyl part (by default unspecified, drop down list due to only 3 possible different values).

On the right are the results.

mean(mtcars$hp)
## [1] 146.6875
unique(mtcars$cyl)
## [1] 6 4 8

ui.R file code

library(shiny)
shinyUI(fluidPage(
    titlePanel("Mpg prediction from mtcars dataset"),
    sidebarLayout(
        sidebarPanel(
            h3(helpText("Choose values:")),
            numericInput("hp", label = h4("hp"), step = 1, value = 145),
            selectInput("cyl", label = h4("cyl"), 
                        choices = list("unspecified" = "*", "4" = "4", "6" = "6", "8" = "8"))
        ),
        mainPanel(
            h4("Predicted value of this diamond is:"),
            h3(textOutput("result")),
            h4(""),
            h4("Regression results:"),
            plotOutput("distPlot")
        )
    )

server.R insight

The server part consists of two parts, the first part being the plot, and the second being the prediction. The plot has been done with ggplot2 (smoothed). The prediction part can be run both with and without cylinder number specified.

server.R file

library(shiny)
library(ggplot2)
library(dplyr)
cars <- mtcars
shinyServer(function(input, output) {
    output$distPlot <- renderPlot({
        # select diamonds depending of user input
        cars <- filter(mtcars, grepl(input$cyl, cyl))
        # build linear regression model
        fit <- lm( mpg~hp, cars)
        # predicts the price 
        pred <- predict(fit, newdata = data.frame(hp = input$hp, cyl = input$cyl))
        # Drow the plot using ggplot2
        plot <- ggplot(data=cars, aes(x=hp, y = mpg))+
            geom_point(aes(color = cyl), alpha = 0.3)+
            geom_smooth(method = "lm")+
            geom_vline(xintercept = input$hp, color = "green")+
            geom_hline(yintercept = pred, color = "red")
        plot
    })
    output$result <- renderText({
        # renders the text for the prediction below the graph
        cars <- filter(cars, grepl(input$cyl, cyl))
        fit <- lm( mpg~hp, cars)
        pred <- predict(fit, newdata = data.frame(hp = input$hp, cyl = input$cyl))
        res <- paste(round(pred, digits = 2), "$")
        res
    })
    
})

All the files can be found at