-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.R
194 lines (151 loc) · 5.67 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#main
library("httr")
library("dplyr")
library("plotly")
library("shiny")
library('leaflet')
library('RColorBrewer')
library('scales')
library('lattice')
library('ggplot2')
library('ggmap')
library('data.table')
source("FAQ.R")
source("graph.R")
shinyServer(function(input, output, session) {
house.data <- read.csv("data/house_data.csv")
house.data$total_area <- house.data$sqft_above + house.data$sqft_basement
output$user <- renderText({
answer <- paste0("Hi " ,input$user , ", this application helps you get information on the houses on sale in the King County Area.
You will find some answers to the questions you might have through maps, graphs and more")
return(answer)
})
############################# FAQ TAB #############################
# answer for question 1
output$answer1 <- renderText(
return(paste("A: Average number of bathrooms in houses priced over 1 million is ", bedrooms.average))
)
# answer for question 2
output$answer2 <- renderText(
return(paste("A: Average number of bathrooms in houses priced over 1 million is ", bathrooms.average))
)
# answer for question 3
output$answer3 <- renderText(
return(paste0("A: Average price of the houses in the best condition is $", best.condition.price))
)
# dynamic function for input from radio buttons
areaAns <- reactive({
if(input$Area == 'min') {
return(toString(filter(house.data, total_area == min(total_area)) %>%
select(price)))
}else{
return(toString(filter(house.data, total_area == max(total_area)) %>%
select(price)))
}
})
# answer for question 4
output$answer4 <- renderText(
return(paste0("A: Price of the house is $", areaAns()))
)
# dyanmic function for input from slider input
gradeAns <- reactive({
ans <- input$Grade
if(ans == 2){
return("There are no houses with grade 2")
}
grade.price.average <- paste0("$", toString(round(filter(house.data, grade == ans) %>%
summarise(mean = mean(price)))))
return(grade.price.average)
})
# answer for question 5
output$answer5 <- renderText(
return(paste0("A: Average price of the houses with the selected grade: ", gradeAns()))
)
# answer for question 6
output$answer6 <- renderText(
return(paste0("A: Average price of the houses with a waterfront: $", waterfront.price))
)
# dynamic method from input from numeric input
zipcodeAns <- reactive({
ans <- input$zipcode
zipcode.price.average <- round(filter(house.data, zipcode == ans) %>%
summarize(mean = mean(price)))
if(is.nan(zipcode.price.average[1,1])) {
return("There are no houses in this zipcode.")
}
return(paste0("$",zipcode.price.average))
})
# answer for question 7
output$answer7 <- renderText(
return(paste0("A: Average price of the house in the selected zipcode: ", zipcodeAns()))
)
# answer for question 8
output$answer8 <- renderText(
return(paste("A: The zipcode of the area with the highest average price of the houses: ", zipcode.highest.price))
)
############################# INTERACTIVE MAP TAB #############################
# Create the map
output$map <- renderLeaflet({
leaflet() %>%
addTiles(
urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>'
) %>%
setView(lng = -122.3321, lat = 47.6062, zoom = 10)
})
# Creates the circles and legend based on input by the user
observe({
colorBy <- input$color
sizeBy <- input$size
bedrm <- input$bdrm
filtered <- house.data[house.data$bedrooms == bedrm,]
colorData <- filtered[[colorBy]]
pal <- colorBin("viridis", colorData, 7, pretty = FALSE)
radius <- filtered[[sizeBy]] / max(filtered[sizeBy]) * 1000
leafletProxy("map", data = filtered) %>%
clearShapes() %>%
addCircles(~long, ~lat, radius=radius, layerId=~id,
stroke=FALSE, fillOpacity=0.4, fillColor=pal(colorData)) %>%
addLegend("bottomleft", pal=pal, values=colorData, title=colorBy,
layerId="colorLegend")
})
# Show a popup at the given location
popup <- function(id, lat, lng) {
selected <- house.data[house.data$id == id,]
content <- as.character(tagList(
tags$h4("Grade:", as.integer(selected$grade)),
tags$strong(HTML(paste0("(", sprintf("%s, %s",
selected$lat, selected$long), ")" )
)), tags$br(),
sprintf("Zipcode: %s", selected$zipcode), tags$br(),
sprintf("Price: $%s", selected$price), tags$br(),
sprintf("Bedrooms: %s", selected$bedrooms), tags$br(),
sprintf("Bathrooms: %s", selected$bathrooms),tags$br()
))
leafletProxy("map") %>% addPopups(lng, lat, content, layerId = id)
}
# When map is clicked, show a popup with house info
observe({
leafletProxy("map") %>% clearPopups()
event <- input$map_shape_click
if (is.null(event))
return()
isolate({
popup(event$id, event$lat, event$lng)
})
})
############################# GRAPH TAB #############################
output$graph1 <- renderPlotly({
g1 <- plot_ly(data = house.data, x = Area, y = Price1) %>%
layout(xaxis = x1 , yaxis = y1)
}
)
output$graph2 <- renderPlotly({
g2 <- plot_ly(data = house.data, x = Grade, y = Price2, xlab = "Grade of houses", ylab = "Price of houses") %>%
layout(xaxis = x2 , yaxis = y2)
}
)
output$graph3 <- renderPlotly({
g3 <- plot_ly(data = df1, x = ~Categories, y = ~AveragePrice)
})
})