Shiny Application Diamond Price

13.08.2017

Data base

library ggplot2,

dataset “diamonds”

A data frame with 53940 rows and 10 variables:

2 variables are used for the application

price: price in US dollars ($326–$18,823)

carat: weight of the diamond (0.2–5.01)

Application

This simple application shows a prediction of a diamond price depending on the variable carat of a diamond.

Method: Linear regression

Input:

Please turn the slider to give in the carat of the diamond.

Then the application will show the price predicted with the “diamonds” dataset.

Code ui.R

library(shiny)
library(ggplot2)

shinyUI(fluidPage(
  titlePanel("Predict diamond price from carat"),
   sidebarLayout(
    sidebarPanel(
       sliderInput("sliderCarat", "Carat of the diamond",1,6,value=2)
    ),
    mainPanel(
       h3("Predicted price from the model (in US dollar): "),
       textOutput("pred1")
      )
    ) 
))

<!–html_preserve–>

Predict diamond price from carat

Predicted price from the model (in US dollar):

<!–/html_preserve–>

Code server.R

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {
  model1 <-lm(price ~ carat,data = diamonds)
  model1pred <- reactive({
    caratinput <-input$sliderCarat
    predict(model1, newdata = data.frame(carat = caratinput))
  })
 output$pred1 = renderText({
   model1pred()
 })   
})