-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
50 lines (41 loc) · 1.73 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
# This is a shiny app to determine diamond price based on carat, cut, color, and clarity
library(shiny)
library(ggplot2)
library(curl)
# Define server logic
shinyServer(function(input, output) {
# load data
data("diamonds")
# create the initial output
output$distPlot <- renderPlot({
# subset the data based on the inputs
diamonds_sub <- subset(diamonds, cut == input$cut &
color == input$color &
clarity == input$clarity)
# plot the diamond data with carat and price
p <- ggplot(data = diamonds_sub, aes(x = carat, y = price)) + geom_point()
p <- p + geom_smooth(method = "lm") + xlab("Carat") + ylab("Price")
p <- p + xlim(0, 6) + ylim (0, 20000)
p
}, height = 700)
# create linear model
output$predict <- renderPrint({
diamonds_sub <- subset( diamonds, cut == input$cut &
color == input$color &
clarity == input$clarity)
fit <- lm(price~carat,data=diamonds_sub)
unname(predict(fit, data.frame(carat = input$lm)))})
observeEvent(input$predict, {distPlot <<- NULL
output$distPlot <- renderPlot({p <- ggplot(data = diamonds, aes(x = carat, y = price)) + geom_point()
p <- p + geom_smooth(method = "lm") + xlab("Carat") + ylab("Price")
p <- p + xlim(0, 6) + ylim (0, 20000)
p
}, height = 700)})})