-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
executable file
·285 lines (244 loc) · 16 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
# BioTIME App
# Description: Explores BioTIME datasets by mapping the global coverage of data sets, attributing contributors,
# and shows trends from Science 2014 paper.
# Author: Cher Chow
require(shiny)
require(shinyjs)
require(tidyverse)
require(leaflet)
require(sf)
# Data set up -------------------------------------------------------------
# SET WORKING DIRECTORY FIRST :)
# select the app_data.csv file. this sets the working directory to the src folder
# setwd('/Volumes/Cherbet/BioTIME/Dataset-Explorer/src')
# in server, working directory for server side defaults to the app folder
set.seed(24)
# BioTIME color functions
source('./src/scale_gg_biotime.R')
# load data
BT_datasets <- read.csv('./src/app_data.csv', header=T) # table for dataset metadata
load('./src/hex_studies.RData') # load the hex cell study extents
load('./src/circle_studies.RData') # load vector listing studies that are too small to plot extents
# Housekeeping after importing
BT_datasets$TAXA <- str_to_title(BT_datasets$TAXA) # fix mismatched title case for levels
BT_datasets$TAXA <- str_replace_all(BT_datasets$TAXA, 'All', 'Multiple')
BT_datasets$TAXA <- as.factor(BT_datasets$TAXA)
BT_datasets$REALM <- as.factor(BT_datasets$REALM)
BT_datasets$BIOME_MAP <- as.factor(BT_datasets$BIOME_MAP)
BT_datasets$CLIMATE <- as.factor(BT_datasets$CLIMATE)
# year inclusive
BT_datasets$DURATION <- BT_datasets$DURATION + 1
extents <- left_join(extents, BT_datasets, by='STUDY_ID')
wgs84 <- '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
merckm <- '+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs'
# transform the spatial data to fit the original BioTIME WGS datum
st_crs(extents) <- merckm # safety net for mismatched GDAL package versions
extents <- st_transform(extents, wgs84)
# User Interface (Frontend) ---------------------------------------------------------------
ui <- fluidPage(
tags$head(
# Style import
tags$link(rel = "stylesheet", type = "text/css", href = "style.css") # things on UI/client side are located in www folder
# all assets that have to be accessible for frontend like css or images have to be in the www folder
),
useShinyjs(),
div(id='mapwrap',
fluidRow(id='count',
div(class='stat', h1(textOutput('studies')), h4('studies')),
div(class='stat', h1(textOutput('years')), h4('years')),
div(class='stat', h1(textOutput('contributors')), h4('contributors')),
div(class='stat', h1(textOutput('taxa')), h4('taxa')),
div(class='stat', h1(textOutput('biome')), h4('biomes')),
div(class="warning", 'We recommend viewing our map on larger devices for the best experience')
),
# Dataset map
fluidRow(id='maprow',
leafletOutput("StudyMap"), #output map
absolutePanel(id='control', draggable=T, # control panel
h2('Filter datasets'),
tags$div(class="accordion",
tags$input(id='tog_realm', type='checkbox', class='accordion-toggle', name='toggle'),
tags$label(`for`='tog_realm', 'Realm'),
tags$section(actionButton("selectallR", label="Select/Deselect all", class='selectall'),
checkboxGroupInput('Realm', label=NULL,
choiceNames=levels(BT_datasets$REALM),
selected=levels(BT_datasets$REALM),
choiceValues=levels(BT_datasets$REALM)))),
tags$div(class="accordion",
tags$input(id='tog_taxa', type='checkbox', class='accordion-toggle', name='toggle'),
tags$label(`for`='tog_taxa', 'Taxa'),
tags$section(actionButton("selectallT", label="Select/Deselect all", class='selectall'),
checkboxGroupInput('Taxa', label=NULL,
choiceNames=levels(BT_datasets$TAXA),
selected=levels(BT_datasets$TAXA),
choiceValues=levels(BT_datasets$TAXA)))),
tags$div(class="accordion",
tags$input(id='tog_climate', type='checkbox', class='accordion-toggle', name='toggle'),
tags$label(`for`='tog_climate', 'Climate'),
tags$section(actionButton("selectallC", label="Select/Deselect all", class='selectall'),
checkboxGroupInput('Climate', label=NULL,
choiceNames=levels(BT_datasets$CLIMATE),
selected=levels(BT_datasets$CLIMATE),
choiceValues=levels(BT_datasets$CLIMATE)))),
tags$div(class="accordion",
tags$input(id='tog_dur', type='checkbox', class='accordion-toggle', name='toggle'),
tags$label(`for`='tog_dur', 'Duration (yrs)'),
tags$section(sliderInput('Duration', label=NULL,
min=2, max=130, value=c(2, 130),
round = T, ticks = F, animate = FALSE))),
checkboxInput(inputId = "newstudies", label = 'Show upcoming v2.0 studies', value=TRUE),
actionButton('reset', 'Reset'),
# manual marker legend
tags$div(class='studies-legend')
) # end control panel
) # end row
) # end wrapper
) # end UI
# Server -----------------------------------------------------------
# define all the under the hood computations that have to be done to show the map
server <- function(input, output) {
# reset button
observeEvent(input$reset, {
reset('control')
})
# select/deselect all buttons
observe({
if (input$selectallR > 0) {
if (input$selectallR %% 2 == 0){
updateCheckboxGroupInput(inputId="Realm",
choices = levels(BT_datasets$REALM),
selected = levels(BT_datasets$REALM))
} else {
updateCheckboxGroupInput(inputId="Realm",
choices = levels(BT_datasets$REALM),
selected = c())
}}
})
observe({
if (input$selectallT > 0) {
if (input$selectallT %% 2 == 0){
updateCheckboxGroupInput(inputId="Taxa",
choices = levels(BT_datasets$TAXA),
selected = levels(BT_datasets$TAXA))
} else {
updateCheckboxGroupInput(inputId="Taxa",
choices = levels(BT_datasets$TAXA),
selected = c())
}}
})
observe({
if (input$selectallC > 0) {
if (input$selectallC %% 2 == 0){
updateCheckboxGroupInput(inputId="Climate",
choices = levels(BT_datasets$CLIMATE),
selected = levels(BT_datasets$CLIMATE))
} else {
updateCheckboxGroupInput(inputId="Climate",
choices = levels(BT_datasets$CLIMATE),
selected = c())
}}
})
# Dataset map -------------------------------------------------------------
# dataframe with information but not split (tallies)
datasets <- reactive({
BT_datasets %>% filter(DURATION >= input$Duration[1] & DURATION <= input$Duration[2] & REALM %in% input$Realm & TAXA %in% input$Taxa & CLIMATE %in% input$Climate)
})
# make a working dataframe for large extent (hex cell) studies based on the filter options from the input
large.studies <- reactive({
extents %>% filter(DURATION >= input$Duration[1] & DURATION <= input$Duration[2] & REALM %in% input$Realm & TAXA %in% input$Taxa & CLIMATE %in% input$Climate)
})
# create a palette for the leaflet map to use
pal <- colorBin(palette=intPalette(biotime_palettes[['gradient']][2:5])(5),
domain=2:130,
bins=c(2,10,25,50,100,130))
# generate a reactive dataset summary statistic counter
output$studies <- renderText({datasets() %>% filter(STUDY_ID < 600) %>% pull(STUDY_ID) %>% n_distinct()})
output$contributors <- renderText({c(datasets() %>% pull(CONTACT_1), datasets() %>% pull(CONTACT_2)) %>% n_distinct()})
output$taxa <- renderText({datasets() %>% pull(TAXA) %>% n_distinct()})
output$biome <- renderText({datasets() %>% pull(BIOME_MAP) %>% n_distinct()})
output$years <- renderText({datasets() %>% pull(DURATION) %>% max()})
# url for custom BioTIME color basemap tiles
BioTIMEtile <- 'https://api.mapbox.com/styles/v1/biotime/ckpz9jzdk2u7117lm6fngw29j/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoiYmlvdGltZSIsImEiOiJja3B6OWhzaDkxbnloMndwOXhtYnM3cGtoIn0.MMtE79hfFUUDQxXOQHmqxg'
# draw the map
output$StudyMap <- renderLeaflet({
leaflet(options = leafletOptions(minZoom=2, maxZoom = 6, worldCopyJump=T)) %>%
setView(lng = runif(n=1, min = -90, max=90), lat = runif(n=1, min = -60, max=60), zoom = 3) %>% addEasyButton(easyButton(
icon="fa-globe", title="Zoom out to global view",
onClick=JS("function(btn, map){ map.setView(new L.LatLng(0, 0), 2); }"))) %>%
# manual legend
addControl(position='bottomright', html = "
<ul id='legend'>
<li><div id='legend-study' class='symbol'></div> Study </li>
<li><div id='legend-hex' class='symbol'></div> Large-extent study</li>
<li><div id='legend-cluster' class='symbol'>3</div> Study cluster</li>
<li><div id='legend-new' class='symbol'></div> <div id='legend-newhex' class='symbol'></div> Upcoming studies</li>
</ul>
") %>%
# addProviderTiles(providers$CartoDB.PositronNoLabels) %>% # uncomment if Mapbox stops working
addTiles(urlTemplate=BioTIMEtile) %>%
# public large extent studies by hex polygons we generated
addPolygons(data=large.studies() %>% filter(STUDY_ID < 2000),
fillOpacity=0.5, fillColor=~pal(DURATION), weight=1.4, color='#155f4966',
highlightOptions = highlightOptions(fillOpacity=0.9,fillColor='#cf7941', bringToFront = F),
popup = ~paste0("<h5>", TITLE,"</h5>",
'<h6>', CONTACT_1, ifelse(CONTACT_2 != '', ', ', ''), CONTACT_2,' et al. </h6>',
"<strong>Duration: </strong>",START_YEAR," to ",END_YEAR,"<br/>",
"<strong>Taxa: </strong>",TAXA, "<br/>",
"<strong>Biome: </strong>", BIOME_MAP,"<br/>",
"<a class='button' target='_parent' href='http://biotime.st-andrews.ac.uk/selectStudy.php?study=", STUDY_ID, "'>View full database record</a>")) %>%
# public small extent studies
addCircleMarkers(data=datasets() %>% filter(STUDY_ID < 2000 & STUDY_ID %in% sing.cell.studies), ~CENT_LONG, ~CENT_LAT, radius=8,
opacity=1, fillOpacity=1, fillColor=~pal(DURATION), weight=2, color='#155f49',
clusterOptions = markerClusterOptions(disableClusteringAtZoom = 4, spiderfyOnMaxZoom = F, # specify custom cluster thresholds with a javascript function
iconCreateFunction=JS("function (cluster) {
var childCount = cluster.getChildCount();
var c = ' marker-cluster-';
if (childCount > 30) {
c += 'large';
} else if (childCount > 10) {
c += 'medium';
} else {
c += 'small';
}
return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
}")),
popup = ~paste0("<h5>", TITLE,"</h5>",
'<h6>', CONTACT_1, ifelse(CONTACT_2 != '', ', ', ''), CONTACT_2,' et al. </h6>',
"<strong>Duration: </strong>",START_YEAR," to ",END_YEAR,"<br/>",
"<strong>Taxa: </strong>",TAXA, "<br/>",
"<strong>Biome: </strong>", BIOME_MAP, "<br/>",
"<a class='button' target='_parent' href='http://biotime.st-andrews.ac.uk/selectStudy.php?study=", STUDY_ID, "'>View full database record</a>")) %>%
addLegend(data=datasets(), position='bottomright', title='Duration', pal=pal, opacity=1,
values=~DURATION)
})
# add/take out new studies if option is toggled
observe({
if (input$newstudies == TRUE){
leafletProxy('StudyMap') %>%
addPolygons(data=large.studies() %>% filter(STUDY_ID > 2000), group='new',
fillOpacity=0.7, fillColor='#cf7941', weight=2, color='#cf7941',
highlightOptions = highlightOptions(fillOpacity=0.9,fillColor='#155f49', color='#155f49',bringToFront = F),
popup = ~paste0("<h5 style='color: #cf7941'>", TITLE,"</h5>",
"<strong style='color: #cf7941'>Coming soon in v2.0</strong>",
'<h6>', CONTACT_1, ifelse(CONTACT_2 != '', ', ', ''), CONTACT_2,' et al. </h6>',
"<strong>Duration: </strong>",START_YEAR," to ",END_YEAR,"<br/>",
"<strong>Taxa: </strong>",TAXA, "<br/>",
"<strong>Biome: </strong>", BIOME_MAP)) %>%
# coming soon new small extent studies
addCircleMarkers(data=datasets() %>% filter(STUDY_ID > 2000 & STUDY_ID %in% sing.cell.studies), group='new', ~CENT_LONG, ~CENT_LAT, radius=8,
opacity=1, fillOpacity=1, fillColor='#cf7941aa', weight=2, color='#cf7941',
popup = ~paste0("<h5 style='color: #cf7941'>", TITLE,"</h5>",
"<strong style='color: #cf7941'>Coming soon in v2.0</strong>",
'<h6>', CONTACT_1, ifelse(CONTACT_2 != '', ', ', ''), CONTACT_2,' et al. </h6>',
"<strong>Duration: </strong>",START_YEAR," to ",END_YEAR,"<br/>",
"<strong>Taxa: </strong>",TAXA, "<br/>",
"<strong>Biome: </strong>", BIOME_MAP))
}
if (input$newstudies == FALSE) {
leafletProxy('StudyMap') %>%
clearGroup('new')
}
})
} # end server
# Run the application
shinyApp(ui = ui, server = server)