-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
414 lines (329 loc) · 14.9 KB
/
app.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#################################################
# author: "Zheyan Liu"
# date: "11/18/2021"
#################################################
# change working directory
# R packages
library(shiny)
library(shinythemes)
library(shinyTime)
library(tidyverse)
library(reticulate)
library(leaflet)
PYTHON_DEPENDENCIES = c('pip', 'numpy','pandas','googlemaps','datetime','sklearn')
# use local python
use_python('/Users/jimmy/anaconda3/python.exe')
# ------------------ App virtualenv setup (Do not edit) ------------------- #
virtualenv_dir = Sys.getenv('VIRTUALENV_NAME')
python_path = Sys.getenv('PYTHON_PATH')
#
# Create virtual env and install dependencies
reticulate::virtualenv_create(envname = virtualenv_dir, python = python_path)
reticulate::virtualenv_install(virtualenv_dir, packages = PYTHON_DEPENDENCIES, ignore_installed=TRUE)
reticulate::use_virtualenv(virtualenv_dir, required = T)
# ------------------ App server logic (Edit anything below) --------------- #
# Import python functions to R
reticulate::source_python('GetRoute.py')
# Set time zone
Sys.setenv(TZ="America/New_York")
subwayIcons <- icons(
iconUrl = "https://maps.gstatic.com/mapfiles/transit/iw2/6/subway2.png",
iconWidth = 12, iconHeight = 12,
iconAnchorX = 12, iconAnchorY = 12,
)
# read cri train
cri = read_csv('data/df_train_val06.csv')
# mygoogle_routes$start_location = '168 st, NY'
# mygoogle_routes$destination = '24 st, NY'
# # it is a R dataframe
# df =
# mygoogle_routes$get_directions() %>%
# mutate(num_stops = as.integer(num_stops),
# route_num = as.integer(route_num))
#
#
# mygoogle_routes$directions_df = reticulate::r_to_py(df)
# mygoogle_routes$get_stops()
####################################
# User Interface #
####################################
ui <- fluidPage(theme = shinytheme("paper"),
navbarPage("Crime-free Subway Route Identification",
tabPanel("Home",
# Input values
sidebarPanel(
HTML("<h3>Input parameters</h3>"),
HTML("<h5>Who are you?</h5>"),
selectInput("gender", "Your Gender",
list("Female", "Male")),
selectInput("age", "Your Age:",
list("<18", "18-24",'25-44','45-64','65+')),
selectInput("race", "Your Race:",
list(`Hispanic` = list('BLACK HISPANIC', 'WHITE HISPANIC'),
`Non-Hispanic` = list("BLACK", "WHITE",'ASIAN / PACIFIC ISLANDER', 'AMERICAN INDIAN/ALASKAN NATIVE')),
selected = 'BLACK'),
HTML("<h5>When you leave?</h5>"),
dateInput("start_date", "Date(YYYY-MM-DD):", value = Sys.Date(), min = Sys.Date(), max = Sys.Date() + 14),
# Default is current time + 3 min
timeInput("time_input", "Time(HH:MM:SS):", value = strptime(unlist(strsplit(as.character(Sys.time() + 180), split = ' '))[2], "%T")),
HTML("<h5>Where to go?</h5>"),
textInput("start_location", "Your Location:", "168 st"),
textInput("destination", "Place of Interest:", "Prospect Park"),
actionButton("submitbutton",
"Submit",
class = "btn btn-primary")
),
mainPanel(
uiOutput("urls"),
tags$label(h3('Routes')), # Status/Output Text Box
verbatimTextOutput('contents'),
DT::dataTableOutput("tabledata"), # Results DT table
# Leaflet map
verbatimTextOutput('routes_selection_box'),
DT::dataTableOutput("tabledata2"), # Results DT table
DT::dataTableOutput("tabledata3"), # Results DT table
leafletOutput("mymap")
) # mainPanel()
)
) # navbarPage()
) # fluidPage()
####################################
# Server #
####################################
server <- function(input, output, session) {
url = a('Crime-score explaination', href='https://zheyanliu99.github.io/SubwayCrime/report_model.html')
output$urls <- renderUI({
tagList("Click to see how we get crime score from GNN model:", url)
})
observeEvent(input$submitbutton, {
cat("Click Once")
})
departure_time = eventReactive(input$submitbutton, {
paste(input$start_date, unlist(strsplit(as.character(input$time_input), split = ' '))[2], sep = ' ')
})
# Input Data
directions_raw = eventReactive(input$submitbutton, {
mygoogle_routes = google_routes()
# take dependence on button
input$submitbutton
mygoogle_routes$start_location = isolate(paste(input$start_location, 'New York', sep = ','))
mygoogle_routes$destination = isolate(paste(input$destination, 'New York', sep = ','))
# it is a R dataframe
df =
mygoogle_routes$get_directions(departure_time()) %>%
mutate(num_stops = as.integer(num_stops),
route_num = as.integer(route_num))
print(df)
})
directions_grouped = eventReactive(input$submitbutton, {
# take dependence on button
input$submitbutton
mygoogle_routes = google_routes()
mygoogle_routes$directions_df = reticulate::r_to_py(directions_raw())
df3 =
directions_raw() %>%
mutate(
# change time into minuates
time = round(time/60),
# change distance into miles
distance = round(distance/1609, 1),
walking_distance = round(walking_distance/1609, 2)) %>%
group_by(route_num) %>%
# mutate(line = paste0(line, num_stops, collapse = '(')) %>%
summarise(time = mean(time),
distance = mean(distance),
walking_distance = mean(walking_distance),
line = paste0(line, '[', as.character(num_stops), ']', collapse = " - "),
n = n()) %>%
mutate(
# crime_score = round(runif(n),2),
crowdness_score = round(runif(n),2)
) %>%
# distinct(line, .keep_all = TRUE) %>%
select(-n, -distance) %>%
# select distinct
distinct(line, .keep_all = TRUE)
})
df_withmodel = eventReactive(input$submitbutton, {
input$submitbutton
# print(class(directions_raw()))
mygoogle_routes = google_routes()
mygoogle_routes$directions_df = reticulate::r_to_py(directions_raw())
adate = as.character(input$start_date)
df1 =
mygoogle_routes$get_stops() %>%
mutate(line = ifelse(line == '7X', '7', line)) %>%
mutate(group = paste(as.character(route_num), line)) %>%
# add subway service
mutate(service =
case_when( line %in% c('A', 'C', 'E') ~ "8 Avenue(ACE)",
line %in% c('S') ~ "Shuttle(S)",
line %in% c('B', 'D', 'F', 'M') ~ "6 Avenue(BDFM)",
line %in% c('G') ~ "Brooklyn-Queens Crosstown(G)",
line %in% c('L') ~ "14 St-Canarsie(L)",
line %in% c('N', 'Q', 'R', 'W') ~ "Broadway(NQRW)",
line %in% c('1', '2', '3') ~ "7 Avenue(123)",
line %in% c('4', '5', '6') ~ "Lexington Av(456)",
line %in% c('7') ~ "Flushing(7)",
TRUE ~ 'other_line')) %>%
mutate(gender = input$gender,
age = input$age,
race = input$race,
date = adate,
time = as.character(input$time_input)) %>%
mutate(gender = ifelse(gender == 'Female', 'F', 'M')) %>%
relocate(route_num)
df2 = impute_and_match(df1)
print(df2)
df2 =
df2 %>%
select(route_num, date, time, age, race, gender, service, cluster)
# code to check
# write.csv(df2,'data/test_r.csv', row.names = FALSE)
GNNpredict(df2)
})
directions_grouped2 = eventReactive(input$submitbutton, {
# take dependence on button
input$submitbutton
directions_grouped() %>%
left_join(df_withmodel(), by = 'route_num') %>%
mutate(crime_score = round(crime_score, 3)) %>%
relocate(route_num, time, walking_distance, crime_score, crowdness_score, line) %>%
rename('line[stops]' = line,
'time(min)' = time,
# 'distance(mile)' = distance,
'walking_distance(mile)' = walking_distance) %>%
select(-crowdness_score)
})
df_map = reactive({
# print(class(directions_raw()))
mygoogle_routes = google_routes()
mygoogle_routes$directions_df = reticulate::r_to_py(directions_raw())
mygoogle_routes$get_stops() %>%
filter(route_num %in% input$tabledata_rows_selected) %>%
mutate(line = ifelse(line == '7X', '7', line)) %>%
mutate(group = paste(as.character(route_num), line)) %>%
# add subway service
mutate(service =
case_when( line %in% c('A', 'C', 'E') ~ "8 Avenue(ACE)",
line %in% c('S') ~ "Shuttle(S)",
line %in% c('B', 'D', 'F', 'M') ~ "6 Avenue(BDFM)",
line %in% c('G') ~ "Brooklyn-Queens Crosstown(G)",
line %in% c('L') ~ "14 St-Canarsie(L)",
line %in% c('N', 'Q', 'R', 'W') ~ "Broadway(NQRW)",
line %in% c('1', '2', '3') ~ "7 Avenue(123)",
line %in% c('4', '5', '6') ~ "Lexington Av(456)",
line %in% c('7') ~ "Flushing(7)",
TRUE ~ 'other_line')) %>%
relocate(route_num)
})
# Status/Output Text Box
output$contents <- renderPrint({
if (input$submitbutton>0) {
isolate("Routes Found")
isolate(paste('Departures at',departure_time()))
# isolate(input$time_input)
} else {
return("Please enter your start location and destination")
}
})
# print the selected indices
output$routes_selection_box = renderPrint({
s = input$tabledata_rows_selected
if (length(s)) {
cat('You have selected ')
cat(paste('Route',s), sep = ', ')
}
else{
cat("You can click on Routes above to show Route Maps")
}
})
output$tabledata <- DT::renderDataTable({
input$submitbutton
if (input$submitbutton>0) {
DT::datatable(directions_grouped2(),
options = list(scrollX = TRUE),
rownames = FALSE)
}
})
# output$tabledata2 <- DT::renderDataTable({
# print(input$tabledata_rows_selected)
# if (input$submitbutton>0) {
#
# DT::datatable(df_map() %>% filter(route_num %in% input$tabledata_rows_selected) ,
# options = list(scrollX = TRUE),
# rownames = FALSE)
# }
# })
#
# output$tabledata3 <- DT::renderDataTable({
# if (input$submitbutton>0) {
#
# DT::datatable(df_withmodel(),
# options = list(scrollX = TRUE),
# rownames = FALSE)
# }
# })
#
output$mymap <- renderLeaflet({
# Use leaflet() here, and only include aspects of the map that
# won't need to change dynamically (at least, not unless the
# entire map is being torn down and recreated).
leaflet(quakes) %>%
# Stamen.TonerLite
addProviderTiles(providers$CartoDB.Positron) %>%
setView(-73.8399986, 40.746739, zoom = 10)
})
pal <-
colorFactor(palette = c("blue", "azure4", "orange",'green','brown','yellow','red','forestgreen','purple'),
levels = c('8 Avenue(ACE)',
'Shuttle(S)',
'6 Avenue(BDFM)',
'Brooklyn-Queens Crosstown(G)',
'14 St-Canarsie(L)',
'Broadway(`NQRW`)',
'7 Avenue(123)',
'Lexington Av(456)',
'Flushing(7)'))
pal2 <-
colorFactor(palette = c("blue", 'blue', 'blue',
"azure4",
"orange", "orange", "orange", "orange",
'green',
'brown',
'yellow', 'yellow', 'yellow', 'yellow',
'red', 'red', 'red',
'forestgreen', 'forestgreen', 'forestgreen',
'purple'),
levels = c('A', 'C', 'E',
'S',
'B', 'D', 'F', 'M',
'G',
'L',
'N', 'Q', 'R', 'W',
'1', '2', '3',
'4', '5', '6',
'7'))
observe({
leafletProxy("mymap", data = df_map()) %>%
clearShapes() %>%
clearMarkers() %>%
clearControls()
# print('I am here')
# print(df_map() %>% distinct(group) %>% pull(group))
for(group in df_map() %>% distinct(group) %>% pull(group)){
leafletProxy("mymap", data = df_map()) %>%
addPolylines(lng = ~long, lat = ~lat, data=df_map()[df_map()$group==group,], color=~pal(service))
}
leafletProxy("mymap", data = df_map()) %>%
# addCircles(lng = ~lng , lat = ~lat, weight = 1, stroke = FALSE,
# radius = 400, opacity = 1, fillOpacity = 1)
addMarkers(lng = ~long, lat = ~lat, icon = subwayIcons) %>%
addLegend("topright", pal = pal2, values = ~line,
title = "Line", opacity = 0.75)
})
}
####################################
# Create Shiny App #
####################################
shinyApp(ui = ui, server = server)