-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShinyApp.R
More file actions
321 lines (278 loc) · 12.7 KB
/
ShinyApp.R
File metadata and controls
321 lines (278 loc) · 12.7 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#' ####################################################################### #
#' PROJECT: [Biodiversity Simplification & Ecological Network Topology]
#' CONTENTS:
#' - Network Visualisations in Shiny App
#' DEPENDENCIES:
#' - [Executed] 3 - Visualisation.R
#' AUTHOR: [Erik Kusch]
#' ####################################################################### #
library(shiny)
library(shinythemes)
library(shinyWidgets)
library(ggplot2)
library(sf)
library(cowplot)
## CRAN -------------------------------------------------------------------
install.load.package <- function(x) {
if (!require(x, character.only = TRUE))
install.packages(x, repos='http://cran.us.r-project.org')
require(x, character.only = TRUE)
}
package_vec <- c(
"devtools", # needed for non-cran packages further down
"rgeos", # for loading shapefiles
"tidyverse", # for data handling
"rgbif", # for occurrence retrieval
"pbapply", # for apply with progress bar
"data.table", # for data handling
"rnaturalearth", # for landmask in projection kriging
"rnaturalearthdata", # for landmask in projection kriging
"rredlist", # for IUCN risk retrieval
"ConR", # for computation of IUCN risks
"CoordinateCleaner", # for additional occurrence cleaning
"igraph", # for graph operations
"FD", # for gower distance of trait data
"reshape2", # for making network matrices into plottable data frames via melt()
"bipartite", # for bipartite network analyses
"leaflet", # for html map products to investigate networks separately
"leafpop", # for graph popups in leaflet output
"cowplot", # for arranging of plots
"gridExtra", # for table grobs as legends in plots
"dplyr" # for data cleaning
)
sapply(package_vec, install.load.package)
# PREAMBLE ==================================================================
# source("3 - Visualisation.R")
# source("0 - Preamble.R")
load(file = "Shiny.RData")
# SHINY APP =================================================================
## User Interface -----------------------------------------------------------
ui <- fluidPage(theme = shinytheme("slate"),
navbarPage(
# theme = "cerulean", # <--- To use a theme, uncomment this
"Biodiversity Simplification & Ecological Network Topology",
tabPanel("Network Measures",
mainPanel(
# h1("Filtering Criteria"),
# fluidRow(
# column(3,
# hr(),
# pickerInput("Holding", "Holding", choices = Locations_sp$Holding,
# options = list(`actions-box` = TRUE),
# multiple = TRUE, selected = NULL)
# ),
# column(3,
# hr(),
# pickerInput("Species", "Species", choices = unique(Data_df$Species),
# options = list(`actions-box` = TRUE),
# multiple = TRUE, selected = NULL)
# ),
# column(3,
# hr(),
# pickerInput("Location", "Location", choices = Locations_sp$ID,
# options = list(`actions-box` = TRUE),
# multiple = TRUE, selected = NULL),
# ),
# ),
h1("Map Selection"),
leafletOutput("map", height = 700),
# absolutePanel(top = 10, right = 10,
# pickerInput("countries", label = "Select a Country:",
# choices = list("All countries",
# unique(leaflet_df$locality)
# ),
# options = list(
# `live-search` = TRUE)
# )
# ),
plotOutput("Plot_Net"),
# uiOutput("plot.ui")
), # mainPanel
sidebarPanel(
tags$h1("Network Data"),
textOutput("nObs"),
tags$h2(textOutput("Study")),
tableOutput("NetTable"),
# fluidRow(verbatimTextOutput("map_marker_click")),
tags$h2("Modularity"),
plotOutput("Plot_Mod"),
br(),
tags$h2("Nestedness"),
plotOutput("Plot_Nest")
), # sidebarPanel
), # Data Explorer Panel
tabPanel("Species Abbreviations",
fluidRow(
column(width = 5,
tags$h1("Animals"),
tableOutput("Tab_Animals")
),
column(width = 5,
tags$h1("Plants"),
tableOutput("Tab_Plants")
),
)
),
tabPanel("The Team", "This panel is intentionally left blank")
) # navbarPage
) # fluidPage
## Server Function ----------------------------------------------------------
server <- function(input, output) {
### Behind the Scenes Data Manipulation ####
#### Filtering Data ----
## this shiny function subsets the raw data according to user-specific data filters, currently not implemented
Data.Subset <- reactive({
SubsetData <- leaflet_df
return(SubsetData)
})
## retaining only plots queried by user
Plots.Subset <- reactive({
SubsetPlots <- Plots_ls
SubsetPlots <- SubsetPlots[which(names(SubsetPlots) %in% Data.Subset()$net.id)]
return(SubsetPlots)
})
### Output for Shiny ####
#### Main Panel ----
output$map <- renderLeaflet({
plot_df <- Data.Subset()
netpaths <- file.path(Dir.E.LeafletImages, paste0(plot_df$net.id, ".png"))
label_ls <- lapply(paste( "<b> Study ID: </b>" , plot_df$study.id, "<br>",
"Network ID:" , plot_df$net.id, "<br>"
, "Weighted Nestedness", plot_df$Nestedness, "<br>"
, "Modularity", plot_df$Modularity, "<br>",
"Robustness to animal extinction", plot_df$RobustnessAnimal, "<br>"
, "Robustness to plant extinction", plot_df$RobustnessPlant),
htmltools::HTML)
Map <- leaflet() %>%
# Base groups
addTiles() %>%
# Overlay groups
addCircleMarkers(data = plot_df, ~longitude, ~latitude,
group = "net.id",
label = label_ls
)
# %>%
# addPopupImages(netpaths, group = "net.id", width = 600, maxWidth = 600, height = 300)
# addPopupGraphs(Plots.Subset(), group = "net.id", width = 800, maxWidth = 800, height = 400)
return(Map)
})
#### Reactive Clicks ----
clickedMarker <- reactiveVal()
clickedMap <- reactiveVal()
## on click of markers in leaflet map
observeEvent(input$map_marker_click,{
# subsetting data
event <- input$map_marker_click
extract_df <- Data.Subset()[Data.Subset()$latitude == event$lat & Data.Subset()$longitude == event$lng, ]
# adding newly coloured circle marker
proxy <- leafletProxy("map")
proxy <- proxy %>%
addCircleMarkers(group = as.character(extract_df$net.id),
lng=extract_df$longitude,
lat=extract_df$latitude,
color = "red")
# removing previous red marker if one is present
if(!is.null(clickedMarker())){
prox <- proxy %>%
clearGroup(as.character(clickedMarker()))
}
# saving objects
clickedMarker(extract_df$net.id)
proxy
})
observeEvent(input$map_click,{
proxy <- leafletProxy("map")
# removing previous red marker if one is present
if(!is.null(clickedMap())){
prox <- proxy %>%
clearGroup(as.character(clickedMap()))
}
clickedMap(clickedMarker())
clickedMarker("Empty")
})
#### Side Panel ----
output$nObs <- renderText(paste("Showing", nrow(Data.Subset()), "individual networks.")) # for reporting number of data points
# this is the visualisation of the aggregate PI in the sample with corresponding colouring
output$Plot_Mod <- renderPlot({
plot_df <- Data.Subset()
ggplot(plot_df, aes(x = Modularity)) +
geom_histogram(bins = 50) +
geom_density(size = 2) +
theme_bw() + labs(y = "Count") +
labs(title = "Pre-Extinction Modularity")
})
output$Plot_Nest <- renderPlot({
plot_df <- Data.Subset()
ggplot(plot_df, aes(x = Nestedness)) +
geom_histogram(bins = 50) +
geom_density(size = 2) +
theme_bw() + labs(y = "Count") +
labs(title = "Pre-Extinction Nestedness")
})
output$Plot_Net <- renderPlot({ggplot() + theme_void()})
output$Tab_Animals <- renderTable(AnimalsLegend)
output$Tab_Plants <- renderTable(PlantsLegend)
output$NetTable <- renderTable(
data.frame(Modularity = mean(Data.Subset()$Modularity),
Nestedness = mean(Data.Subset()$Nestedness))
)
observeEvent(clickedMap(), {
event <- clickedMap()
print(event)
if(event != "Empty"){
extract_df <- Data.Subset()[Data.Subset()$net.id %in% event, ]
output$Study <- renderText(paste(unique(extract_df$study.id), collapse = " & "))
output$NetTable <- renderTable(
data.frame(Modularity = extract_df$Modularity,
Nestedness = extract_df$Nestedness)
)
output$Plot_Nest <- renderPlot({
plot_df <- Data.Subset()
ggplot(plot_df, aes(x = Nestedness)) +
geom_histogram(bins = 50) +
geom_density(size = 2) +
geom_vline(xintercept = extract_df$Nestedness, size = 1.5, col = "red") +
theme_bw() + labs(y = "Count") +
labs(title = "Pre-Extinction Nestedness")
})
output$Plot_Mod <- renderPlot({
plot_df <- Data.Subset()
ggplot(plot_df, aes(x = Modularity)) +
geom_histogram(bins = 50) +
geom_density(size = 2) +
geom_vline(xintercept = extract_df$Modularity, size = 1.5, col = "red") +
theme_bw() + labs(y = "Count") +
labs(title = "Pre-Extinction Modularity")
})
output$Plot_Net <- renderPlot({
plot_grid(plotlist = Plots.Subset()[names(Plots.Subset()) %in% extract_df$net.id], ncol = 1)
}, height = nrow(extract_df)*400)
}else{
output$Study <- renderText("")
output$NetTable <- renderTable(
data.frame(Modularity = mean(Data.Subset()$Modularity),
Nestedness = mean(Data.Subset()$Nestedness))
)
output$Plot_Mod <- renderPlot({
plot_df <- Data.Subset()
ggplot(plot_df, aes(x = Modularity)) +
geom_histogram(bins = 50) +
geom_density(size = 2) +
theme_bw() + labs(y = "Count") +
labs(title = "Pre-Extinction Modularity")
})
output$Plot_Nest <- renderPlot({
plot_df <- Data.Subset()
ggplot(plot_df, aes(x = Nestedness)) +
geom_histogram(bins = 50) +
geom_density(size = 2) +
theme_bw() + labs(y = "Count") +
labs(title = "Pre-Extinction Nestedness")
})
output$Plot_Net <- renderPlot({ggplot() + theme_void()})
}
# output$plot.ui <- renderUI({plotOutput("Plot_Net", height = 300*nrow(extract_df))})
})
} # server
## Shiny Launch -------------------------------------------------------------
shinyApp(ui = ui, server = server)