forked from cmjt/ascr_shiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
1057 lines (1026 loc) · 46.9 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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
options(shiny.maxRequestSize=30*1024^2) ## increase file upload to 30MB
shinyServer(function(input, output,session) {
## initiate trap type
trapType <- "single"
## read in input data
traps <- reactive({
if(input$example == TRUE){
if(input$trapType_ex== "single"){
load("shiny_example_traps.RData")
traps <- shiny_example_traps
traps
}else{
if(input$trapType_ex== "multi"){
load("shiny_multi_traps.RData")
traps <- shiny_multi_traps
traps
}
}
}else{
req(input$file1)
traps <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
validate(need("x" %in% names(traps) & "y" %in% names(traps) & "post" %in% names(traps),
"Trap file must contain columns named x, y, and post"))
validate(need(class(traps$post)=="integer", "Please give post ID as a whole number"))
validate(need(class(traps$x)%in%c("integer","numeric"), "Please ensure x coordinate is numeric"))
validate(need(class(traps$y)%in%c("integer","numeric"), "Please ensure y coordinate is numeric"))
return(traps)
}
})
detections <- reactive({
if("simple" %in% input$which_example & input$example == TRUE & input$trapType_ex== "single"){
load("shiny_example_detections.RData")
detections <- shiny_example_detections[,1:3]
disable("bearing_range")
hide("bearing_range")
return(detections)
}else{
if("bearings" %in% input$which_example & input$example == TRUE & input$trapType_ex== "single"){
load("shiny_example_detections.RData")
detections <- shiny_example_detections[,1:4]
enable("bearing_range")
shinyjs::show("bearing_range")
return(detections)
}else{
if("distance" %in% input$which_example & input$example == TRUE & input$trapType_ex == "single"){
load("shiny_example_detections.RData")
detections <- shiny_example_detections[,-4]
disable("bearing_range")
hide("bearing_range")
return(detections)
}else{
if("bd" %in% input$which_example & input$example == TRUE & input$trapType_ex == "single"){
load("shiny_example_detections.RData")
detections <- shiny_example_detections
enable("bearing_range")
shinyjs::show("bearing_range")
return(detections)
}else{
if("simple" %in% input$which_example_multi& input$example == TRUE & input$trapType_ex== "multi"){
load("shiny_multi_detections.RData")
detections <- shiny_multi_detections[,1:4]
disable("bearing_range")
hide("bearing_range")
return(detections)
}else{
if("bearings" %in% input$which_example_multi & input$example == TRUE & input$trapType_ex== "multi"){
load("shiny_multi_detections.RData")
detections <- shiny_multi_detections[,1:5]
enable("bearing_range")
shinyjs::show("bearing_range")
return(detections)
}else{
req(input$file2)
detections <- read.csv(input$file2$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
validate(need("occasion" %in% names(detections) & "group" %in% names(detections) &
"post" %in% names(detections),
"Detections file must contain columns named occasion, group, and post"))
validate(need(class(detections$group) == "integer", "Please give group ID as a whole number"))
validate(need(class(detections$occasion) == "integer", "Please give occasion ID as a whole number"))
validate(need(class(detections$post) == "integer", "Please give post ID as a whole number"))
if("bearing" %in% names(detections)){
validate(need(class(detections$bearing) == "numeric" | class(detections$bearing) == "integer", "Please make sure all bearings are numeric"))
enable("bearing_range")
shinyjs::show("bearing_range")
}else{
disable("bearing_range")
hide("bearing_range")
}
if("distance" %in% names(detections)){
validate(need(class(detections$distance) == "numeric" | class(detections$distance) == "integer" , "Please make sure all distances are numeric"))
}
return(detections)
}
}
}
}
}
}
})
## single/multi variable
trapType <- reactive({
if("array"%in%names(detections()) & "array"%in%names(traps()) & input$trapType_ex == "multi"){
trapType <- "multi"
}else{
if( "array"%in%names(detections()) & "array"%in%names(traps()) & input$trapType_us == "multi"){
trapType <- "multi"
}else{
trapType <- "single"
}
}
})
## covariates
covariates <- reactive({
if("yes"%in%input$example_covariates){
lst <- list(covariate = raster::raster("example_raster.tif"))
}else{
req(input$covs)
files <- input$covs
lst <- list()
for(i in 1:length(files[,1])){
lst[[i]] <- raster::raster(files[[i,"datapath"]])
}
names(lst) <- unlist(strsplit(files[,1],".tif"))
}
lst
})
output$cov.list <- renderPlot({
req(covariates())
covariates <- covariates()
covs <- lapply(covariates,function(x) gplot(x) +
geom_tile(aes(fill = value)) +
xlab("x-axis") + ylab("y-axis") +
theme(panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1)))
for(i in 1:length(covariates)){covs[[i]] <- covs[[i]] + ggtitle(names(covariates)[i])}
do.call(grid.arrange,covs)
})
## covariate buttons
output$covariate_controls <- renderUI({
req(covariates())
covariates <- covariates()
checkboxGroupInput("covariate.choose", "Choose covariates to include in your model",
names(covariates))
})
## covariate factor buttons
output$cov_factor <- renderUI({
req(covariates())
req(input$covariate.choose)
which.covariates <- input$covariate.choose
covariates <- covariates()
covariates <- covariates[which.covariates]
checkboxGroupInput("covariate.factor", "Which covariates are factor covariates (tick for yes)",
names(covariates))
})
cov.use <- reactive({
req(covariates())
req(input$covariate.choose)
req(mask())
mask <- mask()
if(class(mask) == "list"){
msk.locs <- lapply(mask, function(x) cbind(x[,1],x[,2]))
which.covariates <- input$covariate.choose
covariates <- covariates()
covariates <- covariates[which.covariates]
if(names(covariates) %in% input$covariate.factor){
covariates[[input$covariate.factor]] <- as.factor(covariates[[input$covariate.factor]])
levels(covariates[[input$covariate.factor]])[[1]]$category <- as.factor(levels(covariates[[input$covariate.factor]])[[1]]$ID)
}
covariates.use <- list()
for(i in 1:length(msk.locs)){
tmp <- lapply(covariates,function(x) if(is.factor(x)){
factorValues(x,extract(x,msk.locs[[i]]))
}else{
data.frame(extract(x,msk.locs[[i]]))
}
)
covariates.use[[i]] <- as.data.frame(tmp)
names(covariates.use[[i]]) <- names(covariates)
}
return(covariates.use)
}else{
msk.locs <- cbind(mask[,1],mask[,2])
which.covariates <- input$covariate.choose
covariates <- covariates()
covariates <- covariates[which.covariates]
covariates.use <- lapply(covariates, function(x) if(is.factor(x)){
factorValues(x,extract(x,msk.locs))
}else{
extract(x,msk.locs)
}
)
names(covariates.use) <- names(covariates)
return(as.data.frame(covariates.use))
}
})
## which array raw
output$which_array_raw <- renderUI({
detections <- detections()
traps <- traps()
validate(need(!is.null(traps),""))
validate(need(!is.null(detections),""))
validate(need("array"%in%names(detections),""))
validate(need("array"%in%names(traps),""))
validate(need(trapType() == "multi",""))
arrs <- as.numeric(names(table(traps$array)))
mn <- min(arrs)
mx <- max(arrs)
numericInput("choose_trap_raw", "Choose trap array for plot",
min = mn, max = mx,
value = mn)
})
## which array capt
output$which_array_capt <- renderUI({
detections <- detections()
traps <- traps()
validate(need(!is.null(traps),""))
validate(need(!is.null(detections),""))
validate(need("array"%in%names(detections),""))
validate(need("array"%in%names(traps),""))
validate(need(trapType() == "multi",""))
arrs <- as.numeric(names(table(traps$array)))
mn <- min(arrs)
mx <- max(arrs)
numericInput("choose_trap_capt", "Choose trap array for capture history matrix",
min = mn, max = mx,
value = mn)
})
## which array output plots
output$which_array <- renderUI({
detections <- detections()
traps <- traps()
validate(need(!is.null(traps),""))
validate(need(!is.null(detections),""))
validate(need("array"%in%names(detections),""))
validate(need("array"%in%names(traps),""))
validate(need(trapType() == "multi",""))
## validate(need(length(table(traps$array))==length(table(detections$array)),"Need equal number of arrays in detection file as in trap file"))
arrs <- as.numeric(names(table(traps$array)))
mn <- min(arrs)
mx <- max(arrs)
numericInput("choose_trap", "Choose trap array for output plots",
min = mn, max = mx,
value = mn)
})
## Clunky way of enabling/disabling buttons
observe({
if(input$example == TRUE){
disable("file1")
disable("file2")
disable("header")
disable("sep")
disable("quote")
hide("file1")
hide("file2")
hide("header")
hide("sep")
hide("quote")
enable("example_covariates")
shinyjs::show("example_covariates")
}else{
enable("file1")
enable("file2")
enable("header")
enable("sep")
enable("quote")
shinyjs::show("file1")
shinyjs::show("file2")
shinyjs::show("header")
shinyjs::show("sep")
shinyjs::show("quote")
disable("which_example_multi")
shinyjs::hide("which_example_multi")
disable("which_example")
shinyjs::hide("which_example")
disable("example_covariates")
shinyjs::hide("example_covariates")
}
if(trapType() == "single" & input$example == TRUE){
enable("which_example")
enable("example_covariates")
shinyjs::show("example_covariates")
shinyjs::show("which_example")
disable("which_example_multi")
shinyjs::hide("which_example_multi")
}
if(trapType() == "multi" & input$example == TRUE){
disable("example_covariates")
shinyjs::hide("example_covariates")
disable("which_example")
shinyjs::hide("which_example")
enable("which_example_multi")
shinyjs::show("which_example_multi")
}
## initislly disable some options if no model fitted
observeEvent(!input$fit,{
disable("downloadSurfPlot")
disable("downloadContPlot")
disable("downloadDetPlot")
disable("call.num")
disable("reset_locplot")
disable("distD")
disable("downloadbearingPlot")
disable("downloaddistancePlot")
disable("anispeed")
disable("report")
})
observeEvent(input$fit,{
enable("downloadSurfPlot")
enable("downloadContPlot")
enable("downloadDetPlot")
enable("call.num")
enable("reset_locplot")
enable("distD")
enable("downloadbearingPlot")
enable("downloaddistancePlot")
enable("anispeed")
enable("report")
})
if(input$example == FALSE | isTruthy(input$file1) == FALSE){
disable("downloadMask")
disable("buffer")
disable("spacing")
}
if(isTruthy(input$file1) == TRUE | input$example == TRUE){
enable("downloadMask")
enable("buffer")
enable("spacing")
}
if(input$example == FALSE | isTruthy(input$file1) == FALSE & isTruthy(input$file2) == FALSE){
disable("select")
disable("fixedParamSelection")
disable("fit")
hide("bearing_range")
}
if(input$example == TRUE | isTruthy(input$file1) == TRUE & isTruthy(input$file2) == TRUE){
enable("select")
enable("fixedParamSelection")
enable("fit")
shinyjs::show("bearing_range")
}
})
## output trap locations
output$traps <- renderTable({
traps <- traps()
if(input$disp == "head") {
return(head(traps))
}else{
return(traps)
}
},
striped = TRUE)
## code to plot trap locations
output$trapsPlot <- renderPlot({
traps <- traps()
if(!is.null(traps$post)){
plot(traps$x,traps$y,asp = 1,type = "n",xlab = "x-axis",ylab = "y-axis")
text(traps$x,traps$y,traps$post,lwd = 2)
}else{
plot(traps$x,traps$y,asp = 1,pch = 4,cex = 2,lwd = 3,xlab = "x-axis",ylab = "y-axis")
}
})
output$detections <- renderTable({
detections <- detections()
if(input$disp == "head") {
return(head(detections))
}else{
return(detections)
}
},
striped = TRUE)
capthist <- reactive({
detections <- detections()
traps <- traps()
validate(need(!is.null(traps),""))
validate(need(!is.null(detections),""))
if("bearing" %in% names(detections)){
if(input$bearing_range == "rad"){
validate(need(min(detections$bearing) >= 0 & max(detections$bearing) <= 2*pi,
"Your bearing measurements are outside the range of radians,
please indicate correct bearing measurement in the sidebar."))
}else{
if(input$bearing_range == "bd"){
validate(need(max(detections$bearing) > 2*pi,"Looks like your bearing measurements
are already in radians, please indicate correct bearing measurement in the sidebar. "))
detections$bearing <- (pi/180)*detections$bearing
}
}
}
if(trapType() == "multi"){
traps <- split(traps, traps$array)
capt.hist <- get.capt.hist(detections, traps = traps)
for(i in 1:length(capt.hist)){
colnames(capt.hist[[i]][[1]]) <- paste(1:ncol(capt.hist[[i]][[1]]))
n.row <- nrow(capt.hist[[i]][[1]])
if(n.row != 0 ){
oc <- detections$occasion[detections$array == i]
grp <- detections$group[detections$array == i]
rownames(capt.hist[[i]][[1]]) <- unique(paste("occasion",oc, "group",
grp))
}
}
}else{
validate(need(length(table(traps$array))==length(table(detections$array)),
"Need equal number of arrays in detection file as in trap file"))
validate(need(trapType() == "single",""))
capt.hist <- get.capt.hist(detections, traps = traps)
colnames(capt.hist[[1]]) <- paste(1:ncol(capt.hist[[1]]))
rownames(capt.hist[[1]]) <- unique(paste("occasion",detections$occasion, "group", detections$group))
}
return(capt.hist)
})
output$capt.hist <- renderTable({
capthist <- capthist()
traps <- traps()
validate(need(!is.null(capthist),""))
if(trapType() == "single"){
if(input$disp == "head") {
return(head(capthist[[1]]))
}else{
return(capthist[[1]])
}
}else{
if(trapType() == "multi"){
if(input$disp == "head") {
validate(need(input$choose_trap <= max(as.numeric(names(table(traps$array)))),"Please provide valid array"))
return(head(capthist[[input$choose_trap_capt]][[1]]))
}else{
validate(need(input$choose_trap <= max(as.numeric(names(table(traps$array)))),"Please provide valid array"))
try(capthist[[input$choose_trap_capt]][[1]],silent = TRUE)
}
}
}
},striped = TRUE,rownames = TRUE,colnames = TRUE,digits = 0)
## chage buffer slider based on trap range
## chage spacing slider based on trap range
observe({
if(!is.null(traps())) {
traps <- traps()
detections <- detections()
if(trapType() == "single"){
maxdistance <- diff(range(traps$x,traps$y))/4
updateNumericInput(session, "spacing", max = maxdistance, value = maxdistance/2)
maxdistance <- 4*diff(range(traps$x,traps$y))
updateNumericInput(session, "buffer", max = maxdistance,value = maxdistance/2)
}else{
validate(need("array"%in%names(detections),""))
validate(need("array"%in%names(traps),""))
traps <- split(traps, traps$array)
maxdistance <- diff(range(traps[[1]]$x,traps[[1]]$y))/4
updateNumericInput(session, "spacing", max = maxdistance, value = maxdistance/2)
maxdistance <- 4*diff(range(traps[[1]]$x,traps[[1]]$y))
updateNumericInput(session, "buffer", max = maxdistance,value = maxdistance/2)
}
}
})
## show all plot for raw data
output$show <- renderPlot({
traps <- traps()
capt.hist <- capthist()
if(trapType() == "single"){
validate(need(input$show.call.num,"Please provide a call number"))
validate(need(input$show.call.num <= nrow(capt.hist$bincapt),"Please provide a valid call number"))
show.data(traps, capt.hist,id = input$show.call.num)
legend("top",legend = paste("call",input$show.call.num,sep = " "),bty = "n")
}else{
validate(need(trapType() == "multi",""))
traps <- split(traps, traps$array)
validate(need(input$show.call.num,"Please provide a call number"))
try(show.data(traps[[input$choose_trap_raw]], capt.hist[[input$choose_trap_raw]],id = input$show.call.num),silent = TRUE)
legend("top",legend = paste("array", input$choose_trap_raw, " call",input$show.call.num,sep = " "),bty = "n")
}
})
## change buffer sliding in advanced increase buffer option chosen
observe({
if("inc" %in% input$advancedOptions) {
maxdistance <- input$incmaskbuffer
updateSliderInput(session, "buffer", max = maxdistance,value = maxdistance/2)
}
})
## plot of mask
mask <- eventReactive(input$msk,{
shinyjs::show("processing_msk") ## stuff to disable fitting button
traps <- traps()
validate(need(!is.null(traps),""))
validate(need(input$buffer > input$spacing,"The mask buffer cannot be less than the spacing"))
validate(need(input$buffer/input$spacing < 80, "Infeasibly fine mask"))
if(trapType() == "single"){
traps <- as.matrix(cbind(traps$x,traps$y))
mask <- create.mask(traps,input$buffer,input$spacing)
}else{
traps <- split(traps, traps$array)
traps <- lapply(traps,function(x) cbind(x$x,x$y))
mask <- lapply(traps,create.mask,buffer = input$buffer,spacing = input$spacing)
}
hide("processing_msk")
enable("fit")
return(mask)
})
output$maskPlot <- renderPlot({
traps <- traps()
mask <- mask()
if(trapType() == "single"){
grid.arrange(show.mask(mask,traps))
}else{
traps <- split(traps, traps$array)
validate(need(is.list(mask), ""))
m.lst <- list()
for(i in 1:length(traps)){ m.lst[[i]] <- show.mask(mask[[i]], traps = traps[[i]])}
do.call(grid.arrange, m.lst)
}
})
## print out mask buffer info
output$maskinfo <- renderText({
validate(need(!is.null(mask()),""))
paste("This mask is assuming that a distance of ",input$buffer,
"meters is the maximum distance at which a detection is feasibly possible")
})
## choose which parameters of which detection function to fit, conditional numeric input for fixing param values
output$fixedParamSelection <- renderUI({
params.fix <- cbind(c("g0","sigma","g0","sigma","z","lambda0","sigma","shape","scale"),
c("hn","hn","hr","hr","hr","hhn","hhn","th","th"))
checkboxGroupInput("parameter", "Fix which parameters:",
choices = as.character(params.fix[params.fix[,2] == input$select,1]),inline = TRUE)
})
output$fixedg0 <- renderUI({
conditionalPanel(condition = "input.parameter.includes('g0')",
numericInput("g0","fix g0 to:",value = 1,min = 1,max = 100,step = 1)
)
})
output$fixedsigma <- renderUI({
conditionalPanel(condition = "input.parameter.includes('sigma')",
numericInput("sigma","fix sigma to:",value = 1,min = 1,max = 100,step = 1)
)
})
output$fixedz <- renderUI({
conditionalPanel(condition = "input.parameter.includes('z')",
numericInput("z","fix z to:",value = 1,min = 1,max = 100,step = 1)
)
})
output$fixedlambda0 <- renderUI({
conditionalPanel(condition = "input.parameter.includes('lambda0')",
numericInput("lambda0","fix lambda0 to:",value = 1,min = 1,max = 100,step = 1)
)
})
output$fixedshape <- renderUI({
conditionalPanel(condition = "input.parameter.includes('shape')",
numericInput("shape","fix shape to:",value = 1,min = 1,max = 100,step = 1)
)
})
output$startParamSelection <- renderUI({
params.fix <- cbind(c("g0","sigma","g0","sigma","z","lambda0","sigma","shape","scale"),
c("hn","hn","hr","hr","hr","hhn","hhn","th","th"))
checkboxGroupInput("parset", "Set starting values for which parameters:",
choices = as.character(params.fix[params.fix[,2] == input$select,1]),inline = TRUE)
})
output$svg0 <- renderUI({
conditionalPanel(condition = "input.parset.includes('g0') && !input.parameter.includes('g0')",
numericInput("svg0","g0 start value:",value = 1,min = 1,max = 100,step = 1)
)
}) ## set starting value of g0 ensure it isn't already fixed
output$svsigma <- renderUI({
conditionalPanel(condition = "input.parset.includes('sigma') && !input.parameter.includes('sigma')",
numericInput("svsigma","sigma start value:",value = 1,min = 1,max = 100,step = 1)
)
}) ## set starting value of sigma ensure it isn't already fixed
output$svz <- renderUI({
conditionalPanel(condition = "input.parset.includes('z') && !input.parameter.includes('z')",
numericInput("svz","z start value:",value = 1,min = 1,max = 100,step = 1)
)
}) ## set starting value of z ensure it isn't already fixed
output$svlambda0 <- renderUI({
conditionalPanel(condition = "input.parset.includes('lambda0') && !input.parameter.includes('lambda0')",
numericInput("svlambda0","lambda0 start value:",value = 1,min = 1,max = 100,step = 1)
)
}) ## set starting value of lambda0 ensure it isn't already fixed
output$svshape <- renderUI({
conditionalPanel(condition = "input.parset.includes('shape') && !input.parameter.includes('shape')",
numericInput("svshape","shape start value:",value = 1,min = 1,max = 100,step = 1)
)
}) ## set starting value of shape ensure it isn't already fixed
output$svscale <- renderUI({
conditionalPanel(condition = "input.parset.includes('scale') && !input.parameter.includes('scale')",
numericInput("svscale","scale start value:",value = 1,min = 1,max = 100,step = 1)
)
}) ## set starting value of scale ensure it isn't already fixed
output$svshape.1 <- renderUI({
conditionalPanel(condition = "input.parset.includes('shape.1') && !input.parameter.includes('shape.1')",
numericInput("svshape.1","shape.1 start value:",value = 1,min = 1,max = 100,step = 1)
)
}) ## set starting value of shape.1 ensure it isn't already fixed
output$svshape.2 <- renderUI({
conditionalPanel(condition = "input.parset.includes('shape.2') && !input.parameter.includes('shape.2')",
numericInput("svshape.2","shape.2 start value:",value = 1,min = 1,max = 100,step = 1)
)
}) ## set starting value of shape.2 ensure it isn't already fixed
## Fit model based on inputs of user and output parameter estimates and plots
fit <- eventReactive(input$fit,{
detections <- detections()
traps <- traps()
if(trapType() == "single"){
traps <- as.matrix(cbind(traps$x,traps$y))
}else{
traps <- split(traps, traps$array)
traps <- lapply(traps,function(x) cbind(x$x,x$y))
}
mask <- mask()
validate(need(!is.null(mask), "Please construct mask"))
nms <- names(detections)
capt.hist <- capthist()
validate(need(!is.null(capt.hist), "No capture hstory information"))
if(trapType() == "multi"){
validate(need(length(capt.hist) == length(mask), "Please construct mask for your loaded traps"))
}
## fixed values
param.fix <- input$parameter
param.fix.value <- list(g0 = input$g0,sigma = input$sigma,z = input$z,lambda0 = input$lambda0,shape = input$shape,
scale = input$scale, shape.1 = input$shape.1,shape.2 = input$shape.2)
idx <- match(param.fix,names(param.fix.value))
fix <- param.fix.value[idx]
## starting values
param.sv <- input$parset
param.sv.value <- list(g0 = input$svg0,sigma = input$svsigma,z = input$svz,lambda0 = input$svlambda0,
svshape = input$svshape,
scale = input$svscale, shape.1 = input$svshape.1,shape.2 = input$svshape.2)
idsv <- match(param.sv,names(param.sv.value))
sv <- param.sv.value[idsv]
fit <- NULL
disable("downloadSurfPlot")
disable("downloadContPlot")
disable("downloadDetPlot")
disable("downloadbearingPlot")
disable("downloaddistancePlot")
disable("downloadMask")
disable("downloadModel")
disable("anispeed")
disable("report")
disable("fit")
disable("side-panel")
shinyjs::show("processing") ## stuff to disable fitting button
if(!is.null(input$covariate.choose)){
ihd <- list(model = as.formula(paste("~",
paste(input$covariate.choose,
collapse = " + ", sep = " "))),
covariates = cov.use())
fit <- fit.ascr(capt = capt.hist,traps = traps,mask = mask,detfn = input$select,
fix = fix, sv = sv,trace = TRUE, ihd.opts = ihd)
}else{
fit <- fit.ascr(capt = capt.hist,traps = traps,mask = mask,detfn = input$select,
fix = fix, sv = sv,trace = TRUE)
}
enable("fit")
enable("side-panel")
enable("downloadSurfPlot")
enable("downloadContPlot")
enable("downloadDetPlot")
enable("downloadbearingPlot")
enable("downloaddistancePlot")
enable("anispeed")
enable("report")
enable("downloadMask")
enable("downloadModel")
hide("processing")
return(fit)
})
## coefficients
output$coefs <- renderTable({
fit <- fit()
if(class(fit)[1]=="ascr"){
ci <- confint(fit)
res <- data.frame(Estimate = summary(fit)$coefs,Std.Error = summary(fit)$coefs.se, "2.5%" = ci[,1], "97.5%" = ci[,2] )
if(res$Estimate[1] < 0.01){res <- matrix(apply(res,1,formatC, format = "e",digits = 2),ncol = 4, byrow = TRUE)}
rownames(res) <- names(coef(fit))
colnames(res) <- c("Estimate", "Std.Error", "2.5% Cl", "97.5% Cl")
return(res)
}
},rownames = TRUE,digits = 3)
## AIC and log Likelihood
output$AIClL <- renderTable({
fit <- fit()
if(class(fit)[1]=="ascr"){
tab <- rbind(AIC = AIC(fit),logLik = fit$loglik)
colnames(tab) <- "value"
return(tab)
}
},rownames = TRUE)
## Detection function plots and location estimate plots
output$detectionsurf <- renderPlot({
fit <- fit()
if(class(fit)[1] == "ascr"){
par(mfrow = c(1,2))
show.detsurf(fit,session = input$choose_trap)
show.detsurf(fit,session = input$choose_trap, surface = FALSE)
}else{
plot(1,1,col="white",axes = FALSE,xlab = "",ylab = "")
text(1,1,paste("convergence issues try advanced options"),col = "grey")
}
})
output$detfn <- renderPlot({
fit <- fit()
if(class(fit)[1]=="ascr"){
detfn <- fit$args$detfn
pars <- get.par(fit, pars = fit$detpars, cutoff = fit$fit.types["ss"],as.list = TRUE)
buffer <- attr(get.mask(fit,session = input$choose_trap), "buffer")
probs <- ascr:::calc.detfn(buffer, detfn = detfn, pars = pars,ss.link =fit$args$ss.opts$ss.link)
ascr:::show.detfn(fit)
if(probs >= 0.1){
legend("center", bty = "n",paste("The detection probability at the mask buffer of ", buffer, "m is", round(probs,3), "(i.e., non-zero), perhaps increase mask buffer."),cex = 0.7,text.col = "red")
}
}else{
plot(1,1,col="white",axes = FALSE,xlab = "",ylab = "")
text(1,1,paste("convergence issues try advanced options"),col = "grey")
}
})
## When a double-click happens, check if there's a brush on the plot.
## If so, zoom to the brush bounds; if not, reset the zoom.
ranges <- reactiveValues(x = NULL, y = NULL)
observeEvent(input$locsplot_dblclick, {
brush <- input$locsplot_brush
if (!is.null(brush)) {
ranges$x <- c(brush$xmin, brush$xmax)
ranges$y <- c(brush$ymin, brush$ymax)
} else {
ranges$x <- NULL
ranges$y <- NULL
}
})
observeEvent(input$reset_locplot,{
ranges$x <- NULL
ranges$y <- NULL
})
output$locs <- renderPlot({
fit <- fit()
mask <- mask()
if(trapType() == "single"){
msk <- mask()
}else{
msk <- mask[[input$choose_trap]]
}
if(class(fit)[1]=="ascr"){
validate(need(input$call.num,"Please provide a call number"))
validate(need(input$call.num <= nrow(fit$args$capt[[1]]$bincapt),"Please provide a valid call number"))
if("fine" %in% input$advancedOptions & is.null(ranges$x)){
validate(need(input$plotmaskspacing,
"Please provide a spacing for the (plotting) mask or uncheck this option"))
validate(need(input$plotmaskspacing > 0,
"Cannnot have a spacing of zero meters"))
validate(need(input$buffer > input$plotmaskspacing,
"The mask buffer cannot be less than the (plotting) mask spacing"))
validate(need(input$plotmaskspacing < input$spacing,
"To obtain a smooth plot the (plotting) mask spacing should be finer than the model fit mask "))
locations(fit,input$call.num,mask = msk)
legend("top",legend = paste("call",input$call.num,sep = " "),bty = "n")
}else{
if(!is.null(ranges$x) & !("fine" %in% input$advancedOptions)){
locations(fit,input$call.num,xlim = ranges$x, ylim = ranges$y)
legend("top",legend = paste("call",input$call.num,sep = " "),bty = "n")
}else{
if("fine" %in% input$advancedOptions & !is.null(ranges$x)){
validate(need(input$plotmaskspacing,
"Please provide a spacing for the (plotting) mask or uncheck this option"))
validate(need(input$plotmaskspacing > 0,
"Cannnot have a spacing of zero meters"))
validate(need(input$buffer > input$plotmaskspacing,
"The mask buffer cannot be less than the (plotting) mask spacing"))
validate(need(input$plotmaskspacing < input$spacing,
"To obtain a smooth plot the (plotting) mask spacing should be finer than the model fit mask "))
if(trapType() == "single"){
locations(fit,input$call.num,mask = msk,xlim = ranges$x, ylim = ranges$y)
}else{
locations(fit,session = input$choose_trap,input$call.num,mask = msk,xlim = ranges$x, ylim = ranges$y)
title(main = paste("array",input$choose_trap))
}
legend("top",legend = paste("call",input$call.num,sep = " "),bty = "n")
}else{
if(trapType() == "single"){
locations(fit, input$call.num)
}else{
locations(fit,session = input$choose_trap, input$call.num)
title(main = paste("array",input$choose_trap))
}
legend("top",legend = paste("call",input$call.num,sep = " "),bty = "n")
}
}
}
}else{
plot(1,1,col="white",axes = FALSE,xlab = "",ylab = "")
text(1,1,paste("Convergence issues try advanced options"),col = "grey")
}
},width = 700,height = 700)
## Measurement error plots
output$bearing_pdf <- renderPlot({
fit <- fit()
validate(need(!is.null(fit$args$capt[[1]]$bearing),"No bearing data provided"))
kappa = fit$coefficients["kappa"]
if(trapType() == "single"){
theta = sort(fit$args$capt[[1]]$bearing - pi)
theta = seq(min(theta), max(theta), length.out = 1000)
show.dvm(theta = theta, kappa = kappa)
}else{
theta = sort(fit$args$capt[[input$choose_trap]]$bearing - pi)
theta = seq(min(theta), max(theta), length.out = 1000)
show.dvm(theta = theta, kappa = kappa)
title(paste("array", input$choose_trap))
}
})
output$distance_pdf <- renderPlot({
fit <- fit()
validate(need(!is.null(fit$args$capt[[1]]$dist),"No distance data provided"))
validate(need(!(input$distD == 0), "Distance cannot be zero"))
validate(need(!is.null(input$distD), "Please provide distance for measurement error distribution"))
validate(need(input$distD < max(fit$args$capt[[1]]$dist),"Distance cannot be greater than those observed"))
d <- input$distD
shape <- fit$coefficients["alpha"]
if(trapType() == "single"){
x <- sort(fit$args$capt[[1]]$dist)
show.distgam(x = x, shape = shape, d = d)
}else{
x <- sort(fit$args$capt[[input$choose_trap]]$dist)
show.distgam(x = x, shape = shape, d = d)
title(paste("array", input$choose_trap))
}
})
## inhomogeneous density plot
output$density_surface <- renderPlot({
req(!is.null(input$covariate.choose))
show.Dsurf(fit(), session = input$choose_trap)
title(main = paste("array",input$choose_trap))
})
## Downloads
output$downloaddensity_surfPlot<- downloadHandler(
filename = "ascr_density_surface.png",
content = function(file) {
req(!is.null(input$covariate.choose))
png(file)
show.Dsurf(fit(), session = input$choose_trap)
title(main = paste("array",input$choose_trap))
dev.off()
})
output$downloadMask <- downloadHandler(
filename = "ascrMask.png",
content = function(file) {
png(file)
traps <- traps()
mask <- mask()
if(trapType() == "single"){
show.mask(mask,traps)
}else{
traps <- split(traps, traps$array)
m.lst <- list()
for(i in 1:length(traps)){ m.lst[[i]] <- show.mask(mask[[i]], traps = traps[[i]])}
do.call(grid.arrange, m.lst)
}
dev.off()
})
output$downloadSurfPlot <- downloadHandler(
filename = "ascr_detection_surface_plot.png",
content = function(file) {
png(file)
fit <- fit()
if(class(fit)[1] == "ascr"){
par(mfrow = c(1,2))
show.detsurf(fit,session = input$choose_trap)
show.detsurf(fit,,session = input$choose_trap, surface = TRUE)
}else{
plot(1,1,col="white",axes = FALSE,xlab = "",ylab = "")
text(1,1,paste("convergence issues try advanced options"),col = "grey")
}
dev.off()
})
output$downloadContPlot <- downloadHandler(
filename = "ascr_detection_contour_plot.png",
content = function(file) {
png(file)
fit <- fit()
if(class(fit)[1] == "ascr"){
par(mfrow = c(1,2))
show.detsurf(fit,session = input$choose_trap)
show.detsurf(fit,,session = input$choose_trap, surface = FALSE)
}else{
plot(1,1,col="white",axes = FALSE,xlab = "",ylab = "")
text(1,1,paste("convergence issues try advanced options"),col = "grey")
}
dev.off()
})
output$downloadDetPlot <- downloadHandler(
filename = "ascr_detection_function_plot.png",
content = function(file) {
png(file)
fit <- fit()
if(class(fit)[1]=="ascr"){
ascr:::show.detfn(fit)
}else{
NULL
}
dev.off()
})
## deal with bearing and distance plots
observe({
fit <- fit()
if(is.null(fit$args$capt[[1]]$dist)){
disable("downloaddistancePlot")
disable("distD")
}else{
enable("downloaddistancePlot")
enable("distD")
}
if(is.null(fit$args$capt[[1]]$bearing)){
disable("downloadbearingPlot")
}else{
enable("downloadbearingPlot")
}
})
output$downloadbearingPlot <- downloadHandler(
filename = "ascr_bearing_distribution_plot.png",
content = function(file) {
fit <- fit()
validate(need(!is.null(fit$args$capt[[1]]$bearing),"No bearing data provided"))
png(file)
if(class(fit)[1]=="ascr"){
kappa = fit$coefficients["kappa"]
if(trapType() == "single"){
theta = sort(fit$args$capt[[1]]$bearing - pi)
show.dvm(theta = theta, kappa = kappa)
}else{
theta = sort(fit$args$capt[[input$choose_trap]]$bearing - pi)
show.dvm(theta = theta, kappa = kappa)
title(paste("array", input$choose_trap))
}
}else{
NULL
}
dev.off()
})
output$downloaddistancePlot <- downloadHandler(
filename = "ascr_distance_distribution_plot.png",
content = function(file) {
fit <- fit()
validate(need(!is.null(fit$args$capt[[1]]$dist),"No distance data provided"))
png(file)
if(class(fit)[1]=="ascr"){
d <- input$distD
shape <- fit$coefficients["alpha"]
if(trapType() == "single"){
x <- sort(fit$args$capt[[1]]$dist)
show.distgam(x = x, shape = shape, d = d)
}else{
x <- sort(fit$args$capt[[input$choose_trap]]$dist)
show.distgam(x = x, shape = shape, d = d)
title(paste("array", input$choose_trap))
}
}else{
NULL
}
dev.off()
})
output$downloadModel <- downloadHandler(
filename = paste("ascr_",date(),".RData",sep = ""),
content = function(file){
fit <- fit()
save(fit,file = file)
}
)
output$report <- downloadHandler(
filename = "animation.html",