-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
260 lines (221 loc) · 9.67 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
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
library(shiny)
library(xtable)
library(ggplot2)
source("global.R")
source("processEbd.R")
source("genSpeciesList.R")
source("genSummary.R")
source("writeWordDoc.R")
source("genBirdDensity.R")
source("genCommonSpecies.R")
source("genThreatenedSpecies.R")
source("genGroupSummaries.R")
source("genDiversity.R")
source("genGuildAnalysis.R")
source("surveymapfunctions.R")
source("genMaps.R")
options(shiny.maxRequestSize=30*1024^2)
shinyServer <- function(session, input, output) {
ebd_proc <- reactive ({
req(input$zipfile)
processEBirdFiles(input$zipfile, species, forestmap, input$forestDivision, input$startdate, input$enddate, input$pickalllists)
})
birdlist <- reactive({
ebd_proc() %>%
generateOverallBirdDensity () %>%
setorder (-Density) %>%
inner_join (species, by = c('Scientific Name' = 'Scientific.Name'))
})
observe(
{
updateSelectInput(session,
input = "speciesname",
choices = birdlist()$English.India)
}
)
# An output with renderTable. There are more formatting options which are not tried
output$summary <- renderTable ( {
print (input$zipfile$datapath)
ebd <- ebd_proc()
cbind (generateSummary(ebd), generateOverallSummary(ebd, paste(input$forestDivision,' Division')))
})
# Trying out render Table table - looks neat and formatted
output$checklist <- renderDataTable ( {
ebd_proc() %>%
generateSpeciesList ()
},
options = list( lengthMenu = list(c(20, 50, 100, 300, 500, -1), c('20', '50', '100', '300', '500', 'All')),
pageLength = 100))
output$density <- renderDataTable ( {
ebd_proc() %>%
generateOverallBirdDensity () %>%
subset(select = c("SlNo", "English Name","Scientific Name", "IUCN", "WG", "Density"))
},
options = list( lengthMenu = list(c(20, 50, 100, 300, 500, -1), c('20', '50', '100', '300', '500', 'All')),
pageLength = 100))
output$iucnspecies <- renderTable ( {
ebd_proc() %>%
generateBirdDensity() %>%
generateThreatenedDensity()
})
output$endemicspecies <- renderTable ( {
ebd_proc() %>%
generateBirdDensity() %>%
generateGroupSummaries ("WG")
})
output$shannon <- renderPlot ( {
inTable <- ebd_proc() %>%
genCommunityData() %>%
genShannonDiversity ()
barplot( t(inTable), width = 1, xlim= c(1,4 * nrow(inTable)), ylim = c(0,ceiling(max(inTable))), space = 2, border = par("fg"), main="Shannon Diversity",
xlab="Ranges", col=c("darkblue","red")) %>%
text(t(inTable), labels = t(inTable), pos = 3)
}, width = 1000, height = 800)
output$braycrutis <- renderPlot ( {
ebd_proc() %>%
genCommunityData() %>%
genClusterAnalaysis() %>%
plot(horiz = T, main="Cluster Analysis", xlim=c(1.0, 0.0), xlab="Dissimilarity", ylab = "Ranges")
}, width = 1000, height = 800)
output$guildAnalysis <- renderPlot ( {
ebd_proc() %>%
genGuildAnalysis () %>%
ggplot(aes(x = Range, y = Percentage, fill=Guild)) +
geom_bar(stat='identity', size = 10, show.legend = TRUE)
}, width = 1000, height = 800)
output$surveymaps <- renderPlot ( {
req(input$speciesname)
surveymaps(species = as.character(species[species$English.India == input$speciesname,]$Scientific.Name),
data = ebd_proc(),
tempmap = forestmap,
filter = input$forestDivision,
dataformat = "PROCESSED DATA",
gridsize = as.numeric(input$gridsize),
smooth = input$smooth,
h = 0.1,
cutoff = as.numeric(input$cutoff),
showempty = input$empty)
}, width = 500, height = 400)
# Writing to a word document
output$downloadData <- downloadHandler(
filename = function() { paste('Report_', input$forestDivision,
Sys.Date(),'_',
'.docx', sep='') },
content = function(file) {
ebd <- ebd_proc()
doc <- read_docx() %>%
createWordDocument(paste ('Birds of', input$forestDivision)) %>%
createTableinDoc ( cbind (generateSummary(ebd), generateOverallSummary(ebd, paste(input$forestDivision,' Division'))),
paste('Summary of Birds of', input$forestDivision)) %>%
createTableinDoc ( ebd %>%
generateSpeciesList (), paste('Checklist of Birds of', input$forestDivision)) %>%
createTableinDoc ( ebd %>%
generateOverallBirdDensity () %>%
subset(select = c("SlNo", "English Name","Scientific Name", "IUCN", "WG", "Density")),
paste('Density of Birds of', input$forestDivision))
chartdatasplit <- ebd %>%
generateBirdDensity() %>%
generateCommonSpecies()
for (x in names(chartdatasplit)){ ##go through all individually stored variable data frames in chartdatasplit list
tabledata <- chartdatasplit[[x]]
createTableinDoc(doc, tabledata, paste ("Most Common Birds of",x,"Range"))
}
doc <- doc %>%
createTableinDoc ( ebd %>%
generateBirdDensity() %>%
generateThreatenedDensity(), paste('Threatened Birds of', input$forestDivision)) %>%
createTableinDoc ( ebd %>%
generateBirdDensity() %>%
generateGroupSummaries("WG"), paste('Endemic Birds of', input$forestDivision)) %>%
# createBarPlotinDoc (ebd %>%
# genCommunityData() %>%
# genShannonDiversity(), paste('Bird Diversity Index for', input$forestDivision)) %>%
createStackedBarPlotinDoc (ebd %>%
genGuildAnalysis(), paste('Guild Analysis for', input$forestDivision))
if(length(unique(ebd$RANGE)) > 1)
{ # If there is only a single range, then these does not make sense
# doc <- doc %>%
# createPlotinDoc ( ebd %>%
# genCommunityData() %>%
# genClusterAnalaysis(), paste('Cluster Analysis for', input$forestDivision))
}
chartdatasplit <- ebd %>%
generateIndicatorSpecies(input$indicaterspeciesperrange)
for (x in names(chartdatasplit)) { ##go through all individually stored variable data frames in chartdatasplit list
tabledata <- chartdatasplit[[x]]
createTableinDoc(doc, tabledata, paste ("Encounter Rates of ",x))
}
print(doc, target = file )
print(file)
}
)
# Multiple tables in one tab using HTML. Useful but formatting not yet perfect
output$tables_common_species <- renderUI({
# HTML formatter for the list of data frames
tableize <- function(chartdatasplit){
tables <- list()
for (x in names(chartdatasplit)){ ##go through all individually stored variable data frames in chartdatasplit list
tabledata <- chartdatasplit[[x]] ###function that returns a dataframe to use in table
tables[[as.character(x)]] <-
print(xtable(tabledata, caption=paste("Range:",x)),
type="html", include.rownames = FALSE,
html.table.attributes='class="data table table-bordered table-condensed"',
caption.placement="top", auto=TRUE)
}
return(lapply(tables,paste))
}
out <- ebd_proc() %>%
generateBirdDensity() %>%
generateCommonSpecies() %>%
tableize %>%
unlist
return(div(HTML(out),class ="shiny-html-output"))
})
output$tables_indicator_species <- renderUI({
# HTML formatter for the list of data frames
tableize <- function(chartdatasplit){
tables <- list()
for (x in names(chartdatasplit)){ ##go through all individually stored variable data frames in chartdatasplit list
tabledata <- chartdatasplit[[x]] ###function that returns a dataframe to use in table
tables[[as.character(x)]] <-
print(xtable(tabledata, caption=paste("Group: ",x)),
type="html", include.rownames = FALSE,
html.table.attributes='class="data table table-bordered table-condensed"',
caption.placement="top", auto=TRUE)
}
return(lapply(tables,paste))
}
out <- ebd_proc() %>%
generateIndicatorSpecies(input$indicaterspeciesperrange) %>%
tableize %>%
unlist
return(div(HTML(out),class ="shiny-html-output"))
})
# Writing to a zip file with all maps
output$downloadMaps <- downloadHandler(
filename = function() { paste('Maps_', input$forestDivision,
Sys.Date(),'_',
'.zip', sep='') },
content = function(file) {
ebd <- ebd_proc()
saveRDS(ebd, "temp.RDS")
ebd <- readRDS("temp.RDS")
freqdata <- generateOverallBirdDensity(ebd)
dir <- "maps"
dir.create(dir)
genMaps(ebd, freqdata, forestmap,
input$forestDivision,
as.numeric(input$gridsize),
input$smooth,
as.numeric(input$cutoff),
input$empty,
noofspecies = as.numeric(input$noofspecies),
folder = dir,
extension = '.jpg')
print(dir)
print(zip(file, files = dir))
print(file)
unlink(dir, TRUE)
}
)
}