-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.R
134 lines (121 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
shinyServer(function(input, output, session) {
##### NYC Interactive Map ##############################
# reactivate map info
mapdf <- reactive({
nycmap %>%
filter(boro %in% input$select_boro &
room_type %in% input$select_room &
price >= input$slider_price[1] &
price <= input$slider_price[2] &
review_scores_rating >= input$slider_rating[1] &
review_scores_rating <= input$slider_rating[2] &
number_of_reviews >= input$slider_review[1] &
number_of_reviews <= input$slider_review[2])
})
# create the map
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles("Esri.WorldStreetMap") %>%
addLegend(position = "bottomleft", pal = groupColors, values = room, opacity = 1, title = "Room Type") %>%
setView(lng = -73.9772, lat = 40.7527, zoom = 12)
})
# observe an event
observe({ #require a trigger to call the observe function
proxy <- leafletProxy("map",data = mapdf()) %>% #don't forget ()
clearMarkerClusters() %>%
clearMarkers() %>%
# circle
addCircleMarkers(lng = ~longitude, lat = ~latitude, radius = 2, color = ~groupColors(room_type),
group = "CIRCLE",
popup = ~paste('<b><font color="Black">','Listing Information','</font></b><br/>',
'Room Type:', room_type,'<br/>',
'Price:', price,'<br/>',
'Rating Score:', review_scores_rating, '<br/>',
'Number of Reviews:', number_of_reviews,'<br/>')) %>%
# cluster
addCircleMarkers(lng = ~longitude, lat = ~latitude, clusterOptions = markerClusterOptions(),
group = "CLUSTER",
popup = ~paste('<b><font color="Black">','Listing Information','</font></b><br/>',
'Room Type: ', room_type, '<br/>',
'Price:', price,'<br/>',
'Rating Score:', review_scores_rating, '<br/>',
'Number of Reviews:', number_of_reviews,'<br/>')) %>%
# circle/ cluster panel
addLayersControl(
baseGroups = c("CIRCLE","CLUSTER"),
options = layersControlOptions(collapsed = FALSE)
)
})
## reactivate count dataframe for map graph1
countdf <- reactive({
mapdf() %>%
group_by(., room_type) %>%
summarise(., count_type = n())
})
#map graph1
output$count_room <- renderPlotly({
plot_ly(data = countdf(), x = ~room_type, y = ~count_type, type = "bar", color = ~room_type,
colors = c('#E03A3C','#009DDC','#62BB47'),
hoverinfo = 'text',
text = ~count_type) %>%
layout(xaxis = list(title = "", showticklabels = FALSE),
yaxis = list(title = "count"), showlegend = FALSE,
annotations = list(x = ~room_type, y = ~count_type, text = ~paste(round(count_type/sum(count_type),2)*100,'%'),
xanchor = 'center', yanchor = 'bottom',
showarrow = FALSE)) %>%
config(displayModeBar = FALSE)
})
## reactivate price dataframe for map graph2
pricedf <- reactive({
mapdf() %>%
group_by(., room_type) %>%
summarise(., avg_price = round(mean(price),2))
})
#map graph2 avgprice
output$avgprice <- renderPlotly({
plot_ly(data = pricedf(), x = ~room_type, y = ~avg_price, type = "bar", color = ~room_type,
colors = c('#E03A3C','#009DDC','#62BB47'),
hoverinfo = 'text',
text = ~avg_price) %>%
layout(xaxis = list(title = "", showticklabels = FALSE),
yaxis = list(title = "price"), showlegend = FALSE,
annotations = list(x = ~room_type, y = ~avg_price, text = ~paste('$',avg_price),
xanchor = 'center', yanchor = 'bottom',
showarrow = FALSE)) %>%
config(displayModeBar = FALSE)
})
##### Listings, Boroughs and Price Changes #######################
## reactivate dataframe for listings grapgh
graph1df <- reactive({
nycmap %>%
select(boro,room_type,price,review_scores_rating) %>%
filter(price >= input$tab2_price[1] &
price <= input$tab2_price[2] &
review_scores_rating >= input$tab2_rating[1] &
review_scores_rating <= input$tab2_rating[2]) %>%
group_by(boro,room_type) %>%
summarise(n=n())
})
# listings grapgh
output$graph1 <- renderPlotly({
t <- list(size = 9)
plot_ly(data = graph1df(), x = ~n, y = ~room_type, type = "bar", color = ~boro,
colors = c('#800080','#009DDC','#E03A3C','#62BB47','#FFA500'),
orientation = 'h', showlegend = TRUE) %>%
layout(xaxis = list(title = "count"),
yaxis = list(title = ""),
barmode = 'dodge', font = t)
})
# price change graph (year/ month)
output$tab_price <- renderPlotly({
if(input$price_option == 'Year'){
m <- list(size = 8)
plot_ly(data = overall, x = ~year, y = ~avg_pricemo, type = 'scatter', mode ='markers', linetype = I('solid')) %>%
layout(xaxis = list(title = "", showticklabels = TRUE),
yaxis = list(title = "price"), showlegend = FALSE, font=m)} else{
plot_ly(data = monthdf, x = ~month, y = ~month_avg, type= 'scatter', mode = 'markers+lines', color = "Set1",
text = ~paste('Price: $', month_avg)) %>%
layout(xaxis = list(title = "month", type = 'category'),
yaxis = list(title = "price"))}
})
})