-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathserver.R
204 lines (183 loc) · 8.01 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
194
195
196
197
198
199
200
201
202
#
# 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/
#
###############################Install Related Packages #######################
if (!require("shiny")) {
install.packages("shiny")
library(shiny)
}
if (!require("leaflet")) {
install.packages("leaflet")
library(leaflet)
}
if (!require("leaflet.extras")) {
install.packages("leaflet.extras")
library(leaflet.extras)
}
if (!require("dplyr")) {
install.packages("dplyr")
library(dplyr)
}
if (!require("magrittr")) {
install.packages("magrittr")
library(magrittr)
}
if (!require("mapview")) {
install.packages("mapview")
library(mapview)
}
if (!require("leafsync")) {
install.packages("leafsync")
library(leafsync)
}
#Data Processing
total_citi_bike_df = read.csv('../data/citibike_data.csv')
##compute the daily in and out difference for the station
total_citi_bike_df$day_diff = total_citi_bike_df$endcount - total_citi_bike_df$startcount
#assign each column to weekend or weekday
total_citi_bike_df$weekend_or_weekday = ifelse(total_citi_bike_df$weekday %in% c('Saturday','Sunday'), "Weekend", "Weekday")
#station info
citi_bike_station_info <- total_citi_bike_df[,c('station_id','station_name','station_longitude','station_latitude')]
#remove the duplicates based on station id
citi_bike_station_info <- citi_bike_station_info[!duplicated(citi_bike_station_info[ , c("station_id")]),]
#split the bike data to pre-covid and covid time period
citi_bike_pre_covid_df = total_citi_bike_df[difftime(total_citi_bike_df$date,"2019-05-31")<=0,] #2019-05-01 ~ 2019-05-31
citi_bike_covid_df = total_citi_bike_df[difftime(total_citi_bike_df$date,"2020-04-30")>=0,] #2020-05-01 ~ 2021-05-31
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
## Map Tab section
output$left_map <- renderLeaflet({
#adjust for weekday/weekend effect
if (input$adjust_time =='Overall') {
leaflet_plt_df <- citi_bike_pre_covid_df %>%
group_by(station_id) %>%
summarise(total_start_count = sum(startcount),
total_end_count = sum(endcount),
total_day_diff = sum(day_diff),
total_diff_percentage = sum(day_diff)/sum(startcount),
) %>% left_join(citi_bike_station_info,by='station_id')
} else {
leaflet_plt_df <- citi_bike_pre_covid_df %>%
filter(weekend_or_weekday == input$adjust_time) %>%
group_by(station_id) %>%
summarise(total_start_count = sum(startcount),
total_end_count = sum(endcount),
total_day_diff = sum(day_diff),
total_diff_percentage = sum(day_diff)/sum(startcount),
) %>% left_join(citi_bike_station_info,by='station_id')
}
map_2019 <- leaflet_plt_df %>%
leaflet(options = leafletOptions(minZoom = 11, maxZoom = 13)) %>%
addTiles() %>%
addProviderTiles("CartoDB.Positron",
options = providerTileOptions(noWrap = TRUE)) %>%
setView(-73.9834,40.7504,zoom = 12)
if (input$adjust_score == 'start_cnt') {
map_2019 %>%
addHeatmap(
lng=~station_longitude,
lat=~station_latitude,
intensity=~total_start_count,
max=4000,
radius=8,
blur=10)
}else if (input$adjust_score == 'end_cnt') {
map_2019 %>%
addHeatmap(
lng=~station_longitude,
lat=~station_latitude,
intensity=~total_end_count,
max=4000,
radius=8,
blur=10)
} else if (input$adjust_score == 'day_diff_absolute'){
map_2019 %>%
addHeatmap(
lng=~station_longitude,
lat=~station_latitude,
intensity=~total_day_diff,
max=50,
radius=8,
blur=10)
}else if (input$adjust_score == 'day_diff_percentage'){
map_2019 %>%
addHeatmap(
lng=~station_longitude,
lat=~station_latitude,
intensity=~total_diff_percentage,#change to total day diff percentage
max=0.1,
radius=8,
blur=10)
}
}) #left map plot
output$right_map <- renderLeaflet({
#adjust for weekday/weekend effect
if (input$adjust_time =='Overall') {
leaflet_plt_df <- citi_bike_covid_df %>%
group_by(station_id) %>%
summarise(total_start_count = sum(startcount),
total_end_count = sum(endcount),
total_day_diff = sum(day_diff),
total_diff_percentage = sum(day_diff)/sum(startcount),
) %>% left_join(citi_bike_station_info,by='station_id')
} else {
leaflet_plt_df <- citi_bike_covid_df %>%
filter(weekend_or_weekday == input$adjust_time) %>%
group_by(station_id) %>%
summarise(total_start_count = sum(startcount),
total_end_count = sum(endcount),
total_day_diff = sum(day_diff),
total_diff_percentage = sum(day_diff)/sum(startcount),
) %>% left_join(citi_bike_station_info,by='station_id')
}
#initial the map to plot on
map_2020 <- leaflet_plt_df %>%
leaflet(options = leafletOptions(minZoom = 11, maxZoom = 13)) %>%
addTiles() %>%
addProviderTiles("CartoDB.Positron",
options = providerTileOptions(noWrap = TRUE)) %>%
setView(-73.9834,40.7504,zoom = 12)
if (input$adjust_score == 'start_cnt') {
map_2020 %>%
addHeatmap(
lng=~station_longitude,
lat=~station_latitude,
intensity=~total_start_count, #change to total start count
max=4000,
radius=8,
blur=10)
}else if (input$adjust_score == 'end_cnt') {
map_2020 %>%
addHeatmap(
lng=~station_longitude,
lat=~station_latitude,
intensity=~total_end_count,#change to total end count
max=4000,
radius=8,
blur=10)
} else if (input$adjust_score == 'day_diff_absolute'){
map_2020 %>%
addHeatmap(
lng=~station_longitude,
lat=~station_latitude,
intensity=~total_day_diff,#change to total day diff
max=50,
radius=8,
blur=10)
}else if (input$adjust_score == 'day_diff_percentage'){
map_2020 %>%
addHeatmap(
lng=~station_longitude,
lat=~station_latitude,
intensity=~total_diff_percentage,#change to total day diff percentage
max=0.1,
radius=8,
blur=10)
}
}) #right map plot
})