This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
228 lines (201 loc) · 7.36 KB
/
server.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
# Parameters & data
source('./code/param.R')
server = function(input, output, session) {
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PARAMETERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
sel <- reactive(input$layersTable) # Selected drivers
nSel <- reactive(length(sel())) # Number of selected drivers
type <- reactive(input$dataType) # Selected type of data (footprint vs hotspot)
trans <- reactive(input$rawData) # Selected type of data (raw vs transformed data)
id <- reactive(which(driversList$FileName %in% sel())) # ID of selected drivers in data table
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FUNCTIONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Loading raster ----------------------------------------------------
ras <- reactive({
# Select proper layer depending on user selection
if (length(sel()) == 0) {
raster0
} else if (length(sel()) == 1 && trans() == 'rawData') {
if(type() == 'footprint') {
rawDrivers[[sel()]]
} else {
hotspots[[sel()]]
} } else if (length(sel()) == 1 && trans() == 'transformed') {
if(type() == 'footprint') {
drivers[[sel()]]
} else {
hotspots[[sel()]]
}
} else if (length(sel()) > 1 && trans() == 'rawData') {
raster0
} else if (length(sel()) > 1 && trans() == 'transformed') {
if(type() == 'footprint') {
sum(drivers[[sel()]], na.rm = T) %>%
calc(function(x) ifelse(x == 0, NA, x))
} else {
sum(hotspots[[sel()]], na.rm = T) %>%
calc(function(x) ifelse(x == 0, NA, x))
}
}
# crs = "+init=epsg:3857")
})
# Raster values ----------------------------------------------------
vals <- reactive({
if(nSel() > 0 && trans() == 'transformed') {
val <- ras() %>%
maxValue() %>%
ceiling() %>%
seq(0, ., by = ./10)
} else if(nSel() == 1 && trans() == 'rawData') {
val <- ras() %>%
maxValue() %>%
ceiling() %>%
seq(0, ., by = ./10)
} else {
val <- seq(0, 1, by = .1)
}
})
# Colors ----------------------------------------------------
couleurs <- reactive({
colorBin(
palette = colorRampPalette(cols)(11),
domain = values(ras()),
bin = vals(),
na.color = "transparent"
)
})
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ INTERACTIVE MAP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Leaflet
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles(provider = providers$CartoDB.Positron) %>%
setView(lng = -65, lat = 48.50, zoom = 6) %>%
addEasyButton(
easyButton(icon = "fa-globe",
title = "Zoom to Level 1",
onClick = JS("function(btn, map){ map.setZoom(6); }")))
})
# Update map
observe({
# Map
leafletProxy(mapId = "map") %>%
clearShapes() %>%
clearControls() %>%
clearImages() %>%
addRasterImage(x = ras(),
colors = couleurs(),
opacity = .75,
project = FALSE) #%>%
# setView(lng = -65, lat = 48.50, zoom = 6)
# Legend
leafletProxy(mapId = "map") %>%
addLegend(position = "bottomright",
pal = couleurs(),
values = vals(),
title = "Map legend",
opacity = 1,
className = "info legend")
})
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ACTION BUTTONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Action button to reset choices
observe({
if (input$Uncheck > 0) {
updateCheckboxGroupInput(session = session,
inputId = 'layersTable',
label = '',
choiceNames = `layers$Drivers`,
choiceValues = layers$FileName)
}
})
# Action button to select all choices
observe({
if (input$CheckAll > 0) {
updateCheckboxGroupInput(session = session,
inputId = 'layersTable',
label = '',
selected = layers$FileName,
choiceNames = layers$Drivers,
choiceValues = layers$FileName)
}
})
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CONDITIONAL PANELS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~ DATA DESCRIPTION ~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~ PLATFORM ~~~~~~~~~~~~~~~ #
output$eDrivers <- renderUI({
HTML(eDrivers)
})
# ~~~~~~~~~~~~ SINGLE DRIVER ~~~~~~~~~~~~~ #
# To make reactivity better, but this is definitely not the most efficient way to do it!
# Need to figure out a better way at some point!
# Reactive object for data description
dataDescReactive <- reactive({
if (nSel() == 1) HTML(dataDesc(id()))
})
# Reactive object for data overview
dataOverReactive <- reactive({
if (nSel() == 1) HTML(dataOver(id()))
})
# Render data description
output$dataDescription <- renderUI(
dataDescReactive()
)
# Render data overview
output$dataOverview <- renderUI(
dataOverReactive()
)
# ~~~~~~~~~~~~~ MULTIPLE DRIVERS ~~~~~~~~~~~~~ #
output$multiDescription <- renderUI({
HTML(multiDesc(type()))
})
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~ WARNING MESSAGE ~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
output$warningMSG <- renderUI({
# ~~~~~~~~~~~~~~~ PARAMETERS ~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~ MESSAGE ~~~~~~~~~~~~~~~~~ #
if(nSel() > 1 && trans() == 'rawData') {
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
HTML({
paste(
'<div style="background-color:#4e4e4eCC;
background: radial-gradient(#4e4e4e, #4e4e4e66);
margin-left: -5%;
margin-right: -5%;
padding-top:25%;
padding-bottom:25%;
padding-left:35%;
padding-right:35%;
text-align:center;">',
'<h6>Driver data must be transformed to allow cumulative intensity visualization<h6/></div>'
)
})
}
})
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PLOTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ~~~~~~~~~~~~~ SINGLE DRIVER ~~~~~~~~~~~~~~ #
output$singlePlot <- renderPlot({
if (nSel() == 1) {
if (type() == 'footprint') histDriver(ras())
if (type() == 'hotspots') histHotspot(ras())
}
})
# ~~~~~~~~~~~~ MULTIPLE DRIVERS ~~~~~~~~~~~~ #
output$multiPlot <- renderPlot({
if (nSel() > 1) {
if (type() == 'footprint' && trans() == 'transformed') cumulIntensity(sel(), ras())
if (type() == 'hotspots' && trans() == 'transformed') cumulHotspots(sel(), ras())
}
})
} # End