-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.R
185 lines (144 loc) · 4.17 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
###############################################################################
# Defining Server Logic behind App to explore UBER data
#
# Author: Vivek Katial
# Created 2019-01-30 20:32:44
###############################################################################
server <- function(input, output) {
# Basic Numbers Page --------------------------------------------------------------
# Number of trips text in UI
output$num_trips <- renderText({
d_clean %>%
filter(trip_or_order_status == "COMPLETED") %>%
nrow()
})
# Number of kilometers
n_distance <- reactive({
d_clean %>%
pull(distance_miles) %>%
sum() * 1.6
})
# Number of kilometers in UI
output$num_distance <- renderText({
d_clean %>%
pull(distance_miles) %>%
sum(na.rm = T) * 1.6
})
output$longest_trip_distance_text <- renderText({
d_clean %>%
pull(distance_miles) %>%
max(na.rm = T) * 1.6
})
# Number of hours spent calculation
d_duration <- reactive({
d_clean %>%
mutate(duration = dropoff_time - begin_trip_time) %>%
select(duration) %>%
filter(duration > 0) %>%
pull(duration)
})
# Number of hours in UI
output$num_hours <- renderText({
d_duration() %>%
sum() %>%
as.numeric()/3600 %>%
round(., 1)
})
# Longest trip time
output$longest_trip_time_text <- renderText({
d_duration() %>%
max(na.rm = T) %>%
as.numeric()/60 %>%
round(., 1)
})
# Calculation for number of USD spent
n_dollars_spent <- reactive({
d_clean %>%
left_join(d_currency_rates, by = "fare_currency") %>%
mutate(usd_spent = fare_amount * rate) %>%
select(usd_spent) %>%
pull(usd_spent) %>%
sum(na.rm = T) %>%
round()
})
# Number of USD spent
output$num_dollars_spent <- renderText({
n_dollars_spent()
})
most_expensive_trip <- reactive({
d_clean %>%
left_join(d_currency_rates, by = "fare_currency") %>%
mutate(usd_spent = fare_amount * rate) %>%
filter(usd_spent == max(usd_spent, na.rm = T))
})
output$most_expensive_trip_text <- renderText({
most_expensive_trip() %>%
pull(usd_spent)
})
# Create Map Plot ---------------------------------------------------------
points_full <- reactive({
# Clean trip data
d_show <- d_routes %>%
filter(city == input$city)
# Check if any trips present otherwise return NULL
if (nrow(d_show) > 0) {
# store in DF
d_show <- d_show %>% unnest(route)
# Convert to SP obj
split_data = lapply(
unique(d_show$trip),
function(x) {
df = as.matrix(d_show[d_show$trip == x, c("lon", "lat")])
lns = Lines(Line(df), ID = x)
return(lns)
}
)
# Convert to SP lines so it can be plotted
data_lines = SpatialLines(split_data)
} else {
NULL
}
})
points <- reactive({
# Clean trip data
d_show <- d_routes %>%
filter(city == input$city) %>%
filter(request_time <= (input$time))
# Check if any trips present otherwise return NULL
if (nrow(d_show) > 0) {
# store in DF
d_show <- d_show %>% unnest(route)
# Convert to SP obj
split_data = lapply(
unique(d_show$trip),
function(x) {
df = as.matrix(d_show[d_show$trip == x, c("lon", "lat")])
lns = Lines(Line(df), ID = x)
return(lns)
}
)
# Convert to SP lines so it can be plotted
data_lines = SpatialLines(split_data)
} else {
NULL
}
})
output$map <- renderLeaflet({
# Base map
leaflet(points_full()) %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels)
})
observe({
req(!is.null(points()))
# create the map
leafletProxy("map", data = points()) %>%
clearShapes() %>%
addPolylines(weight = 1, color = "violet") %>%
fitBounds(
points_full()@bbox[1],
points_full()@bbox[2],
points_full()@bbox[3],
points_full()@bbox[4]
)
})
}