-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
346 lines (299 loc) · 10.8 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# 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(DLMtool)
library(shiny)
library(shinyBS)
source("Functions.r")
###Server
shinyServer(function(input, output, session) {
## Sidebar
#Set the available data to the numeric vector required
availData<-reactive({
availdat <- input$availdat
if (length(availdat) <= 0) return(rep(0, length(DataTypes)))
as.numeric(DataTypes %in% availdat)
})
#PickMSE
MSE <- reactive({ChooseSFOb(input$Stock, input$Fleet)})
#Pick feasability object and resulting feasable MPs
Feasefun <- reactive({ModFease(availData(), AllFeaseObj)})
feaseMPs <- reactive({FeaseMPs(availData(), MSE(), Feasefun())})
#Pick DLM Data object
OurData <- reactive({ModDataObj(availData(), DLMData)})
# ## Debugging ##
# output$checkfease <- renderDataTable({
# FeaseObj <- ModFease(availData(), AllFeaseObj)
# tt<- as.list(FeaseObj)
# data.frame(unlist(tt ))
# })
# output$printfease <- renderText({
# tt <- capture.output(Fease)
# print(tt)
# HTML(tt)
# })
output$pckV <- renderText({
paste0("DLMtool Package Version: ", as.character(packageVersion("DLMtool")))
})
#################
#Pick Stock Name and Link
Stock <- reactive({
switch(as.numeric(input$Stock),
"Albacore",
"Snapper",
"Mackerel",
"Blue_shark",
"Sole",
"Rockfish"
)
})
output$StockName<-renderText({ if(input$Stock==0){"Please Select a Stock"} else{ colnames(Stock())[[2]]}})
output$StockLink<-renderDataTable({
if(input$Stock==0) output<-matrix(,15,)
else{
read.csv(paste0(switch(as.numeric(input$Stock),
"Albacore",
"Snapper",
"Mackerel",
"Blue_shark",
"Sole",
"Rockfish"
), ".csv"))
}
}, options = list(searching = FALSE, paging = TRUE))
#Pick Fleet Name and Link
Fleet<-reactive({
Name <- paste0("Fleet", input$Fleet)
Fleet<- get(Name)
return(Fleet)
})
output$FleetName<-renderText({ switch(input$Fleet,
"0"="Please Select a Fleet",
"1"="Stable Effort",
"2"="table Effort & Targetting Small Fish",
"3"="Increasing Effort",
"4"="Increasing Effort & Targetting Small Fish"
)})
output$FleetLink<-renderDataTable({
if(input$Fleet==0) output<-matrix(,15,)
else {
dat <- read.csv(paste0(switch(as.numeric(input$Fleet), "FlatE_NDom", "FlatE_Dom", "IncE_NDom", "IncE_HDom"), ".csv"))
dat
}
}, options = list(searching = FALSE, paging = TRUE))
observe({ # select all checkboxes
if(input$selectall == 0) return(NULL)
else if (input$selectall%%2 == 0)
{
updateCheckboxGroupInput(session,"availdat","Select Available Data Types::",choices=DataTypes)
}
else
{
updateCheckboxGroupInput(session,"availdat","Select Available Data Types:",choices=DataTypes,selected=DataTypes)
}
})
## end Sidebar
## Tabs
#Formatting for Tab names
output$instructions<-renderUI({if(input$tabsetpanel=="Instructions") {strong(h4("Instructions"))} else {p(h5("Instructions"))}})
output$MSEtab<-renderUI({if(input$tabsetpanel=="MSE") {strong(h4("Simulation Testing (MSE)"))} else {p(h5("Simulation Testing (MSE)"))}})
output$MPtab<-renderUI({if(input$tabsetpanel=="MP") {strong(h4("Method Application"))} else {p(h5("Method Application"))}})
output$VOItab<-renderUI({if(input$tabsetpanel=="VOI") {strong(h4("Value of Information"))} else {p(h5("Value of Information"))}})
#display anything?
showresults<- reactive({
# if (input$Stock==0 || input$Fleet==0 ||input$Xchoice==" "||input$Ychoice==" "|| is.element(1,availData())==FALSE)
if (input$Stock==0 || input$Fleet==0 ||input$Xchoice==" "||input$Ychoice==" ")
{0}
else {1}
})
output$display <- renderText(showresults())
output$display2 <- renderText(showresults())
output$display3 <- renderText(showresults()) # this is dodgy, but it works...
##Tab 1
#create the Trade Plot
# Data frame of performance of methods
Perf <- reactive({
PerfStats(MSE())
})
PerfMetrics <- reactive({
TradePerf(
Perf(),
XAxis=c(input$Xchoice),
YAxis=c(input$Ychoice),
XThresh=input$XThresh,
YThresh=input$YThresh,
maxVar=15)
})
clickedMP <- reactiveValues()
clickedMP$clicked <- NULL
observeEvent(input$plot_click,{
tempDF <- PerfMetrics()[[1]]
temp <- nearPoints(tempDF, input$plot_click, xvar="x", yvar="y", threshold=15, addDist=TRUE)
clickedMP$clicked <- head(temp,1)[,1]
})
#output the trade plot
output$tplot <- renderPlot({
TradePlot2(PerfMetrics(),
AvailMPs=feaseMPs(),
clickedMP=clickedMP$clicked,
XThresh=input$XThresh,
YThresh=input$YThresh)
}, height = 500, width = 500)
# output the pop-in information
output$info <- renderUI({
# if (length(input$plot_click)==0||showresults()==0 ) return("")
# #don't show anything if no plot point's been clicked
if(length(clickedMP$clicked)==0)
return(p( HTML("<i>","Click a point for details","</i>", sep="")
, class = "well"))
#actual info
p(HTML("<font size=\"6\" color=\"red\">●</font><i>",MPname(clickedMP$clicked),"</i>", sep=""), class = "well")
})
## Projection Plots ##
# Choose MPs
output$selectMP <- renderUI({
selectInput("MP1", "Method 1", as.character(PerfMetrics()[[1]][,1]),
selected = NULL)
})
GetMP2 <- reactive({
MPList <- as.character(PerfMetrics()[[1]][,1])
ind <- MPList %in% input$MP1
MPList[!ind]
})
output$selectMP2 <- renderUI({
# selectInput("MP2", "Method 2", GetMP2(),
MPList <- as.character(PerfMetrics()[[1]][,1])
selectInput("MP2", "Method 2", MPList,
selected = MPList[2])
})
output$pplot <- renderPlot({
# Top4 <- head(PerfMetrics()[[1]],4)
# subMPs <- as.character(Top4[,1])
if (length(input$MP1) > 0) if (input$MP1 == input$MP2) return("")
subMPs <- c(input$MP1, input$MP2)
MSEtemp <- MSE()
FMSYr<-quantile(MSEtemp@F_FMSY,c(0.001,0.90),na.rm=T)
BMSYr<-quantile(MSEtemp@B_BMSY,c(0.001,0.975),na.rm=T)
subMSE <- Sub(MSEtemp, MPs=subMPs, sims=1:60)
subMSE@Name <- "Projections"
Pplot2(subMSE, nam="", YLim1=FMSYr, YLim2=BMSYr)
})
output$kplot <- renderPlot({
# Top4 <- head(PerfMetrics()[[1]],4)
# subMPs <- as.character(Top4[,1])
if (length(input$MP1) > 0) if (input$MP1 == input$MP2) return("")
subMPs <- c(input$MP1, input$MP2)
MSEtemp <- MSE()
subMSE <- Sub(MSEtemp, MPs=subMPs, sims=1:60)
subMSE@Name <- "Kobe Plot"
Kplot2(subMSE, nam="")
})
##
output$SameMPs <- renderText({
if (length(input$MP1) > 0) if (input$MP1 == input$MP2) {
return("Please select two different methods")
}
})
##Tab 2
#Get the MPs that meet the performance criteria
AvailAcceptMPs <- reactive({
perf <- PerfMetrics()[[1]]
accept <- perf[,4]
mpnames <- as.character(perf[,1])
mpnames[mpnames %in% feaseMPs() & accept]
})
#Output the MP selector
output$MPselector<-renderUI({
if (showresults()==0) {selectInput("MP", label=h5(strong(em("Acceptable")), "and", strong(em("Available")), "Management Procedure(s) (select one):"), c("None Available or Acceptable"),width="100%")}
else{
#get the performing MPs
performingMPs<-AvailAcceptMPs()
if(length(performingMPs)==0) {performingMPs<-c("No Available Methods Meet Your Performance Metrics",performingMPs)}
else {
#remove all MPs with nas and add blank
performingMPs<-c(" ",performingMPs)
#name the MPs for greater comprehension in the UI
for (X in seq_along(performingMPs)){
names(performingMPs)[X]<-MPname(performingMPs[X])
}
}
#actual drop-down
selectInput("MP", label=h5("Available Management Procedure(s): (select one)"), performingMPs,width="100%",selected=performingMPs[1])
}})
#Output the data table mock
output$AvailableDataMockup <- renderTable({
if (!all(availData() ==0)) DataTable(OurData())
})
#Output the recommendation
output$mprecommendation<-renderText({
MP<-input$MP
if(length(MP)==0 || MP==" " || MP=="None Available" || MP=="No Available Methods Meet Your Performance Metrics") return("")
Name<-MPname(MP)
Class <- class(get(MP))
if (Class == "DLM_output") Class <- "output control"
if (Class == "DLM_input") Class <- "input control"
dat <- OurData()
test<-RunMP(1,OurData(),MP)
paste(gsub("ton","tons",gsub("TAC","Total Allowable Catch",test[1])),sep="")
})
##
##Tab 3
#Output the "No Available Plots" message
# output$voi2message<-renderText({
# if (showresults()==0) {return("")}
# #get the performing MPs
# #performingMPs<-PerformingMPs()
# # if (length(performingMPs)==0) {return ("No Management Procedures Meet Your Performance Metrics.")}
# else {
# Top4 <- head(PerfMetrics()[[1]],4)
# subMPs <- as.character(Top4[,1])
# MSEtemp <- MSE()
# subMSE <- Sub(MSEtemp, MPs=subMPs)
# MSEtemp<-Sub(MSEtemp, MPs=feaseMPs())
# # voi<-VOI2(MSEtemp,lay=T)
# # voiplots<-voi[[5]]
# # # voiplots<-Filter(length,voiplot)
# nmps <- MSEtemp@nMPs
# if(nmps < 2) {return("No Value of Information plots are available with the selected Available Data. Please select more Available Data to use this functionality.")
# #if(length(voiplots)==0) {return("No Value of Information plots are available above the Performance Metric thresholds with the selected Available Data. Please select more Available Data to use this functionality.")
# }
# else{
# return ("")}
# }
# })
#Output the VOI2 plot
output$voi2plot<-renderPlot({
# if (showresults()==0) {
# plot(1, type="n", axes=F, xlab="", ylab="")}
# else{
Perf <- PerfMetrics()[[1]]
Accept <- Perf[,4]
AvailMPs <- feaseMPs()
Perf$Avail <- FALSE
Perf$Avail[Perf$Names %in% AvailMPs] <- TRUE
AcceptMPs <- as.character(Perf$Names[Perf$Accept])
AcceptAvailMPs <- as.character(Perf$Names[Perf$Avail & Perf$Accept])
myMPs <- AcceptAvailMPs[1:4] # Top 4 methods
if (any(is.na(myMPs))) { # add some unavailable methods
naind <- which(is.na(myMPs))
Nna <- length(naind)
myMPs[naind] <- as.character(Perf$Names[Perf$Accept & !Perf$Avail][1:Nna])
}
if (any(is.na(myMPs))) { # add some unacceptable methods
naind <- which(is.na(myMPs))
Nna <- length(naind)
myMPs[naind] <- as.character(Perf$Names[!Perf$Accept][1:Nna])
}
MSEtemp <- MSE()
subMSE <- Sub(MSEtemp, MPs=myMPs)
VOIplot(subMSE, availMP=AvailMPs, acceptMP=AcceptMPs, Par=input$ptype)
# voi<-VOI2(MSEtemp,lay=T)
# voiplots<-voi[[5]]
# voiplots<-Filter(length,voiplots)
# if(length(voiplots)==0) {plot(1, type="n", axes=F, xlab="", ylab="")} else {
# MSEtemp@nMPs<-length(voiplots)
# VOI2(MSEtemp,lay=T)
# }
})
})