-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
184 lines (163 loc) · 4.92 KB
/
app.R
File metadata and controls
184 lines (163 loc) · 4.92 KB
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
# Real-Time Bus Tracking System - Surat, Gujarat
library(shiny)
library(googleway)
library(dplyr)
library(shinydashboard)
# Load API key and bus route data
source("api_key.R")
bus_routes <- read.csv("bus_routes.csv", stringsAsFactors = FALSE)
# Filter for Route 1, Bus 101
bus_route <- bus_routes %>%
filter(route_id == 1, bus_id == 101) %>%
arrange(sequence)
# UI Definition
ui <- dashboardPage(
dashboardHeader(title = "Real-Time Bus Tracking - Surat, Gujarat"),
dashboardSidebar(
sidebarMenu(
menuItem("Map View", tabName = "map", icon = icon("map")),
menuItem("Route Info", tabName = "info", icon = icon("info-circle")),
actionButton("start_button", "Start Bus", icon = icon("play")),
actionButton("stop_button", "Stop Bus", icon = icon("stop")),
tags$div(
style = "padding: 10px;",
tags$p(strong("Source: "), bus_route$stop_name[1]),
tags$p(strong("Destination: "), bus_route$stop_name[nrow(bus_route)])
)
)
),
dashboardBody(
tabItems(
tabItem(tabName = "map",
fluidRow(
box(
width = 12,
google_mapOutput("map", height = "600px")
)
),
fluidRow(
box(
width = 6,
title = "Bus Information",
verbatimTextOutput("bus_info")
),
box(
width = 6,
title = "Route Information",
verbatimTextOutput("route_info")
)
)
),
tabItem(tabName = "info",
fluidRow(
box(
width = 6,
title = "Instructions",
tags$p("This app shows a bus moving along a route in Surat, Gujarat."),
tags$p("The bus updates its position every 2 seconds."),
tags$p("Click 'Start Bus' to begin the simulation."),
tags$p("The red marker represents the bus's current location."),
tags$p("The blue line shows the complete route.")
)
)
)
)
)
)
# Server Logic
server <- function(input, output, session) {
# Initialize reactive values
rv <- reactiveValues(
position_index = 1,
timer_active = FALSE,
last_update = Sys.time(),
initialized = FALSE
)
# Initialize map
output$map <- renderGoogle_map({
google_map(
key = api_key,
location = c(bus_route$latitude[1], bus_route$longitude[1]),
zoom = 14,
map_type_control = TRUE
)
})
# Add route polyline
observe({
if (!rv$initialized) {
google_map_update(map_id = "map") %>%
add_polylines(
data = bus_route,
lat = "latitude",
lon = "longitude",
stroke_colour = "#0000FF",
stroke_weight = 5,
stroke_opacity = 0.7
)
rv$initialized <- TRUE
}
})
# Display bus information
output$bus_info <- renderText({
if(rv$timer_active) {
invalidateLater(100)
}
current_pos <- bus_route[rv$position_index, ]
paste(
"Bus ID: 101",
paste("Current Stop:", current_pos$stop_name),
paste("Latitude:", current_pos$latitude),
paste("Longitude:", current_pos$longitude),
paste("Sequence:", current_pos$sequence),
paste("Last Updated:", format(rv$last_update, "%H:%M:%S")),
sep = "\n"
)
})
# Update bus position marker
observe({
if(rv$timer_active) {
invalidateLater(100)
current_time <- Sys.time()
time_diff <- as.numeric(difftime(current_time, rv$last_update, units = "secs"))
if(time_diff >= 2) {
rv$position_index <- (rv$position_index %% nrow(bus_route)) + 1
rv$last_update <- current_time
}
}
current_pos <- bus_route[rv$position_index, ]
google_map_update(map_id = "map") %>%
clear_markers() %>%
add_markers(
data = current_pos,
lat = "latitude",
lon = "longitude",
marker_icon = "https://maps.google.com/mapfiles/ms/icons/red-dot.png",
info_window = paste(
"<b>Bus ID:</b> 101<br>",
"<b>Stop:</b> ", current_pos$stop_name, "<br>",
"<b>Sequence:</b> ", current_pos$sequence
)
)
})
# Start button handler
observeEvent(input$start_button, {
rv$timer_active <- TRUE
rv$last_update <- Sys.time()
})
# Stop button handler
observeEvent(input$stop_button, {
rv$timer_active <- FALSE
})
# Display route information
output$route_info <- renderText({
paste(
"Route ID:", bus_route$route_id[1], "\n",
"Bus ID:", bus_route$bus_id[1], "\n",
"Starting Point:", bus_route$stop_name[1], "\n",
"Ending Point:", bus_route$stop_name[nrow(bus_route)], "\n",
"Total Stops:", nrow(bus_route)
)
})
}
# Run application
shinyApp(ui, server)