-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
99 lines (86 loc) · 2.37 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
library(shiny)
library(semantic.dashboard)
library(ggplot2)
library(plotly)
library(DT)
library(leaflet)
data_loading <- function() {
ships <<- readRDS("ships_calculated.Rds")
choices_table <<- ships %>% dplyr::select(ship_type, SHIP_ID)
}
data_loading()
server <- shinyServer(function(input, output, session) {
observe({
updateSelectInput(session,
inputId = "vessel",
choices = unique(choices_table[choices_table$ship_type == input$type, "SHIP_ID"]))
})
get_vessel_data <- function() {
ships %>% filter(SHIP_ID == input$vessel) ->> vessel
}
output$distance <- renderText({
get_vessel_data()
paste("The vessel travelled", vessel$dist, "meters")
})
output$dates <- renderText({
get_vessel_data()
paste("The vessel travelled between",
vessel$prevDATE,
"and",
vessel$DATETIME)
})
output$map_all <- renderLeaflet({
map <- data.frame(
lat = c(ships$prevLAT, ships$LAT),
long = c(ships$prevLON, ships$prevLON)
)
leaflet() %>%
addTiles() %>%
addPolylines(data = map,
lng = ~ long,
lat = ~ lat)
})
output$map <- renderLeaflet({
get_vessel_data()
map <- data.frame(
lat = c(vessel$prevLAT, vessel$LAT),
long = c(vessel$prevLON, vessel$prevLON)
)
leaflet() %>%
addTiles() %>%
addPolylines(data = map,
lng = ~ long,
lat = ~ lat)
})
output$top_vessels <- renderPlotly({
top_ships <- ships %>% arrange(desc(dist)) %>% head(10)
top_ships$dist_km <- round(top_ships$dist / 1000)
ggplotly(
ggplot(top_ships, mapping = aes(
x = reorder(SHIP_ID, dist_km),
y = dist_km,
fill = dist_km
)) +
geom_bar(stat = "identity") +
xlab("Vessel ID") +
ylab("Distance (km)") +
coord_flip()
)
})
output$top_types <- renderPlotly({
av_dist <- ships %>% group_by(ship_type) %>%
summarise(dist_km = round(mean(dist)))
av_dist$dist_km <- round(av_dist$dist_km / 1000)
ggplotly(
ggplot(av_dist, mapping = aes(
x = reorder(ship_type, dist_km),
y = dist_km,
fill = dist_km
)) +
geom_bar(stat = "identity") +
xlab("Vessel type") +
ylab("Distance (km)") +
coord_flip()
)
})
})