-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
329 lines (251 loc) · 10.1 KB
/
server.R
File metadata and controls
329 lines (251 loc) · 10.1 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
322
323
324
325
#########################################################################################
#load necessary packages
#########################################################################################
require("leaflet")
require("dplyr")
require("plotly")
require("ggplot2")
require("dplyr")
require("reshape2")
require("tidyverse")
#########################################################################################
#load data sets used
#########################################################################################
#Genreal data
dados <- read_csv("2018_09_04_dados.csv")
#Convert data to data
dados <- dados %>%
mutate(Data = as.Date(Data, format = "%d/%m/%Y"))
#iNext data
sample_coverage <- read_csv("sample_coverage.csv")
sample_coverage <- sample_coverage %>%
mutate(lty = factor(method, c("interpolated", "observed", "extrapolated"),
c("interpolation", "interpolation", "extrapolation")))
hill_numbers <- read.csv("hill_numbers.csv")
hill_numbers <- hill_numbers %>%
mutate(lty = factor(method, c("interpolated", "observed", "extrapolated"),
c("interpolation", "interpolation", "extrapolation")))
hill_ci <- read_csv("hill_ci.csv")
#Permanova data
perm_all <- read_csv("Perm_res_all.csv")
perm_adu <- read_csv("Perm_res_adult.csv")
perm_nym <- read_csv("Perm_res_nymph.csv")
#########################################################################################
#Manipulating data for sites map
#########################################################################################
#Couting the n of individulas in each point
map_plot <- dados %>% select("Ambiente", "Especie", "Lat", "Long") %>%
group_by_all() %>%
summarise(N = n())
map_plot <- map_plot %>% spread(Especie, N, fill = 0)
#Cretae a new data frame
dados_riq <- data.frame(lapply(map_plot[5:12], function(x) ifelse(x >= 1,1,0)))
dados_riq <- dados_riq %>%
mutate(riq = rowSums(dados_riq))
dados_amb <- map_plot %>% select("Ambiente", "Lat", "Long")
dados_amb <- data.frame(dados_amb)
map_plot <- cbind(dados_amb, dados_riq)
map_plot <- map_plot %>%
group_by(Lat, Long, Ambiente) %>%
summarise(riq = sum(riq))
pal <- colorFactor(c("#2b8cbe", "#006d2c"), domain = c("Periurbano", "Rural"))
#########################################################################################
#Manipulating data descriptive analysis
#########################################################################################
gg_esp <- dados %>%
group_by(Especie) %>%
summarise(count = n()) %>%
rename(Abundance = count)
#Creating a plot representing proportion of each stage
gg_prop <- dados %>%
group_by(Especie, Estagio) %>%
summarise(Abundance = n())
gg_prop <- dcast(Especie ~ Estagio, data = gg_prop)
gg_prop <- gg_prop %>%
replace(., is.na(.), 0) %>%
mutate(Total = Adulto + Ninfa)
#Change to long format
gg_prop <- melt(gg_prop, id.vars = c("Especie", "Total"))
gg_prop <- gg_prop %>%
mutate(Proportion = round(value/Total, 2))
#########################################################################################
#Manipulating data for time series analysis
#########################################################################################
ts_plot <- dados %>%
mutate(Estagio = ifelse(Estagio == "Adulto", "Adult", "Nymph")) %>%
mutate(Data = format(as.Date(Data), "%Y-%m")) %>%
group_by(Data, Especie, Estagio) %>%
summarise(Abundance = n())
#########################################################################################
#Creating functionality
#########################################################################################
server <- function(input, output){
#Plot the map
output$mapa <- renderLeaflet({
leaflet(data = map_plot) %>%
addTiles() %>%
setView(lng = -63.87, lat = -8.75, zoom = 10) %>%
addCircleMarkers(radius = ~riq * 2, color = ~pal(Ambiente), stroke = FALSE, fillOpacity = 0.8,
label = ~paste("Number of species colected: ", riq)) %>%
addLegend("bottomright", colors = c("#2b8cbe", "#006d2c"),
label = c("Periurbano", "Rural"), title = "Number of Species")
})
output$wihtakker <- renderPlotly({
ggplotly(ggplot(data = gg_esp, aes(x = reorder(Especie, Abundance), y = Abundance)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw() +
labs(y = "Abundance", x = "Thick Species"),
tooltip = "y")
})
output$prop_plot <- renderPlotly({
ggplotly(ggplot(data = gg_prop, aes(x = reorder(Especie,Total), y = Proportion,
fill = variable)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw() +
labs(y = "Proportion of Individuals", x = "Thick Species"),
tooltip = "y")
})
#Function to filter data based on input
data_to_plot <- reactive({
if(input$sel_stage == "All" & input$sel_species == "All"){
data <- ts_plot
data <- data %>% group_by(Data, Estagio) %>% summarise(Abundance = sum(Abundance))
}
else if(input$sel_stage == "All" & input$sel_species != "All"){
data <- ts_plot[ts_plot$Especie == input$sel_species,]
}
else if(input$sel_stage != "All" & input$sel_species == "All"){
data <- ts_plot[ts_plot$Estagio == input$sel_stage,]
data <- data %>% group_by(Data) %>% summarise(Abundance = sum(Abundance))
}
else{
data <- ts_plot[ts_plot$Especie == input$sel_species & ts_plot$Estagio == input$sel_stage, ]
}
data
})
#Plot ts
output$ts_plot <- renderPlotly({
ts <- data_to_plot()
if(input$sel_stage != "All" & input$sel_species == "All"){
ggplotly(ggplot(data = ts,aes(x = Data, y = Abundance)) +
geom_bar(stat = "identity") +
theme_bw() +
labs(y = "Abundance", x = "Data of the collect"),
tooltip = "y")
}
else{
ggplotly(ggplot(data = ts,aes(x = Data, y = Abundance, fill = Estagio)) +
geom_bar(stat = "identity") +
theme_bw() +
labs(y = "Abundance", x = "Data of the collect"),
tooltip = "y")
}
})
#Function to filter data based on input
hill_data <- reactive({
if(input$sel_hill == "Sample coverage"){
data_sub <- sample_coverage
}
else{
data_sub <- hill_numbers
data_sub <- data_sub[data_sub$order == input$sel_hill, ]
}
data_sub
})
#Plot iNext graphs
output$hill_plot <- renderPlot({
data_sub <- hill_data()
if(input$sel_hill == "Sample coverage"){
ggplot(data_sub, aes(x = x, y = y, col = site)) +
theme_bw(base_size = 24) +
geom_line(aes(linetype = lty), lwd = 1.5) +
theme(legend.position = "bottom",
legend.title = element_blank()) +
labs(x = "Number of individuals", y = "Sample coverage") +
geom_ribbon(aes(ymin = y.lwr, ymax = y.upr, fill = site, colour = NULL),
alpha = 0.2)
}
else{
ggplot(data_sub, aes(x = x, y = y, col = site)) +
theme_bw(base_size = 24) +
geom_line(aes(linetype = lty), lwd = 1.5) +
theme(legend.position = "bottom",
legend.title = element_blank()) +
labs(x = "Number of individuals", y = "Species diversity") +
geom_ribbon(aes(ymin = y.lwr, ymax = y.upr, fill = site, colour = NULL),
alpha = 0.2)
}
})
#Function to read ci
table_ci <- reactive({
if(input$sel_hill == "Sample coverage"){
data_ci <- hill_ci[is.na(hill_ci$Diversity),]
}
else{
data_ci <- hill_ci
data_ci <- data_ci[data_ci$Diversity == input$sel_hill, ]
}
data_ci
})
#Retrun table
output$table1 <- renderTable({
table_ci()
})
#Function to read permanova data
Permanova_table <- reactive({
if(input$sel_com == "All"){
data_perm <- perm_all
}
else if(input$sel_com == "Adult"){
data_perm <- perm_adu
}
else{
data_perm <- perm_nym
}
})
#Retrun table
output$table2 <- renderTable({
Permanova_table()
})
#Return image
output$image <- renderImage({
if(input$sel_com == "All"){
return(
list(
src = "www/All_venn.png",
filetype = "image/png",
alt = "Venn All",
width = 640,
height = 480,
align = "center"
)
)
}
else if(input$sel_com == "Adult"){
return(
list(
src = "www/Adult_venn.png",
filetype = "image/png",
alt = "Venn All",
width = 640,
height = 480,
align = "center"
)
)
}
else{
return(
list(
src = "www/Ninfa_venn.png",
filetype = "image/png",
alt = "Venn All",
width = 720,
height = 480,
align = "center"
)
)
}
},deleteFile = FALSE)
}