-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.R
More file actions
2276 lines (2175 loc) · 71.7 KB
/
app.R
File metadata and controls
2276 lines (2175 loc) · 71.7 KB
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
library(shiny)
library(shinydashboard)
library(amt)
library(rhandsontable)
library(leaflet)
# Example data
# Tracking data
fisher_ny <- readr::read_csv("data/Martes pennanti LaPoint New York.csv")
# Subset relevant columns
fisher_ny <- fisher_ny %>% select(
longitude = "location-long", latitude = "location-lat", "timestamp",
id = "individual-local-identifier"
)
# Rename columns containing special characters e.g. "-"
names(fisher_ny) <- make.names(names(fisher_ny), unique = TRUE)
# Environmental data
land_use_fisher_ny <- raster::raster("data/landuse_study_area.tif")
# Rename TIF for usage in model building
names(land_use_fisher_ny) <- "land_use"
# EPSG Codes
epsg_data <- rgdal::make_EPSG()
# UI ----------------------------------------------------------------------
# Header --------------------------------------------------------
ui <- dashboardPage(skin = "green",
# Title of browser tab
title = "amtGUI",
dashboardHeader(
title = "amtGUI",
# Set width in pixels
titleWidth = 180
),
# Sidebar -----------------------------------------------------------------
dashboardSidebar(
# Set width in pixels
width = 180,
sidebarMenu(
menuItem("Data Upload", tabName = "data", icon = icon("file-upload")),
menuItem("Upload Map", tabName = "map", icon = icon("upload")),
menuItem("Configure Analysis", tabName = "configure", icon = icon("database"),
menuSubItem("Create Track", tabName = "track", icon = icon("map-marked-alt")),
menuSubItem("Add Covariates", tabName = "covariates", icon = icon("plus-square"))),
menuItem("Modeling", tabName = "model", icon = icon("table")),
menuItem("Interactive Map", tabName = "plot", icon = icon("globe-americas")),
menuItem("Help", tabName = "help", icon = icon("question-circle"))
)),
# Body ----------------------------------------------------------
dashboardBody(
tabItems(
# Data Upload Tab ---------------------------------------------------------
tabItem(
tabName = "data",
# Align inputs and buttons (applies to all tabItems)
tags$head(
tags$style(
HTML(
".form-group {margin-bottom: 0 !important;}"
)
)
),
fluidRow(
column(
width = 12,
h4("Upload Tracking Data and Assign Corresponding EPSG Code"),
br()
)
),
fluidRow(
column(
width = 3,
# Example Datasets
selectInput(
inputId = "ex_data_csv",
label = "Load Example Data:",
choices = c("None", "Fisher NY (EPSG Code: 4326)")
),
# Input: Select EPSG Code
selectizeInput(
inputId = "epsg_csv",
label = "Assign EPSG Code:",
choices = c('', sort(na.omit(epsg_data$code))),
selected = '', # 4326 # Example data EPSG code
options = list(maxOptions = 10000)
)
),
column(
width = 3, offset = 1,
# Input: Select a file
fileInput(
inputId = "dataset_csv",
label = "Upload CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain", ".csv")
),
# Action button to reset input
actionButton('reset', 'Reset Input')
),
column(
width = 2,
# Input: Select separator
radioButtons(
inputId = "sep",
label = "Separator",
choices = c(Comma = ",", Semicolon = ";", Tab = "\t"),
selected = ","
),
# Input: Checkbox if file has header
checkboxInput(
inputId = "header",
label = "File has Header",
value = TRUE
)
),
column(
width = 2,
# Input: Select quotes
radioButtons(
inputId = "quote",
label = "Quote",
choices = c(None = "", "Double Quote" = '"',
"Single Quote" = "'"),
selected = '"'
)
)
),
br(),
fluidRow(
column(
width = 12,
# Tabs within fluid row
tabsetPanel(
tabPanel(
title = "Data Frame",
br(),
# Add horizontal scroll bar to data table
div(
style = 'overflow-x: scroll',
DT::dataTableOutput(outputId = "contents")
)
),
tabPanel(
title = "Column Summary",
br(),
verbatimTextOutput(outputId = "summary")
)
)
)
)
),
# Upload Map Tab ----------------------------------------------------------
tabItem(
tabName = "map",
fluidRow(
column(
width = 12,
h4(textOutput(outputId = "env_head")),
br()
)
),
fluidRow(
column(
width = 3,
# Example data set
uiOutput(outputId = "ex_data_env"),
# EPSG Code TIF
uiOutput(outputId = "epsg_env")
),
column(
width = 3, offset = 1,
# Input: Select a file
fileInput(
inputId = "dataset_env",
label = "Upload TIF File(s)",
multiple = TRUE,
# Restrict upload to TIF-Files only
accept = c(
"image/tif",
"image/tiff",
".tif",
".tiff"
)
),
# Action button to reset input
actionButton('reset_env', 'Reset Input')
)
),
fluidRow(
column(
width = 12,
br(),
br(),
# Headline
h4(textOutput(outputId = "epsg_head")),
# Sub headline (instructions)
h5(textOutput(outputId = "epsg_sub_head")),
br(),
DT::dataTableOutput(outputId = "contents_env")
)
)
),
# Track Creation Tab ------------------------------------------------------
tabItem(
tabName = "track",
fluidRow(
# Create a track
column(
width = 4,
h4(textOutput(outputId = "track_head")),
uiOutput(outputId = "x"),
uiOutput(outputId = "y"),
uiOutput(outputId = "ts")
),
# Transform EPSG Codes of CSV and TIF and select ID(s)
column(
width = 4,
br(),
br(),
uiOutput(outputId = "epsg_trk"),
uiOutput(outputId = "id"),
uiOutput(outputId = "id_trk")
),
# Resample track
column(
width = 4,
h4(textOutput(outputId = "resamp_head")),
# Increase distance between numeric Inputs
div(
style = "height: 75px;",
uiOutput(outputId = "rate_min")
),
div(
style = "height: 75px;",
uiOutput(outputId = "tol_min")
),
# Apply min height of selectInputs to dataRangeInput
tags$style(
type = 'text/css',
".input-daterange input {min-height: 34px; font-size: 14px;}"
),
uiOutput(outputId = 'fetch_dr')
)
),
br(),
fluidRow(
# Change color of tab titles from blue to green
tags$style(type = "text/css", "li a{color: #15AE57;}"),
column(
width = 12,
# Tabs within fluid row
tabsetPanel(
tabPanel(
title = "Track",
# Input: Select data table or summary of track
column(
width = 1,
br(),
br(),
uiOutput(outputId = "display_trk")
),
# Data table or summary
column(
width = 11,
DT::dataTableOutput(outputId = "contents_trk"),
verbatimTextOutput(outputId = "summary_trk")
)
),
tabPanel(
title = "Summary of Sampling Rate",
# Data table: summary of sampling rate
column(
width = 12,
# Headline
h4(textOutput(outputId = "samp_rate_head")),
DT::dataTableOutput(outputId = "summary_samp_rate")
)
)
)
)
)
),
# Add Additional Covariates Tab -------------------------------------------
tabItem(
tabName = "covariates",
fluidRow(
column(
width = 5,
# Headline
h4(textOutput(outputId = "min_burst_head")),
br(),
# Only retain bursts with a minimum number of relocations
uiOutput(outputId = "min_burst"),
br(),
# Headline
h4(textOutput(outputId = "bursts_head")),
# No. of bursts and observations remaining given minimum no.
# of relocations per burst
DT::dataTableOutput(outputId = "contents_bursts")
),
column(
width = 5, offset = 1,
# Headline
h4(textOutput(outputId = "env_info_head")),
# Sub headline (instructions)
h5(textOutput(outputId = "env_info_sub_head")),
# Environmental covariates data frame
rHandsontableOutput(outputId = "env_df"),
br(),
br(),
# Headline
h4(textOutput(outputId = "tod_head")),
# Sub headline (instructions)
h5(textOutput(outputId = "tod_sub_head")),
# Time of day
uiOutput(outputId = "tod"),
# Data frame Time of Day levels
DT::dataTableOutput(outputId = "contents_tod")
)
)
),
# Visualize Tab -----------------------------------------------------------
tabItem(
tabName = "plot",
fluidRow(
column(
width = 12,
# Headline
h4(textOutput(outputId = "visual_head"))
)
),
fluidRow(
column(
width = 12,
# Adjust height of map dynamically
tags$style(type = "text/css", "#mymap {height: calc(100vh - 110px)
!important;}"),
leafletOutput("mymap")
)
)
),
# Modeling Tab ------------------------------------------------------------
tabItem(
tabName = "model",
# Enable to clear all inputs of tab by clear button
shinyjs::useShinyjs(),
div(
id = "modeling_tab",
fluidRow(
column(
width = 4,
# Headline
h4(textOutput(outputId = "modeling_head")),
# Choose a model
uiOutput(outputId = "model")
),
column(
width = 3,
br(),
br(),
# Set number of random steps per relocation (ISSF)
uiOutput(outputId = "rand_stps"),
# Set number of random points (RSF)
uiOutput(outputId = "rand_points")
)
),
br(),
fluidRow(
column(
width = 4,
uiOutput(outputId = "mod_var")
),
column(
width = 3,
# Select no. of interaction terms to add
uiOutput(outputId = "inter_no")
)
),
br(),
fluidRow(
column(
width = 2,
# Select 1st interaction
uiOutput(outputId = "inter_1")
),
column(
width = 2,
# Select 2nd interaction
uiOutput(outputId = "inter_2")
),
column(
width = 2,
# Select 3rd interaction
uiOutput(outputId = "inter_3")
),
column(
width = 2,
# Select 4th interaction
uiOutput(outputId = "inter_4")
),
column(
width = 2,
# Select 5th interaction
uiOutput(outputId = "inter_5")
)
)
), # End of clear inputs
fluidRow(
column(
width = 12,
# Clear button
actionButton("clear_button", "Clear", icon = icon("times-circle")),
# Fit model button
actionButton("fit_button", "Fit Model", icon = icon("poll")),
# Download button for model estimates
downloadButton("downloadData", "Download Estimates"),
# Download button for user reports
downloadButton("report", "Download Report")
)
),
fluidRow(
column(
width = 5,
br(),
DT::dataTableOutput(outputId = "contents_mod")
)
)
),
# README ------------------------------------------------------------
tabItem(
tabName = "help",
fluidRow(
column(
width = 12,
includeHTML("help.html")
)
)
)
# End tabItems (insert a new tabItem above)
)
# End dashboardBody
)
# End UI
)
# Server ------------------------------------------------------------------
server <- function(input, output, session) {
# Data Upload -------------------------------------------------------------
# input$dataset_csv will be NULL initially. After the user selects
# and uploads a file its content is shown in a data frame by default
# or if selected a summary will be shown.
# Increase maximum upload size from 5 MB to 30 MB
options(shiny.maxRequestSize = 30*1024^2)
# Upload data and reset-button to switch between upload and example data set
values_csv <- reactiveValues(upload_state = NULL)
observeEvent(input$dataset_csv, {
values_csv$upload_state <- 'uploaded'
})
observeEvent(input$reset, {
values_csv$upload_state <- 'reset'
})
csvInput <- reactive({
# No upload
if (is.null(values_csv$upload_state)){
switch (input$ex_data_csv,
"Fisher NY (EPSG Code: 4326)" = fisher_ny,
"None" = return()
)
} else if (values_csv$upload_state == 'uploaded') {
# File uploaded
# Comma separated
if (input$sep == ",") {
csv_uploaded <- readr::read_csv(file = input$dataset_csv$datapath,
col_names = input$header,
quote = input$quote)
# Rename columns containing special characters e.g. "-"
names(csv_uploaded) <- make.names(names(csv_uploaded), unique = TRUE)
} else if (input$sep == ";") {
# Semicolon separated
csv_uploaded <- readr::read_csv2(file = input$dataset_csv$datapath,
col_names = input$header,
quote = input$quote)
# Rename columns containing special characters e.g. "-"
names(csv_uploaded) <- make.names(names(csv_uploaded), unique = TRUE)
} else if (input$sep == "\t") {
# Tab separated
csv_uploaded <- readr::read_tsv(file = input$dataset_csv$datapath,
col_names = input$header,
quote = input$quote)
# Rename columns containing special characters e.g. "-"
names(csv_uploaded) <- make.names(names(csv_uploaded), unique = TRUE)
}
return(csv_uploaded)
} else if (values_csv$upload_state == 'reset') {
# Upload reseted
switch (input$ex_data_csv,
"Fisher NY (EPSG Code: 4326)" = fisher_ny,
"None" = return()
)
}
})
# Display data frame or summary of data
output$contents <- DT::renderDataTable({
if (!is.null(csvInput())) {
DT::datatable(csvInput(),
rownames = FALSE,
options = list(
lengthMenu = list(c(5, 10, 20, 50, 100),
c('5', '10', '20', '50', '100')),
pageLength = 10
)
)
}
})
# Summary
output$summary <- renderPrint({
if (!is.null(csvInput())) {
summary(object = csvInput())
}
})
# Map Upload --------------------------------------------------------------
# Show headline of tab
output$env_head <- renderText({
validate(
need(csvInput(), 'Please upload tracking data first.'),
need(input$epsg_csv, 'Please assign an EPSG code to the tracking data first.')
)
"Upload Map(s) of Environmental Covariates and Assign Corresponding EPSG Code"
})
# Upload and reset-button to switch between upload and example TIF
values_env <- reactiveValues(upload_state = NULL)
observeEvent(input$dataset_env, {
values_env$upload_state <- 'uploaded'
})
observeEvent(input$reset_env, {
values_env$upload_state <- 'reset'
})
# Environmental data input e.g. TIF-File
envInput <- reactive({
validate(
need(csvInput(), ''),
need(input$epsg_csv, ''),
need(!is.null(input$ex_data_env), '')
)
# No upload
if (is.null(values_env$upload_state)){
switch (input$ex_data_env,
"Fisher NY Land Use Area" = land_use_fisher_ny,
"None" = return()
)
} else if (values_env$upload_state == 'uploaded') {
# File uploaded
# Get names of uploaded TIFs (named X0, X1, ... otherwise)
names_list <- lapply(input$dataset_env$name, function(x) x)
names_list <- gsub(".tif", '', names_list)
# Store uploaded TIF file(s) as raster layer(s) in list
raster_list <- lapply(input$dataset_env$datapath, raster::raster)
# Test whether multiple files are uploaded
# Single file: access raster layer in list
if (length(raster_list) == 1) {
# Rename
names(raster_list[[1]]) <- names_list
raster_list[[1]]
} else {
# Multiple files: store raster layers in list as raster stack
r_stack <- raster::stack(raster_list)
# Rename
names(r_stack) <- names_list
r_stack
}
} else if (values_env$upload_state == 'reset') {
# Upload reseted
switch (input$ex_data_env,
"Fisher NY Land Use Area" = land_use_fisher_ny,
"None" = return()
)
}
})
# Rename environmental data input
env <- reactive({
# No handsontable input
if (is.null(input$env_df)) {
envInput()
} else {
# Handsontable input
env_renamed <- envInput()
names(env_renamed) <- env_info()$Covariate
env_renamed
}
})
# Input: Upload example data
output$ex_data_env <- renderUI({
validate(
need(csvInput(), ''),
need(input$epsg_csv, '')
)
selectInput(
inputId = "ex_data_env",
label = "Load Example Map:",
choices = c("None", "Fisher NY Land Use Area")
)
})
# Detect EPSG Code of TIF-File by left joining data frame "epsg_data"
epsg_env_detected <- reactive({
validate(
need(env(), '')
)
env_prj4 <- data.frame("prj4" = raster::projection(env()),
stringsAsFactors = FALSE)
detect_epsg <- dplyr::left_join(env_prj4, epsg_data, by = "prj4")
# Multiple matches possible
detect_epsg %>% select("EPSG Code(s)" = code, "Description" = note)
})
# Input: EPSG Code TIF
output$epsg_env <- renderUI({
validate(
need(csvInput(), ''),
need(input$epsg_csv, '')
)
selectizeInput(
inputId = "epsg_env",
label = "Assign EPSG Code:",
choices = c('', sort(na.omit(epsg_data$code))),
selected = '',
options = list(maxOptions = 10000)
)
})
# Show headline of EPSG Code table
output$epsg_head <- renderText({
validate(
need(csvInput(), ''),
need(input$epsg_csv, '')
)
if (!is.null(epsg_env_detected())) {
"Found EPSG Code(s) for Uploaded File(s)"
}
})
# Sub headline (instructions)
output$epsg_sub_head <- renderText({
validate(
need(epsg_env_detected(), '')
)
"Please verify and assign an appropriate EPSG code this may vary from the
option(s) below."
})
# Data frame of detected EPSG codes
output$contents_env <- DT::renderDataTable({
validate(
need(env(), 'Please upload a map of environmental covariates.'),
need(epsg_env_detected(), 'No EPSG code detected for uploaded file.')
)
DT::datatable(epsg_env_detected(),
rownames = FALSE,
options = list(searching = FALSE, paging = FALSE,
# Left align columns
columnDefs = list(
list(className = 'dt-left', targets = 0:1)
)
)
)
})
# Track Creation ----------------------------------------------------------
# Update variable selection based on uploaded data set
# Show headline for track menu in sidebar
output$track_head <- renderText({
if (!is.null(csvInput()) && !is.null(envInput()) && input$epsg_env != '') {
"Select Track Variables"
} else if (!is.null(csvInput()) && !is.null(envInput()) &&
input$epsg_env == '') {
"Please assign an EPSG code to the map of environmental covariates first."
} else {
"Please upload tracking data and map first."
}
})
# Choose x (longitude)
output$x <- renderUI({
validate(
need(csvInput(), ''),
need(envInput(), ''),
need(input$epsg_env, '')
)
selectizeInput(
inputId = 'x',
label = "Longitude:",
choices = colnames(csvInput()),
options = list(
placeholder = 'Please assign longitude.',
onInitialize = I('function() { this.setValue(""); }')
)
)
})
# Choose y (latitude)
output$y <- renderUI({
validate(
need(csvInput(), ''),
need(envInput(), ''),
need(input$epsg_env, '')
)
selectizeInput(
inputId = 'y',
label = "Latitude:",
choices = colnames(csvInput()),
options = list(
placeholder = 'Please assign latitude.',
onInitialize = I('function() { this.setValue(""); }')
)
)
})
# Choose ts (timestamp)
output$ts <- renderUI({
validate(
need(csvInput(), ''),
need(envInput(), ''),
need(input$epsg_env, '')
)
selectizeInput(
inputId = 'ts',
label = "Timestamp:",
choices = colnames(csvInput()),
options = list(
placeholder = 'Please assign timestamp.',
onInitialize = I('function() { this.setValue(""); }')
)
)
})
# EPSG Code Transformation of tracking data (CSV)
output$epsg_trk <- renderUI({
validate(
need(csvInput(), ''),
need(envInput(), ''),
need(input$epsg_env, '')
)
selectizeInput(
inputId = "epsg_trk",
label = "Transform Track to EPSG Code:",
choices = sort(na.omit(epsg_data$code)),
selected = input$epsg_env,
options = list(maxOptions = 10000)
)
})
# Choices of ID input
# Remove assigned longitude, latitude and timestamp columns
id_choices <- reactive({
validate(
need(input$x, ''),
need(input$y, ''),
need(input$ts, '')
)
csv_cols <- colnames(csvInput())
pos_x_y_ts <- which(csv_cols %in% c(input$x, input$y, input$ts))
# Remove assigned longitude, latitude and timestamp columns
csv_cols[-pos_x_y_ts]
})
# ID
output$id <- renderUI({
validate(
need(id_choices(), '')
)
selectizeInput(
inputId = 'id',
label = "ID:",
choices = id_choices(),
options = list(
placeholder = 'Group by ID (optional)',
onInitialize = I('function() { this.setValue(""); }')
)
)
})
# Filter IDs of the track
output$id_trk <- renderUI({
validate(
need(input$id, '')
)
selectizeInput(
inputId = "id_trk",
label = "Select ID(s):",
choices = sort(unique(dat()$id)),
multiple = TRUE,
selected = sort(unique(dat()$id))[1:length(unique(dat()$id))]
)
})
# Resample track headline
output$resamp_head <- renderText({
validate(
need(input$x, ''),
need(input$y, ''),
need(input$ts, '')
)
"Resample Track"
})
# Input: resampling rate in min
output$rate_min <- renderUI({
validate(
need(input$x, ''),
need(input$y, ''),
need(input$ts, '')
)
numericInput(
inputId = "rate_min",
label = "Resampling Rate (in min):",
value = NA, #15,
min = 0,
step = 1
)
})
# Input: tolerance in min
output$tol_min <- renderUI({
validate(
need(input$x, ''),
need(input$y, ''),
need(input$ts, '')
)
numericInput(
inputId = "tol_min",
label = "Tolerance (in min):",
value = NA, #2,
min = 0,
step = 1
)
})
# Dynamic dateRangeInput for tracking data frame ----
output$fetch_dr <- renderUI({
validate(
need(input$ts, ''),
need(!is.null(input$id), '')
)
if (input$id == '') {
min.date <- min(dat_excl_id()$ts)
max.date <- max(dat_excl_id()$ts)
} else {
validate(
need(input$id_trk, 'Please select at least one ID.')
)
# Subset of "dat" data frame by selected IDs
dat_id <- dat() %>% filter(id %in% input$id_trk)
min.date <- min(dat_id$ts)
max.date <- max(dat_id$ts)
}
dateRangeInput(inputId = "daterange",
label = "Choose a Date Range:",
start = min.date,
# Does not include upper bound therefore plus 1 day
end = as.Date(max.date) + 1,
max = Sys.Date(),
format = "yyyy-mm-dd",
separator = "to",
startview = "year"
)
})
# Display data frame or column summary of track
output$display_trk <- renderUI({
validate(
need(input$x, ''),
need(input$y, ''),
need(input$ts, '')
)
radioButtons(
inputId = "display_trk",
label = "Display",
choices = c("Data Frame", "Column Summary"),
selected = "Data Frame"
)
})
# Create a track: select relevant columns and omit NAs
dat <- reactive({
validate(
need(input$x, ''),
need(input$y, ''),
need(input$ts, ''),
need(input$id, '')
)
csvInput() %>%
select(x = input$x, y = input$y, ts = input$ts, id = input$id) %>%
na.omit()
})
# Create a track: select relevant columns and omit NAs (excluding ID)
dat_excl_id <- reactive({
validate(
need(input$x, ''),
need(input$y, ''),
need(input$ts, '')
)
csvInput() %>%
select(x = input$x, y = input$y, ts = input$ts) %>%
na.omit()
})
#trk()####
# Create a track
trk <- reactive({
validate(
need(input$x, ''),
need(input$y, ''),
need(input$ts, ''),
need(input$daterange[1], 'Please select start of date range.'),
need(input$daterange[2], 'Please select end of date range.')
)
# No ID selected (one model for all animals)
if (input$id == '') {
# Subset data according to selected dateRangeInput
dat_excl_id_df <- dat_excl_id() %>%
filter(ts >= input$daterange[1] & ts <= input$daterange[2])
# Assign known EPSG Code to tracking data
track <- make_track(dat_excl_id_df,
x, y, ts,
crs = sp::CRS(paste0("+init=epsg:", input$epsg_csv))
)
# Transform CRS of track
if (input$epsg_csv == input$epsg_trk) {
track
} else {
transform_coords(track, sp::CRS(paste0("+init=epsg:", input$epsg_trk)))
}
} else if (ifelse(input$id == '', yes = 0, no = length(input$id_trk)) > 1) {
# Multiple IDs selected (individual models)
dat_df <- dat() %>%
filter(
# Subset filtered IDs
id %in% input$id_trk,
# Subset data according to selected dateRangeInput
ts >= input$daterange[1] & ts <= input$daterange[2]
)
# Assign known EPSG Code to tracking data (no transformation necessary)
if (input$epsg_csv == input$epsg_trk) {
track_multi <- dat_df %>% nest(-id) %>%
mutate(track = lapply(data, function(d) {
amt::make_track(d, x, y, ts, crs = sp::CRS(paste0("+init=epsg:",
input$epsg_csv))
)
}))
track_multi
} else {
# Transform CRS of track
trk_multi_tr <- dat_df %>% nest(-id) %>%
mutate(track = lapply(data, function(d) {
amt::make_track(d, x, y, ts, crs = sp::CRS(paste0(
"+init=epsg:", input$epsg_csv))) %>%
amt::transform_coords(sp::CRS(paste0("+init=epsg:", input$epsg_trk))
)
}))
trk_multi_tr
}
} else if (ifelse(input$id == '', yes = 0, no = length(input$id_trk)) == 1) {
# One ID selected
dat_df <- dat() %>%
filter(
# Subset filtered ID
id == input$id_trk,