forked from ices-tools-dev/LBIndicator_shiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
682 lines (599 loc) · 25.9 KB
/
app.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
rm(list = ls())
library(LBSPR)
library(shiny)
library(reshape2)
library(ReporteRs)
library(ggplot2)
source("utilities.R")
# Define the UI
ui <- tagList(
tags$style(HTML("div#lb_indicator_plot img {width: auto; height: auto; max-width: auto; max-height: 800px;}")),
navbarPage(title = "LBIndicator Application",
id = "navbar",
tabPanel(title = "Instructions",
p("To use the LBI Application you will need: 1) a length frequency distribution 2) weight at length data, and 3) estimates of the life history parameters. The following paragraphs outline the steps to use the LBI Application. Each heading refers to a tab on the menu."),
h4("Upload Data"),
p("The first step is to upload two CSV (comma separated variable) files containing the length frequency distribution and mean weight at length. The file must be in CSV format and contain only numeric values except for the header row which can contain labels. Multiple years of data should be placed in seperate columns."),
p("Length frequency data must have the midpoints of the length classes (the length bins) in the first column, and numeric values for all counts (i.e., all columns are the same length). Length measurements should be raw numbers, each column representing a different year."),
p("Two example stocks have been included. Click the 'I want to look at sample data' radio button to make see the exact format."),
h4("Plot length frequency distribution"),
p("Slide the bar according to the bin width that you want to aggregate the data. Both length frequency distribution and weight at length data frames are modified concurrently. Only the LFD is displayed"),
h4("LBI plot"),
p("Enter the life history parameters for your stock. The application will automatically generate the LBI indicators and reference points."),
h4("LBI table"),
p("The last 3 years of LBI indicators are generated in a table relative to reference points. The shading of the cell indicates the relative status."),
h4("Downloads"),
p("All figures and tables can be downloaded as a .docx report or as individual .png images.")
), #tabPanel
tabPanel(title = "Upload data",
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "input_type",
label = "How do you want to upload data?",
list("I have a length frequency distribution I want to explore." = "input_file",
"I want to look at sample data" = "example_file"),
selected = ""),
conditionalPanel(
condition = "input.input_type == 'input_file'",
fileInput(inputId = 'choose_input_data',
label = "Choose length frequency distribution to upload",
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
), #fileInput
fileInput(inputId = 'choose_weight_data',
label = "Choose weight at length data to upload",
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
), #fileInput
radioButtons(inputId = 'separator_button',
label = 'Separator',
selected = ",",
choices = c(
Comma = ',',
Semicolon = ';',
Tab = '\t')
), #radioButtons
actionButton(inputId = "load_input_data",
label = "Load Data",
icon("cloud-upload")
) #fileInput
), #conditionalPanel
conditionalPanel(
condition = "input.input_type == 'example_file'",
selectInput(inputId = 'choose_example_data',
label = "Example Data",
choices = c(
"",
"Frequency - Nephrops" = "freqNeph",
"Frequency - Gadoid" = "freqGad")
) #selectInput
) #ConditionalPanel
), #sidebarPanel
mainPanel(
h3("Length frequency distribution"),
h4("Steps:"),
tags$ol(
tags$li("Upload a CSV data file or select an example data set from the menu on the left."),
tags$li("Length frequency distributions should have the length bin in the first column and annual frequency in subsequent columns."),
tags$li("Double check the first 6 rows to make sure everything looks OK.")
),
dataTableOutput("data_table")
) # mainPanel
) # sidebar layout
), #tabPanel
tabPanel(title = "Plot length frequency data",
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "binwidth_value",
label = "Bin width for length frequencies",
min = 1,
max = 100,
value = ""
), #sliderInput
radioButtons(inputId = 'length_units',
label = "",
selected = ",",
choices = c(
"cm" = 'cm',
"mm" = 'mm',
"in" = 'in')
) #radioButtons
), #sidebarPanel
mainPanel(
h3("Plots go here"),
h4("Steps:"),
tags$ol(
tags$li("Select the optimal bin width for length frequencies and the proper units. Note: units are only used for plot labels."),
tags$li("If the length frequencies displayed are not correct, please review the raw data")
),
# Reactive length frequencies
plotOutput("histogram_plot")
) # mainPanel
) # sidebar layout
), #tabPanel
tabPanel(title = "Length based indicator - plots",
sidebarLayout(
sidebarPanel(
# Input Linf and Lmat parameters
tags$div(
HTML("L<sub>∞</sub>")
),
numericInput(inputId = 'linf_value',
label = "",
value = "",
min = 1,
max = 100
), # textInput
tags$div(
HTML("L<sub>mat</sub>")
),
numericInput(inputId = 'lmat_value',
label = "",
value = "",
min = 1,
max = 100
), # textInput
tags$div(
HTML("m/k ratio")
),
numericInput(inputId = 'mk_ratio',
label = "",
value = "1.5",
min = 0,
max = 100
) # textInput
), #sidebarPanel
mainPanel(
h3("Length-based Indicator plots"),
# h4(""),
tags$ol(
HTML("Enter the L<sub>∞</sub>, L<sub>mat</sub>, and m/k ratio parameters")
),
# Reactive table
plotOutput("lb_indicator_plot")
) # mainPanel
) # sidebar layout
), #tabPanel
tabPanel(title = "Length based indicator - table",
# sidebarLayout(
# sidebarPanel(
# ), #sidebarPanel
# mainPanel(
h3("Stock status relative to reference points"),
tableOutput(outputId = "lb_indicator_table")
# ) # mainPanel
# ) # sidebar layout
), #tabPanel
tabPanel("Downloads",
# h3(":"),
# tags$ol(
# tags$li("Fill in the name of your stock if you want to download summary "),
# tags$li("")
# ),
# h4("Binned Data"),
# downloadButton('download_data',
# 'Download binned data (.csv)'
# ), # downloadButton
# br(),
h4("Plots"),
downloadButton('download_lb_plot',
'Download indicator time series plots (.png)'
), # downloadButton
# downloadButton('download_histogram',
# 'Download histogram (.png)'
# ), # downloadButton
br(),
h4("Summary sheet"),
downloadButton('download_doc',
'Download summary document (.docx)'
), # downloadButton
br(),
br(),
textInput(inputId = 'stock_name',
label = "Enter stock name as filename",
value = "My stock"
), # textInput
h3("Sources:"),
p("The application was created by Scott Large from example R code generously supplied to WKLIFE5 by T. Miethe and C. Silva. Inspiration for the application was drawn from J. Cope and A. Hordyk."),
p("The application should be used as a tool to explore data and a thorough audit of the length based indicators should be conducted before they can be used for decision making."),
p("Source code can be found at https://github.com/ices-tools-dev/LBIndicator_shiny. Questions and issues should be directed to the github repository.")
) #tabPanel
) # NavbarPage
) #tagList
server <- function(input, output, session) {
# LOAD YOUR OWN DATA
data_from_user <- reactive({
req(input$input_type,
input$choose_input_data,
input$load_input_data
)
datapath <- input$choose_input_data$datapath
dat <- read.csv(file = datapath,
header = TRUE,
sep = input$separator_button,
stringsAsFactors = FALSE,
check.names = FALSE)
if(class(dat) == "data.frame" | class(dat) == "matrix") {
if(ncol(dat) > 1) {
chk_NAs <- apply(dat, 2, is.na) # check NAs
dat <- dat[!apply(chk_NAs, 1, prod), , drop = FALSE]
dat <- dat[, !apply(chk_NAs, 2, prod), drop = FALSE]
}
}
if(class(dat) == "numeric" | class(dat) == "integer") {
dat <- dat[!is.na(dat)]
}
return(dat)
})
weights_from_user <- reactive({
req(input$input_type,
input$choose_weight_data,
input$load_input_data
)
# weights
weightpath <- input$choose_weight_data$datapath
wght <- read.csv(file = weightpath,
header = TRUE,
sep = input$separator_button,
stringsAsFactors = FALSE,
check.names = FALSE)
if(class(wght) == "data.frame" | class(wght) == "matrix") {
if(ncol(wght) > 1) {
chk_NAs <- apply(wght, 2, is.na) # check NAs
wght <- wght[!apply(chk_NAs, 1, prod), , drop = FALSE]
wght <- wght[, !apply(chk_NAs, 2, prod), drop = FALSE]
}
}
if(class(wght) == "numeric" | class(wght) == "integer") {
wght <- wght[!is.na(wght)]
}
return(wght)
})
data_from_example <- reactive({
req(
input$input_type,
input$choose_example_data
)
weightpath <- input$choose_weight_data$datapath
read.csv(switch(input$choose_example_data,
freqNeph = "data/freqNeph.csv",
freqGad= "data/freqGad.csv"),
header = TRUE,
stringsAsFactors = FALSE,
check.names = FALSE)
})
weights_from_example <- reactive({
req(
input$input_type,
input$choose_example_data
)
read.csv(switch(input$choose_example_data,
freqNeph = "data/walNeph.csv",
freqGad = "data/walGad.csv"),
header = TRUE,
stringsAsFactors = FALSE,
check.names = FALSE)
})
output$data_table <- renderDataTable({
# Print out first 6 observations
validate(
need(input$input_type, "")
)
if(input$input_type == "input_file"){
validate(
need(input$choose_input_data, "Load length frequency data to explore"),
need(input$choose_weight_data, "Load the corresponding weight at length data, too.")
)
dat <- data_from_user()
wght <- weights_from_user()
validate(
need(all(dim(dat) == dim(wght)), "Dimensions of length frequency data and weight at length data do not agree")
)
}
if(input$input_type == "example_file"){
validate(
need(input$choose_example_data, "Select the example data to explore")
)
dat <- data_from_example()
wght <- weights_from_example()
}
head(dat)
}, options = list(pageLength = -1,
searching = FALSE,
paging = FALSE,
ordering = FALSE,
info = FALSE)
)
output$histogram_plot <- renderPlot({
validate(
need(input$input_type, "")
)
if(input$input_type == "input_file"){
validate(
need(input$choose_input_data, "Load length frequency data to explore"),
need(input$choose_weight_data, "Load the corresponding weight at length data, too."),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_user()
wght <- weights_from_user()
}
if(input$input_type == "example_file"){
validate(
need(input$choose_example_data, "Select the example data to explore"),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_example()
wght <- weights_from_example()
}
bin_plot(dat,
binwidth = input$binwidth_value,
l_units = input$length_units)
})
output$lb_indicator_data <- renderDataTable({
validate(
need(input$input_type, "")
)
if(input$input_type == "input_file"){
validate(
need(input$choose_input_data, "Load length frequency data to explore"),
need(input$choose_weight_data, "Load the corresponding weight at length data, too."),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_user()
wght <- weights_from_user()
}
if(input$input_type == "example_file"){
validate(
need(input$choose_example_data, "Select the example data to explore"),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_example()
wght <- weights_from_example()
}
validate(
need(input$linf_value, "Enter Linf"),
need(input$lmat_value, "Enter Lmat"),
need(input$mk_ratio, "Enter mk_ratio")
)
lb_dat <- lb_table(data = dat,
binwidth = input$binwidth_value,
linf = input$linf_value,
lmat = input$lmat_value,
mk_ratio = input$mk_ratio,
weight = wght)
})
output$lb_indicator_plot <- renderPlot({
validate(
need(input$input_type, "")
)
if(input$input_type == "input_file"){
validate(
need(input$choose_input_data, "Load length frequency data to explore"),
need(input$choose_weight_data, "Load the corresponding weight at length data, too."),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_user()
wght <- weights_from_user()
}
if(input$input_type == "example_file"){
validate(
need(input$choose_example_data, "Select the example data to explore"),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_example()
wght <- weights_from_example()
}
validate(
need(input$linf_value, "Enter Linf"),
need(input$lmat_value, "Enter Lmat"),
need(input$mk_ratio, "Enter m/k ratio")
)
lb_plot(data = dat,
binwidth = input$binwidth_value,
l_units = input$length_units,
linf = input$linf_value,
lmat = input$lmat_value,
mk_ratio = input$mk_ratio,
weight = wght)
},
width = 1000,
height = 600)
output$lb_indicator_table <- renderFlexTable({
validate(
need(input$input_type, "")
)
if(input$input_type == "input_file"){
validate(
need(input$choose_input_data, "Load length frequency data to explore"),
need(input$choose_weight_data, "Load the corresponding weight at length data, too."),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_user()
wght <- weights_from_user()
}
if(input$input_type == "example_file"){
validate(
need(input$choose_example_data, "Select the example data to explore"),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_example()
wght <- weights_from_example()
}
validate(
need(input$linf_value, "Enter Linf"),
need(input$lmat_value, "Enter Lmat")
)
lb_table(data = dat,
binwidth = input$binwidth_value,
l_units = input$length_units,
linf = input$linf_value,
lmat = input$lmat_value,
mk_ratio = input$mk_ratio,
weight = wght)
})
# }
#
# output$download_data <- downloadHandler(
#
# filename = function() {
# nm <- input$stock_name
# nm <- gsub(" ", "", nm)
# if (nchar(nm) < 1) nm <- "stock_name"
# paste0(nm, '_binned_data.docx')
# },
#
# content = function(file) {
# validate(
# need(input$input_type, "")
# )
#
# if(input$input_type == "input_file"){
# validate(
# need(input$choose_input_data, "Load length frequency data to explore"),
# need(input$choose_weight_data, "Load the corresponding weight at length data, too."),
# need(input$binwidth_value, "Select the binwidth for the histograms"),
# need(input$length_units, "Select the proper units.")
# )
# dat <- data_from_user()
# wght <- weights_from_user()
# }
# if(input$input_type == "example_file"){
# validate(
# need(input$choose_example_data, "Select the example data to explore"),
# need(input$binwidth_value, "Select the binwidth for the histograms"),
# need(input$length_units, "Select the proper units.")
#
# )
# dat <- data_from_example()
# wght <- weights_from_example()
# }
#
# bin_mat(data = dat,
# binwidth = input$binwidth_value,
# l_units = input$length_units)
# }
# )
#
# output$download_histogram <- downloadHandler(
#
# )
output$download_doc <- downloadHandler(
filename = function() {
nm <- input$stock_name
nm <- gsub(" ", "", nm)
if (nchar(nm) < 1) nm <- "stock_name"
paste0(nm, '_LBI_summary.docx')
},
content = function(file) {
validate(
need(input$input_type, "")
)
if(input$input_type == "input_file"){
validate(
need(input$choose_input_data, "Load length frequency data to explore"),
need(input$choose_weight_data, "Load the corresponding weight at length data, too."),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_user()
wght <- weights_from_user()
}
if(input$input_type == "example_file"){
validate(
need(input$choose_example_data, "Select the example data to explore"),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_example()
wght <- weights_from_example()
}
validate(
need(input$linf_value, "Enter Linf"),
need(input$lmat_value, "Enter Lmat"),
need(input$mk_ratio, "Enter M/K ratio")
)
lb_doc(data = dat,
binwidth = input$binwidth_value,
l_units = input$length_units,
linf = input$linf_value,
lmat = input$lmat_value,
mk_ratio = input$mk_ratio,
stock = input$stock_name,
weight = wght,
filename = file)
})
output$download_lb_plot <- downloadHandler(
filename = function() {
nm <- input$stock_name
nm <- gsub(" ", "", nm)
if (nchar(nm) < 1) nm <- "stock_name"
paste0(nm, '_LBI.png')
},
content = function(file) {
validate(
need(input$input_type, "")
)
if(input$input_type == "input_file"){
validate(
need(input$choose_input_data, "Load length frequency data to explore"),
need(input$choose_weight_data, "Load the corresponding weight at length data, too."),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_user()
wght <- weights_from_user()
}
if(input$input_type == "example_file"){
validate(
need(input$choose_example_data, "Select the example data to explore"),
need(input$binwidth_value, "Select the binwidth for the histograms"),
need(input$length_units, "Select the proper units.")
)
dat <- data_from_example()
wght <- weights_from_example()
}
validate(
need(input$linf_value, "Enter Linf"),
need(input$lmat_value, "Enter Lmat"),
need(input$mk_ratio, "Enter M/k ratio")
)
png(file,
bg = "white",
pointsize = 5,
units = "cm",
width = 16,
height = 15,
res = 400)
lb_plot(data = dat,
binwidth = input$binwidth_value,
l_units = input$length_units,
linf = input$linf_value,
lmat = input$lmat_value,
mk_ratio = input$mk_ratio,
weight = wght)
dev.off()
})
}
#
# Return a Shiny app object
shinyApp(ui = ui, server = server)