/
server.R
93 lines (78 loc) · 3.09 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
library(reshape2)
library(dplyr)
library(magrittr)
library(saves)
library(shiny)
library(googleVis)
library(leaflet)
library(dplyr)
library(curl) # make the jsonlite suggested dependency explicit
library(dygraphs)
library(xts)
#cars_weather <- read.csv("/Users/austintodd/Documents/Work/Projects/AIL/Code/cars_weather.csv",header=TRUE,sep=",")
#uniqueIDs <- unique(cars_weather$DeviceID)
shinyServer(function(input, output, session) {
observe({
updateSelectizeInput(session, 'trip_id',
choices = as.character(sort(unique(cars_weather[cars_weather$DeviceID==input$car_ind,'Trip']),decreasing=FALSE)),
selected= as.character(sort(unique(cars_weather[cars_weather$DeviceID==input$car_ind,'Trip']),decreasing=FALSE)[1]))
})
tripdata <- reactive({
if ( is.null(input$trip_id))
return()
cars_weather[(cars_weather$DeviceID==input$car_ind) & (cars_weather$Trip == input$trip_id), ]
})
output$busmap <- renderLeaflet({
colors <- tripdata()$GPS_Speed/max(cars_weather$GPS_Speed)
lons <- tripdata()$GPS_Longitude
lats <- tripdata()$GPS_Latitude
startIcon <- makeIcon(
iconUrl = "carGreen.svg", iconWidth = 18, iconHeight =24
)
stopIcon <- makeIcon(
iconUrl = "carRed.svg", iconWidth = 18, iconHeight =24
)
leaflet() %>%
addTiles('http://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}.png') %>%
setView(lng = -83.75, lat = 42.28, zoom = 12) %>%
addPolylines(lons[seq(1,length(lons),10)],
lats[seq(1,length(lats),10)],
color= "blue",
weight=8,
opacity = 0.6) %>%
addMarkers(lons[1],lats[1],
icon = startIcon,
popup = paste('<b>Start Time</b><br>',as.character(as.POSIXct(as.POSIXlt(tripdata()$TimeUTC[1],tz="EST5EDT"),tz="GMT")))) %>%
addMarkers(lons[length(lons)],lats[length(lats)],
icon = stopIcon,
popup = paste('<b>End Time</b><br>',as.character(as.POSIXct(as.POSIXlt(tripdata()$TimeUTC[length(lons)],tz="EST5EDT"),tz="GMT"))))
})
#---------------------------------------------------
# Add functionality for plotting data using dygraph
#---------------------------------------------------
# Reactive data for updated time series
pltdata <- eventReactive(input$makeplot,{
t <- tripdata()[c('TimeUTC',input$plotvars)]
t[,1] <- as.POSIXct(as.POSIXlt(t[,1],tz="EST5EDT"))
xts(t[,-1],order.by=t[,1])
})
output$dygraph <- renderDygraph({
dygraph(pltdata()) %>%
dyHighlight(highlightCircleSize = 5) %>%
dyRangeSelector() %>%
dyLegend(width = 600) %>%
dyOptions(colors = RColorBrewer::brewer.pal(3, "Set2"))
})
output$from <- renderText({
if (!is.null(input$dygraph_date_window))
strftime(input$dygraph_date_window[[1]], "%H:%M:%S")
})
output$to <- renderText({
if (!is.null(input$dygraph_date_window))
strftime(input$dygraph_date_window[[2]], "%H:%M:%S")
})
output$plot_hoverinfo <- renderPrint({
cat("input$plot_hover:\n")
str(input$plot_hover)
})
})