-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
294 lines (239 loc) · 9.73 KB
/
app.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#' Visualisation task
#' This code contains a Shiny app to
#' visualise reptiles in ACT.
#' Created by Francisca Maron
#' Date: 07/12/2021
# Dependencies
library(shiny)
library(shinythemes)
library(shinyWidgets)
library(tidyverse)
library(leaflet)
library(sf)
library(plotly)
library(htmltools)
#Read the data
load("reptiles.RData")
ui <- bootstrapPage(
#Code for creating the navigation bar at the top of the web page
navbarPage(
windowTitle = "Reptiles",
theme = shinytheme("united"),
collapsible = TRUE,
title = "Reptiles of the Australian Capital Territory"),
#Tab with leaflet map
tabPanel(
"Reptile Occurences",
div(class = "outer", tags$head(includeCSS("style.css")),
leafletOutput("reptileLocation", width = "100%", height = "100%"),
#Sidebar with filters and plots
absolutePanel(id = "controls", #id for style.css
top = 75, left = 70, width = 400, height = "auto",
fixed = TRUE,
draggable = TRUE,
#Reactive text
h3(textOutput("numSpecies"), align = "left"),
h3(textOutput("occurrences"), align = "left"),
#Filter by species
#CSS for picker input text when nothing is selected
tags$style(".bs-placeholder {color: #FFFFFF !important;}"),
pickerInput(inputId = "selectSpecies",
label = h5("Select species"),
choices = unique(reptiles$scientificName),
multiple = TRUE,
options = pickerOptions(actionsBox = TRUE,
deselectAllText = "Deselect",
selectAllText = "Select all",
noneSelectedText= "Please select species")),
#Create tab set inside left panel
tabsetPanel(
tabPanel("Type of forest",plotlyOutput("forest")),
tabPanel("Data resource name", plotlyOutput("resource", height = "200%")),
tabPanel("Basis of record", plotlyOutput("record")),
tabPanel("Records by month", plotlyOutput("month"))
),
#GitHub logo to the repo
absolutePanel(id = "logo",
bottom = 40,
left = 60,
width = 50,
fixed=TRUE,
draggable = FALSE,
height = "auto",
tags$a(href = 'https://github.com/fmaron/ala_reptiles',
tags$img(src = 'github_logo.png', height = '50', width = '50')))
)
)
)
)
server <- function(input, output, session){
##### Lefleat map #####
#Reactive data, filters by species selected and converts to spatial
reactiveReptiles <- reactive({
req(input$selectSpecies)
if(!is.null(input$selectSpecies)){
reptiles %>%
filter(scientificName %in% input$selectSpecies) %>%
mutate(id = as.character(row_number()))%>%
st_as_sf(coords = c("decimalLongitude", "decimalLatitude"), remove = FALSE) %>%
st_sf(crs=4326)
}else(
return(NULL)
)
})
#Map text
mapText <- reactive(paste(
"Longitude: ", reactiveReptiles()$decimalLongitude, "<br/>",
"Latitude: ", reactiveReptiles()$decimalLatitude, "<br/>",
"Date: ", reactiveReptiles()$eventDate, "<br/>",
"Species: ", reactiveReptiles()$scientificName, "<br/>",
"Type of forest: ", reactiveReptiles()$forest2013, "<br/>",
"Data resource: ",reactiveReptiles()$dataResourceName, "<br/>",
"Basis of record: ",reactiveReptiles()$basisOfRecord,sep = "")%>%
lapply(htmltools::HTML))
#Base leaflet map
map <- reactive({
leaflet()%>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addScaleBar(position = "bottomright")%>%
setView(lng = 149, lat = -35.34, zoom = 10)
})
#Output leaflet
output$reptileLocation <- renderLeaflet({
map()
})
#Output leaflet, changes when user selects species
observeEvent(input$selectSpecies, {
proxy <- leafletProxy("reptileLocation")
if(!is.null(input$selectSpecies)){
proxy %>%
addCircleMarkers(data = reactiveReptiles(),
fillColor = "#F16648",
stroke = FALSE,
fillOpacity = .6,
radius = 8,
layerId = ~id,
label = mapText(),
group = "speciesPoints") %>%
flyToBounds(lng1 = min(reactiveReptiles()$decimalLongitude)- 0.3,
lng2 = max(reactiveReptiles()$decimalLongitude),
lat1 = min(reactiveReptiles()$decimalLatitude),
lat2 = max(reactiveReptiles()$decimalLatitude))
}else{
leafletProxy("reptileLocation")%>%
clearMarkers()%>%
clearShapes()%>%
removeMarker(layerId = reactiveReptiles()$id)
}
}, ignoreNULL = FALSE)
##### Plotly ####
#Forest
reactiveForest <- reactive({
req(input$selectSpecies)
if(!is.null(input$selectSpecies)){
reptiles %>%
filter(scientificName %in% input$selectSpecies) %>%
group_by(forest2013) %>%
summarise(numOccurrence = n(), .groups = "drop")
}else{
return(NULL) #This helps to return a null plot when nothing is selected
}
})
output$forest <- renderPlotly({
reactiveForest() %>%
highlight_key(key = ~forest2013)%>%
plot_ly(x = ~forest2013, y = ~numOccurrence, type = 'bar',
opacity = .6,
marker = list(color = "#F16648"),
hovertemplate = paste('%{x}', '<br>Occurrence: %{y}<br>','<extra></extra>')) %>%
layout(yaxis = list(title = 'Number of occurrences'),
xaxis = list(title='Type of forest in 2013'),
barmode = "overlay") %>%
highlight(on = "plotly_hover", off = "plotly_deselect", color = "#F16648")
})
#Resource name
reactiveResource <- reactive({
req(input$selectSpecies)
if(!is.null(input$selectSpecies)){
reptiles %>%
filter(scientificName %in% input$selectSpecies) %>%
group_by(dataResourceName) %>%
summarise(numOccurrence = n(), .groups = "drop")
}else{
return(NULL) #This helps to return a null plot when nothing is selected
}
})
output$resource <- renderPlotly({
reactiveResource() %>%
mutate(dataResourceName = str_trunc(dataResourceName, 25, "right"))%>%
highlight_key(key = ~dataResourceName)%>%
plot_ly(x = ~dataResourceName, y = ~numOccurrence,
type = 'bar',
opacity = .6,
marker = list(color = "#F16648"),
hovertemplate = paste('%{x}', '<br>Occurrence: %{y}<br>','<extra></extra>')) %>%
layout(yaxis = list(title = 'Number of occurrences'),
xaxis = list(title='Data resource name'),
barmode = "overlay") %>%
highlight(on = "plotly_hover", off = "plotly_deselect", color = "#F16648")
})
#Basis of record
reactiveRecord <- reactive({
req(input$selectSpecies)
if(!is.null(input$selectSpecies)){
reptiles %>%
filter(scientificName %in% input$selectSpecies) %>%
group_by(basisOfRecord) %>%
summarise(numOccurrence = n(), .groups = "drop")
}else{
return(NULL) #This helps to return a null plot when nothing is selected
}
})
output$record <- renderPlotly({
reactiveRecord() %>%
highlight_key(key = ~basisOfRecord)%>%
plot_ly(x = ~basisOfRecord, y = ~numOccurrence, type = 'bar',
opacity = .6,
marker = list(color = "#F16648"),
hovertemplate = paste('%{x}', '<br>Occurrence: %{y}<br>','<extra></extra>')) %>%
layout(yaxis = list(title = 'Number of occurrences'),
xaxis = list(title='Basis of record'),
barmode = "overlay") %>%
highlight(on = "plotly_hover", off = "plotly_deselect", color = "#F16648")
})
#Monthly records
reactiveMonth <- reactive({
req(input$selectSpecies)
if(!is.null(input$selectSpecies)){
reptiles %>%
filter(scientificName %in% input$selectSpecies) %>%
group_by(month) %>%
summarise(numOccurrence = n(), .groups = "drop")
}else{
return(NULL) #This helps to return a null plot when nothing is selected
}
})
output$month <- renderPlotly({
suppressWarnings(
reactiveMonth() %>%
highlight_key(key = ~month)%>%
plot_ly(x = ~month, y = ~numOccurrence, type = 'bar',
opacity = .6,
marker = list(color = "#F16648"),
hovertemplate = paste('%{x}', '<br>Occurrence: %{y}<br>','<extra></extra>')) %>%
layout(yaxis = list(title = 'Number of occurrences'),
xaxis = list(title='Month'),
barmode = "overlay") %>%
highlight(on = "plotly_hover", off = "plotly_deselect", color = "#F16648")
)
})
##### Text output #####
output$numSpecies <- renderText({
paste0(prettyNum(length(input$selectSpecies)), " species selected")
})
output$occurrences <- renderText({
paste0(prettyNum(nrow(reactiveReptiles())), " occurrences")
})
}
shinyApp(ui,server)