-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
199 lines (170 loc) · 6.43 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
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
library(dplyr)
library(myebird)
library(DT)
library(shinythemes)
ED <- as.data.frame(edge)
ED$GE[ED$GE == "CR"] <- "Critically Endangered"
ED$GE[ED$GE == "EN"] <- "Endangered"
ED$GE[ED$GE == "VU"] <- "Vulnerable"
ED$GE[ED$GE == "NT"] <- "Near Threatened"
ED$GE[ED$GE == "LC"] <- "Least Concern"
ED$GE[ED$GE == "DD"] <- "Data Deficient"
function(input, output) {
# save clean data as reactive object that can be used further down
mydata <- reactive({
if(!is.null(input$mydata)) {
mydata <- ebirdclean(input$mydata$datapath)
return(mydata)
}
else {
return(data.frame(NULL))
}
})
edgedat <- reactive({
if(!is.null(input$mydata)) {
if (input$grouping == TRUE) {
edgedat <- group_by(mydata(), Country) %>%
do(myedge(.))
} else {
edgedat <- myedge(mydata())
}
}
else {
return(data.frame(NULL))
}
})
phyres <- reactive({
if(!is.null(input$mydata)) {
out <- edgedat() %>% select(-sciName.edge)
if (input$grouping) {
out <- dplyr::arrange(out, Country, -ED.Score)
} else {
out <- dplyr::arrange(out, -ED.Score)
}
out$GE[out$GE == "CR"] <- "Critically Endangered"
out$GE[out$GE == "EN"] <- "Endangered"
out$GE[out$GE == "VU"] <- "Vulnerable"
out$GE[out$GE == "NT"] <- "Near Threatened"
out$GE[out$GE == "LC"] <- "Least Concern"
out$GE[out$GE == "DD"] <- "Data Deficient"
out <- rename(out, `Common name` = comName,
`Scientific name` = sciName,
`ED Score` = ED.Score,
`Red List Status` = GE,
`EDGE Score` = EDGE.Score,
`EDGE Rank` = EDGE.Rank)
}
})
output$phylo.results <- DT::renderDataTable(
datatable(phyres(),
options = list(pageLength = 50,
lengthMenu = list(c(10, 25, 50, 100, 500, -1), c(10, 25, 50, 100, 500, "All")),
order = list(list(6, "asc")),
columnDefs = list(list(visible = FALSE, targets = c(0)))),
filter = list(position="top", clear=FALSE, plain=TRUE)
)%>% formatRound(c("ED Score","EDGE Score"),digits=3))
# Calculates sum of Top 5 EDGE species from eBird data
Edscore2 <- eventReactive(input$action3, {
## to attempt to estumate top5 for each country
# if (input$grouping) {
# y1 <- group_by(edgedat(), Country)
# } else {
# y1 <- edgedat()
# }
# distinct() needed to remove duplicates when grouped by country
y1 <- edgedat() %>% ungroup %>% distinct(sciName, .keep_all = TRUE)
y2 <- arrange(y1, desc(EDGE.Score))
y3 <- y2 %>% top_n(n = 5, wt = EDGE.Score)
y4 <- y3 %>% summarise(sum = sum(EDGE.Score) %>%
round(digits = 2))
y5 <- as.character(y4[1])
})
output$EDSCORE2 <- Edscore2
# PD is calculated in this renderTable
output$summary.tab <- renderTable({
if(!is.null(input$mydata)) {
if(input$pd == TRUE) {
if (input$grouping) {
pddata <- mydata() %>%
group_by(Country) %>%
do(mypd(., ntrees = 5)) %>%
select(Country, median_pd) %>%
rename(`Total million years of evolution` = median_pd)
} else {
pddata <- mypd(mydata(), ntrees = 5) %>%
select(median_pd) %>%
rename(`Million Years` = median_pd)
}
}
if (input$grouping) {
sumout <- edgedat() %>% group_by(Country) %>%
summarise(`Sum of ED Scores` = sum(ED.Score),
`Sum of EDGE Scores` = sum(EDGE.Score))
} else {
sumout <- summarise(edgedat(), `Sum of ED Scores` = sum(ED.Score),
`Sum of EDGE Scores` = sum(EDGE.Score))
}
if (input$pd) {
if(input$grouping) {
return(left_join(pddata, sumout, by = "Country"))
} else {
return(cbind(pddata, sumout))
}
} else
return(sumout)
}
})
###Create selectable Original ED file from my ebird
output$origTable <- DT::renderDataTable({
datatable(
ED, options = list(pageLength=10,order = list(list(6, "asc")),
columnDefs = list(list(visible = FALSE, targets = c(0,7)))),
selection = list(mode = "multiple"),
filter=list(position="top",clear=FALSE,plain=TRUE),
colnames=c("","Scientific Name","Common Name","ED Score","Red List Status", "EDGE Score","EDGE Rank","Hidden key for outdated names")
) %>% formatRound(c("ED.Score","EDGE.Score"),digits=3)
})
origTable_selected <- reactive(as.data.frame({
ids <- input$origTable_rows_selected
ED[ids,]
}))
###Render a new table with selected species
output$origTableSelected <- DT::renderDataTable({
datatable(
origTable_selected(),colnames=c("My Ranking","Scientific Name","Common Name","ED Score", "Red List Status", "EDGE Score","EDGE Rank","Hidden key for outdated names"),
options = list(dom = "t",order = list(list(6, "asc")),
scrollY = '250px', paging = FALSE ,
columnDefs = list(list(visible = FALSE, targets = c(7)))),
selection = list(mode = "multiple"),
caption = "My EDGE checklist",
callback=JS("table.on( 'order.dt search.dt', function () {
table.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
cell.innerHTML = i+1;});}).draw();")
) %>% formatRound(c("ED.Score","EDGE.Score"),digits=3)
})
##Save the new table as csv.
output$downloadData <- downloadHandler('myedge.csv', content = function(file) {
s <- input$origTable_rows_selected
write.csv(ED[s, , drop = FALSE], file)
})
###Obtain PD2###
pd2<-eventReactive(input$action,{
pd2b<-mypd(origTable_selected(), ntrees = 5)
pd2c<-as.character(round(pd2b[2],digits=2))
})
output$pd2<-pd2
###Obtain sum of TOP 5 birds
Edscore1 <- eventReactive(input$action2, {
x2 <- head(arrange(origTable_selected()),desc(EDGE.Score), n = 5)
x3<-x2 %>%
summarise(sum(EDGE.Score),n = n())
x4<-round(x3,digits=2)
x5<-as.character(x4[1])})
output$EDSCORE1<-Edscore1
}