-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
209 lines (176 loc) · 7.57 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
# Shiny Dashboard for SAPFLUXNET project
# by Sapfluxnet Team
# Shiny Dashboard for SAPFLUXNET project
# by Sapfluxnet Team
# Server logic
# This server logic is based in superzip shiny example
# (https://github.com/rstudio/shiny-examples/tree/master/063-superzip-example)
# and amerifluxR app (https://github.com/khufkens/amerifluxr) by Koen Hufkens.
### Libraries
library(shiny)
library(leaflet) # for maps
library(DT) # for datatables
library(ggplot2) # for plots
library(stringr) # for species names
# color palette for map & histogram legend
pal <- c("#D8B70A", "#02401B", "#A2A475", "#81A88D", "#972D15")
shinyServer(function(input, output) {
### Interactive map data
# Creating the map
output$preliminaryMap <- renderLeaflet({
leaflet() %>%
addTiles(urlTemplate = 'http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}',
attribution = 'Tiles © Esri — Esri, DeLorme, NAVTEQ',
options = tileOptions(noWrap = FALSE)) %>%
setView(lng = 60, lat = 10, zoom = 2) #%>%
# fitBounds(lng1 = -180, lng2 = 180, lat1 = -70, lat2 = 90) %>%
# addCircleMarkers(lng = ~longitude, lat = ~latitude, layerId = ~site_name)
})
# Observer to show colors and sizes of points according to the variables
# selected by the user
observe({
point_color <- input$color
# point_size <- input$size
# color
color_data <- preliminary_map_data[[point_color]]
palette <- colorFactor(pal[1:length(unique(preliminary_map_data[[point_color]]))],
color_data)
# size
# size <- preliminary_survey_fixed[[point_size]]
# add markers to map
leafletProxy('preliminaryMap', data = preliminary_map_data) %>%
clearMarkers() %>%
addCircleMarkers(lng = ~longitude, lat = ~latitude, layerId = ~site_name,
radius = 10,
fillColor = palette(color_data), fillOpacity = 0.8,
stroke = FALSE) %>%
addLegend('bottomright', pal = palette, values = color_data,
title = point_color, layerId = 'legend_color', opacity = 0.8)
})
# function to create the popups
site_popup <- function(site, lat, lng) {
selected_site <- preliminary_map_data[preliminary_map_data$site_name == site, ]
popup_text <- as.character(tagList(
tags$h4(selected_site$site_name),
tags$strong(selected_site$country), tags$br(),
sprintf('Sap flow method: %s', selected_site$sap_flow_method), tags$br(),
sprintf('Approximate number of trees per species: %s',
selected_site$aprox_numbers_tree_species), tags$br(),
sprintf('Approximate number of growing seasons: %s',
selected_site$aprox_years_growing_seasons), tags$br(),
sprintf('Meteorogical data available: %s',
selected_site$meteo_data_available), tags$br()
))
leafletProxy('preliminaryMap') %>%
addPopups(lng, lat, popup_text, layerId = site)
}
# Observer to show popups at click
observe({
leafletProxy('preliminaryMap') %>% clearPopups()
event <- input$preliminaryMap_marker_click
if (is.null(event)) {
return()
}
isolate({
site_popup(event$id, event$lat, event$lng)
})
})
# Histogram for color variable selected
# we need to play a little with the palette to achieve the
# same sequence in leaflet and in ggplot, as leaflet takes
# the colors not in order.
output$histogram <- renderPlot({
ggplot(preliminary_map_data,
aes_string(x = input$color, fill = input$color)) +
geom_bar(show.legend = FALSE) +
scale_fill_manual(
values = pal[1:length(unique(preliminary_map_data[[input$color]]))]
) +
labs(title = '', x = '', y = 'Number of sites') +
theme_minimal() +
theme(panel.background = element_blank(),
plot.background = element_blank(),
axis.text.x = element_blank(),
panel.grid = element_blank())
},
width = 200, height = 200, bg = 'transparent')
### Data table
output$preliminary_table <- renderDataTable({
datatable(table_data,
extensions = list('FixedColumns' = list(leftColumns = 3),
'Scroller' = NULL),
options = list(scrollX = TRUE, scrollCollapse = TRUE,
scrollY = 400, deferRender = TRUE,
pageLength = 250, dom = 'tf', autoWidth = TRUE,
columnDefs = list(list(width = '75px',
targets = c(1:11)),
list(class = 'dt-center',
targets = c(1:11))))
)
})
### Facts pane, valueboxes
output$N_sites <- renderValueBox({
valueBox(value = length(preliminary_table_data$site_name),
subtitle = 'sites willing to contribute',
icon = icon('leaf'),
color = 'olive')
})
output$N_wrong_coord <- renderValueBox({
valueBox(value = paste((sum(!preliminary_table_data$is_inside_country)/length(preliminary_table_data$is_inside_country))*100,
' %'),
subtitle = paste(sum(!preliminary_table_data$is_inside_country), ' sites with wrong coordinates'),
icon = icon('eye'),
color = 'red')
})
output$Meteo_data <- renderValueBox({
valueBox(value = paste((sum(preliminary_table_data$meteo_data_available == 'yes')/length(preliminary_table_data$meteo_data_available))*100,
' %'),
subtitle = paste(sum(preliminary_table_data$meteo_data_available == 'yes'), ' sites have environmental data'),
icon = icon('cloud'),
color = 'teal')
})
output$Countries <- renderValueBox({
valueBox(value = length(unique(preliminary_table_data$country)),
subtitle = 'different countries',
icon = icon('globe'),
color = 'purple')
})
output$Species <- renderValueBox({
valueBox(value = length(species_names_trim),
subtitle = 'different species (approx.)',
icon = icon('tree-deciduous', lib = "glyphicon"),
color = 'green')
})
# Site inspector pane
output$site_sps <- renderPrint({
site_sel <- preliminary_survey_fixed[preliminary_survey_fixed$site_name == input$site_input, ]
sps <- str_replace_all(site_sel$species, ',', ';')
sps <- unlist(str_split(sps, ';'))
sps <- str_c(sps, collapse = '\n')
cat(sps)
})
output$site_contr <- renderPrint({
site_sel <- preliminary_survey_fixed[preliminary_survey_fixed$site_name == input$site_input, ]
name <- str_c(site_sel$first_name, site_sel$last_name, sep = ' ')
aff <- str_c(name, site_sel$affiliation, sep = '\n')
contr <- str_c(aff, site_sel$country, sep = '\n')
cat(contr)
})
output$coord_ok <- renderInfoBox({
if (input$site_input == '') {
infoBox('Coordinates', 'NA', icon = shiny::icon('question'),
color = 'blue', width = 4, fill = TRUE)
} else {
site_sel <- preliminary_survey_fixed[preliminary_survey_fixed$site_name == input$site_input, ]
if (site_sel$is_inside_country) {
infoBox('Coordinates', 'OK', icon = shiny::icon('check'),
color = 'blue', width = 4, fill = TRUE)
} else {
infoBox('Coordinates', 'BAD',
icon = shiny::icon('exclamation-sign', lib = 'glyphicon'),
color = 'red', width = 4, fill = TRUE,
subtitle = 'Please revise coordinates')
}
}
})
})