-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shiny-App-For-Geolocation-Data.R
271 lines (207 loc) · 9.27 KB
/
Shiny-App-For-Geolocation-Data.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(leaflet)
library(shinydashboard)
library(RColorBrewer)
library(ggplot2)
library(tidyr)
library(dbplyr)
library(datasets)
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Upload data file", icon = icon("th"), tabName = "data", badgeColor = "green"),
menuItem("Data Table", icon = icon("th"), tabName = "dataTable", badgeColor = "green"),
menuItem("Leaflet Map", icon = icon("th"), tabName = "leaflet", badgeColor = "green")
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "data",
ui <- fluidPage(
# App title ----
titlePanel("Uploading Files"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput(inputId = "file1", "Choose file:",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Horizontal line ----
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
# Input: Select quotes ----
radioButtons("quote", "Quote",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"'),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Data file ----
tableOutput("contents")
)
)
)
),
tabItem(
tabName = "leaflet",
fluidPage(
leafletOutput(height = 800,"mymap"),
p()
)
),
tabItem(
tabName = "dataTable",
fluidPage(
titlePanel("Basic DataTable"),
#attempt to have conditional checkboxes for data:
#sidebarPanel(
#conditionalPanel(
# 'input.dataset === "df"',
# checkboxGroupInput("show_vars", "Columns to show:",
# names(df), selected = names(df)))
#),
mainPanel(
tabsetPanel(
id='dataset',
tabPanel("df", DT::dataTableOutput("mytable1"))
)
))
),
tabItem(
tabName = "anothertab",
fluidPage(
titlePanel("Telephones by region"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("region", "Region:",
choices=colnames(WorldPhones)),
hr(),
helpText("Data from AT&T (1961) The World's Telephones.")
),
# Create a spot for the barplot
mainPanel(
plotOutput("phonePlot")
)
)
)
)))
# Put them together into a dashboardPage
# Create the UI using the header, sidebar, and body
ui <- dashboardPage(dashboardHeader(title="App"),
sidebar = sidebar,
body = body)
# Define server logic required to draw a histogram
server <- function(input, output,session) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote,
)
#separate(df,col=`GPS location (click icon to view map & confirm)`,into=c("lat","long"), sep="|")
if(input$disp == "head") {
return(head(df))
}
else {
return(df)
}
})
#Leaflet map server side
#Leaflet map render output
output$mymap <- renderLeaflet({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote,
)
#function chooses icons based on vessel type
getIcon <- function(df) {
sapply(df$vessel, function(vessel) {
if(vessel == "Powerboat") {
"ship"
} else if(vessel == "ski") {
"fighter-jet"
}else if(vessel == "Paddler"){
"compass"
} else {
"anchor"
} })
}
#applies coloring based on if warning has been issued
getColor <- function(df) {
sapply(df$warning, function(warning) {
if(warning == "yes") {
"red"
} else if(warning == "no") {
"green"
} else {
"black"
} })
}
icons <- awesomeIcons(
icon = getIcon(df),
iconColor = 'black',
library = 'fa',
markerColor = getColor(df)
)
#trying to separate columns into latitude and longitude for use in leaflet map
#df <- sep = separate(df,col=df$`GPS location (click icon to view map & confirm)`,into=c("lat","long"), sep="|")
leaflet(df,options = leafletOptions(minZoom = 4)) %>%
addProviderTiles("OpenStreetMap")%>%
setView(lng = 172.6398470,lat = -43.525650, zoom = 6)%>%
addAwesomeMarkers(~longitude, ~latitude,icon=icons ,popup = paste("Vehicle: ", df$vessel, "<br>",
"No. People:", df$people, "<br>",
"Infringement:", df$warning, "<br>",
"Lat:", df$latitude, "<br>",
"Long:", df$longitude, "<br>",
"Form completed by:", df$Submitted, "<br>",
"Time:", df$Time))
})
# Filter data table based on selections
output$mytable1 <- DT::renderDataTable({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
#Was trying to implement checkboxes for displaying columns
#DT::datatable(df[, input$show_vars, drop = FALSE])
df
})
}
# Run the application
shinyApp(ui = ui, server = server)