Repository navigation
Expand file tree
/
Copy pathapp.R
More file actions
159 lines (116 loc) · 5.48 KB
/
Copy pathapp.R
File metadata and controls
159 lines (116 loc) · 5.48 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
## app.R ##
## Load packages
library(shiny)
library(shinydashboard)
library(DT)
library(ggplot2)
library(dplyr)
library(readr)
library(leaflet)
marine <- "ships.csv"
mdata <- read_csv(marine, col_types = cols())
# Count all ship types
all_count <- mdata %>% select(ship_type) %>% count(ship_type, sort = TRUE) %>% group_by(ship_type)
# Sampling of data
set.seed(1375)
mysample <- sample_n(mdata, size = 60)
# Sample ship_type count
type_count <- mysample %>% select(ship_type) %>% count(ship_type, sort = TRUE) %>% group_by(ship_type)
# Map data
mysample <- mysample %>% mutate(DISTANCE = SPEED * ELAPSED)
map_data <- mysample %>% filter(!is.na(LAT), !is.na(LON), !is.na(COURSE), !is.na(HEADING),
!is.na(SHIPNAME), !is.na(DATETIME), !is.na(ship_type),
!is.na(DISTANCE)) %>%
select(LAT, LON, COURSE, HEADING, SHIPNAME, DATETIME, ship_type, DISTANCE)
#Define ui
ui <- dashboardPage(
dashboardHeader(title = "Maritime dashboard"),
dashboardSidebar(
sidebarMenu(
menuItem("Visuals", tabName = "visuals", icon = icon("chart-bar")),
menuItem("Geospatial", tabName = "geospatial", icon = icon("digital-ocean"))
)
),
dashboardBody(
tabItems(
# First tab
tabItem(tabName = "visuals",
# Boxes for graphics
fluidRow(
box(
title = "Chart", background = "maroon", solidHeader = TRUE,
plotOutput("plot", height = 300)),
box(width = 5, background = "aqua", solidHeader = TRUE,
title = "Data choice",
selectInput("data", "", choices = c("type_plot", "all_plot"), selected =
"type_plot")
)
),
dataTableOutput("MaritimeTable")
),
# Second tab
tabItem(tabName = "geospatial",
h2("Maritime graph"),
# Boxes for list menu
fluidRow(
column(10,
box(
title = "Name of ship", background = "olive", solidHeader = TRUE,
selectInput(inputId = "ship_name",
label = "Choose ship",
selected = NULL,
choices = unique(mysample$SHIPNAME)),
),
8000
column(12,
box(width = 10,
title = "Type of ship", background = "navy", solidHeader = TRUE,
selectInput(inputId = "shipType",
label = "Choose type",
selected = "All",
choices = c("All", "Cargo", "Tanker", "Tug",
"Unspecified", "Fishing", "Pleasure", "Passenger"))),
),
#uiOutput("ship"),
)
),
fluidRow(
box(width = 9, background = "fuchsia", solidHeader = TRUE,
leafletOutput("map", width = "100%")
)
),
dataTableOutput("GeospatialTable")
)
)
)
)
server <- function(input, output){
output$plot <- renderPlot({
if(input$data == "type_plot"){
# Plot sample ship_type
bplot <- ggplot(type_count, aes(ship_type, n, fill = ship_type)) + geom_col() +
geom_text(aes(label = n, vjust = -0.5)) +
theme_minimal() +
theme(legend.position = "none", axis.title.y = element_text(angle = 0)) +
labs(title = "Top vessel types")
}
if(input$data == "all_plot"){
# All types plot
bplot <- ggplot(all_count, aes(ship_type, n, fill = ship_type)) + geom_col() +
geom_text(aes(label = n, vjust = -0.5)) +
theme_minimal() +
theme(legend.position = "none", axis.title.y = element_text(angle = 0)) +
labs(title = "All vessel types")
}
bplot
})
output$MaritimeTable <- renderDataTable(mysample)
output$map <- renderLeaflet({
map_labels <- paste0(sep = "<br/>", "Name of ship:", map_data$SHIPNAME,
"<br/>", "Ship type:", map_data$ship_type,
"<br/>", "Distance:", map_data$DISTANCE)
map <- leaflet(map_data, height = 2000, width = 2000) %>% addTiles() %>%
addMarkers(~ LON, ~ LAT, clusterOptions = markerClusterOptions(), popup = ~ map_labels)
})
output$GeospatialTable <- renderDataTable(map_data)
}
shinyApp(ui, server)