-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
1752 lines (1332 loc) · 64.1 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
#' Paper based Annex dashboard
#' Authors : Meklit Chernet, Turry Ouma, IITA
#' Last updated on : November 2021 (to include GH)
#'
#setwd("C:/Users/User/Documents/ACAI/DASHBOARDS/paper based/PaperbasedDashboard_NG -testSP - feb")
#setwd("/home/akilimo/projects/PaperbasedDashboard_NG")
library(tidyr)
require(plyr)
library(rgdal)
library(raster)
library(dismo)
library(maptools)
library(rgeos)
require(RColorBrewer)
require(graphics)
require(rasterVis)
library(sp)
library(shinyalert)
library(ggthemes)
require(ggplot2)
library(gridExtra)
library(hexbin)
library(viridis)
library(sf)
library(ggspatial)
require(ggrepel)
library(shiny)
library(shinyWidgets)
library(shinythemes)
library(plotly)
library(sf)
library(raster)
library(dplyr)
library(spData)
library(tmap)
library(leaflet)
library(cartogram)
library(grid)
library(formattable)
library(shinybusy)
library(DT)
library(shinyjs)
source("SP_functions.R")
### SHINY SERVER ###
server = function(input, output, session) {
#.............................................................................
# Show a modal on initiation of tool
#.............................................................................
#
dataModal <- function(failed = FALSE) {
list(
modalDialog(
#span("AKILIMO Paper Based Annex dashboard"),
size = c("m"),
HTML('<img src="pics/akilimo4.jpg" />'),
br(),
# br(),
selectInput("use", "Please select the use case",
choices = c("Fertilizer Recommendation", "Scheduled Planting"),
selected = ""),
easyClose = FALSE,
fade = TRUE,
# if (failed)
# div(tags$b("Invalid name of data object", style = "color: red;")),
footer = tagList(
#modalButton("Cancel"),
useShinyjs(), # Set up shinyjs
actionButton("ok", "DONE!")
)
),
tags$style("
#shiny-modal .modal-dialog {
position: absolute;
top: 150px;
right: 10px;
left: 200px;
bottom: 50;
left: 0;
z-index: 10040;
}")
)
}
# Show modal when button is clicked.
# showModal(
# list(
# modalDialog(title = "Modal2"),
# tags$script("$('.modal-backdrop').css('display', 'none');")
# )
# )
showModal(
dataModal()
)
observeEvent(input$ok, {
if(input$use == "Fertilizer Recommendation"){
shinyalert("Fertilizer Recommendation", "This tool contains tables and maps with advice on application rates of urea,
NPK fertilizer for cassava, as well as the expected root yield response. Response to fertilizer
depends on soil conditions and the time of planting.
This window will automatically close once country data is loaded.
",
type = "info", timer = 9000, size = 'm',
closeOnClickOutside = FALSE,
closeOnEsc = FALSE,
animation = FALSE,
html = FALSE,
showConfirmButton = FALSE,
showCancelButton = FALSE,
confirmButtonText = "OK",
confirmButtonCol = "#AEDEF4")
}
})
# showModal(dataModal())
observeEvent(input$ok, {
if(input$use == "Scheduled Planting"){
shinyalert("Loading planting and harvest schedules...", "
This tool provides expected cassava yields for different planting and harvest schedules.
These are yields predicted based on the common rainfall pattern between the month of planting
and harvest, and assume good agronomic practices are applied. For every State and planting
month, a table is generated, presenting the expected root yield for different LGAs (in the rows)
and harvest from 8 up to 15 months after planting (in the columns).
This window will automatically close when the data is loaded.
",
type = "info", timer = 10000, size = 'm',
closeOnClickOutside = FALSE,
closeOnEsc = FALSE,
animation = FALSE,
html = FALSE,
showConfirmButton = FALSE,
showCancelButton = FALSE,
confirmButtonText = "OK",
confirmButtonCol = "#AEDEF4")
}
})
# # Display information about selected data
# output$dataInfo <- renderPrint({
# if(input$usecase == "Fertilizer Recommendation"){
# "No data selected"
# }else{
# "summary(vals$data)"
# }
# })
#.............................................................................
#spinner before maps are displayed
#.............................................................................
observeEvent(input$btn_go, {
shinybusy::show_modal_spinner(
spin = "cube-grid",
#spin = "fading-circle",
#spin = "fading-circle",
color = "#228B22",
#00FF00
text = "Please wait while the map is being generated...",
)
Sys.sleep(6)
remove_modal_spinner()
})
# spin = "double-bounce",
# color = "#112446",
# timeout = 100,
# position = c("top-right", "top-left", "bottom-right", "bottom-left", "full-page"),
# onstart = TRUE,
# margins = c(10, 10),
# height = "50px",
# width = "50px"
#.............................................................................
#render select input options
#.............................................................................
# output$country <- renderUI({
#
# pickerInput("country", "Country:",
# choices = c("Nigeria"),
# selected = "Nigeria",
# multiple = TRUE,
# options = pickerOptions(maxOptions = 1))
# })
#})
# output$countrySP <- renderUI({
#
# pickerInput("countrySP", "Country:",
# choices = c("Nigeria"),
# selected = "Nigeria",
# multiple = TRUE,
# options = pickerOptions(maxOptions = 1))
# })
#
# output$usecase <- renderUI({
#
# pickerInput("usecase", "Select use case",
# choices = c("Fertilizer Recommendation", "Scheduled Planting"),
# selected = NULL,
# multiple = TRUE,
# options = pickerOptions(maxOptions = 1),
# )
# })
output$lga_Groups <- renderUI({
pickerInput("lga_Groups", "Select state",
choices = c("Abia", "Akwa Ibom","Anambra", "Benue", "Cross River", "Delta", "Ebonyi","Edo", "Ekiti",
"Enugu","Imo", "Kogi", "Kwara","Ogun", "Ondo", "Osun", "Oyo","Taraba"),
selected = NULL,
multiple = TRUE,
options = pickerOptions(maxOptions = 1))
})
observeEvent(input$lga_Groups, {
if(!is.null(input$lga_Groups)) {
output$plntmth <- renderUI({
pickerInput("plntmth", "Select planting month",
choices = c("January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December"),
selected = NULL,
multiple = TRUE,
options = pickerOptions(maxOptions = 1))
})
}
})
observeEvent(input$plntmth, {
if(!is.null(input$plntmth)) {
output$costs <- renderUI({
pickerInput("costs", "Would you like to specify your prices for cassava and fertilizers?",
choices = c("Yes", "No"),
selected = NULL,
multiple = TRUE,
options = pickerOptions(maxOptions = 1))
})
}
})
observeEvent(input$plntmth, {
if(!is.null(input$plntmth)) {
output$selection <- renderUI({
pickerInput("selection", "Select variable to view",
choices = c("NPK 15:15:15 rate", "Expected yield response", "Urea rate"),
selected = NULL,
multiple = TRUE,
options = pickerOptions(maxOptions = 1))
})
}
})
observeEvent(input$lga_Groups, {
if(!is.null(input$lga_Groups)) {
output$unit_loc <- renderUI({
selectInput("unit_loc", "Select unit of land",
choices = c("acre", "hectare"))
})
}
})
observeEvent(input$lga_GroupsSP, {
if(!is.null(input$lga_GroupsSP)) {
output$unit_locSP <- renderUI({
selectInput("unit_locSP", "Select unit of land",
choices = c("acre", "hectare"))
})
}
})
observeEvent(input$unit_loc, {
if(!is.null(input$unit_loc)) {
output$FCY_ha <- renderUI({
selectInput("FCY_ha", "Select Your Current Yield (Tonnes)",
choices = c("0-7.5 t/hectare", "7.5-15 t/hectare", "15-22.5 t/hectare", "22.5-30 t/hectare", ">30 t/hectare", ""),
selected = "")
})
output$FCY_acre <- renderUI({
selectInput("FCY_acre", "Select Your Current Yield (Tonnes)",
choices = c("0-3 t/acre", "3-6 t/acre", "6-9 t/acre", "9-12 t/acre", ">12 t/acre", ""),
selected = "")
})
}
})
observeEvent(input$costs, {
if(input$costs == "Yes" ) {
output$CassavaPrice <- renderUI({
textInput("CassavaPrice", "Price of cassava per ton")
})
}
})
observeEvent(input$costs, {
if(input$costs == "Yes") {
output$NPK151515Price <- renderUI({
textInput("NPK151515Price", "Cost of NPK:15:15:15 per 50Kg bag")
})
}
})
observeEvent(input$costs, {
if(input$costs == "Yes") {
output$UreaPrice <- renderUI({
textInput("UreaPrice", "Cost of Urea per 50Kg bag")
})
}
})
observeEvent(input$costs, {
if(input$costs == "Yes") {
output$btn_go <- renderUI({
actionButton("btn_go", "Get Maps & Tables", icon("map"),
style="color: #fff; background-color: green; border-color: #2e6da4")
})
}else if(input$costs == "No"){
output$btn_go <- renderUI({
actionButton("btn_go", "Get Maps & Tables", icon("map"),
style="color: #fff; background-color: green; border-color: #2e6da4")
})
}
})
#
# hideTab(inputId = "nav", target = "Use case mapper")
# hideTab(inputId = "nav", target = "View maps side by side")
# hideTab(inputId = "nav", target = "View Table")
# hideTab(inputId = "nav", target = "Scheduled Planting Table")
#
observeEvent(input$use, {
if (input$use == "Scheduled Planting"){
showTab(inputId = "nav", target = "Expected yield for different planting and harvest schedules", select = TRUE)
hideTab(inputId = "nav", target = "Use case mapper")
hideTab(inputId = "nav", target = "View maps side by side")
hideTab(inputId = "nav", target = "View Table")
}
})
observeEvent(input$use, {
if(input$use == "Fertilizer Recommendation"){
hideTab(inputId = "nav", target = "Expected yield for different planting and harvest schedules")
showTab(inputId = "nav", target = "Use case mapper", select = TRUE)
showTab(inputId = "nav", target = "View maps side by side")
showTab(inputId = "nav", target = "View Table")
}
})
# observeEvent(input$btn_go, {
# if (input$btn_go > 0){
# hideTab(inputId = "nav", target = "Use case mapper")
# showTab(inputId = "nav", target = "View maps side by side")
# showTab(inputId = "nav", target = "View Table")
# showTab(inputId = "nav", target = "Scheduled Planting Table")
#
#
# }
#
# })
# observeEvent(input$jumpToP2, {
# updateTabsetPanel(session, "nav",
# selected = "panel2")
# })
#
# observeEvent(input$jumpToP3, {
# updateTabsetPanel(session, "nav",
# selected = "panel1")
# })
#
# observeEvent(input$ok,{
# runjs("$('.active').removeClass('active');")
#
# })
# $('.active').removeClass('active');//remove current active element if there's
# observeEvent(input$use, {
# if (input$use == "Scheduled Planting"){
# hideTab(inputId = "nav", target = "View maps side by side")
# }else if(input$use == "Fertilizer Recommendation"){
# showTab(inputId = "nav", target = "View maps side by side")
# }
# })
#
# observeEvent(input$use, {
# if (input$use == "Scheduled Planting"){
# hideTab(inputId = "nav", target = "View Table")
# }else if(input$use == "Fertilizer Recommendation"){
# showTab(inputId = "nav", target = "View Table")
# }
# })
#
# observeEvent(input$use, {
# if (input$use == "Scheduled Planting"){
# showTab(inputId = "nav", target = "Scheduled Planting Table")
# }else if(input$use == "Fertilizer Recommendation"){
# hideTab(inputId = "nav", target = "Scheduled Planting Table")
# }
# })
#
#.............................................................................
# # Show second modal to select usecase
# #.............................................................................
#
# dataModal <- function(failed = FALSE) {
# modalDialog(
# selectInput("useselect", "Select the use case",
# choices = c("Fertilizer Recommendation", "Scheduled Planting"),
# selected = "")
# )
# }
#
# # Show modal when button is clicked.
#
#
#
# showModal(dataModal())
#
# observeEvent(callbackR){
# if(is.null(callbackR)){
# output$output1 <- renderText({
# if(input$useselect == "Fertilizer Recommendation"){
# paste("success!!")
# }else if(input$useselect == "Scheduled Planting"){
# paste("failed!!")
#
# }
# })
# }
# }
#.................................................................................................................
## Determine platform type and set working directory accordingly
# When OK button is pressed, attempt to load the data set. If successful,
# remove the modal. If not show another modal, but this time with a failure
# message.
observeEvent(input$ok, {
if(!is.null(input$ok)){
removeModal()
}
})
observeEvent(input$ok, {
# Check that data object exists and is data frame.
if (input$use == "Fertilizer Recommendation"){
#######################################################################################
## Read the GIS layers
#######################################################################################
TownsNG <- readOGR(dsn = ".", layer = "Places_towns")
RiversNG <- readOGR(dsn = ".", layer = "Rivers")
boundaryNG <- readOGR(dsn=getwd(), layer="gadm36_NGA_1")
ngstate <- readOGR(dsn=getwd(), layer="gadm36_NGA_2")
###################################################################################################
## NG fertilizer recom for FCY 1:5
###################################################################################################
FR_NG_FCY1 <- readRDS("FRrecom_lga_level1_NG_2020.RDS")
FR_NG_FCY2 <- readRDS("FRrecom_lga_level2_NG_2020.RDS")
FR_NG_FCY3 <- readRDS("FRrecom_lga_level3_NG_2020.RDS")
FR_NG_FCY4 <- readRDS("FRrecom_lga_level4_NG_2020.RDS")
FR_NG_FCY5 <- readRDS("FRrecom_lga_level5_NG_2020.RDS")
###########################################################################
## adding planting month
###########################################################################
addplm <- function(ds, country){
ds$respY <- ds$TargetY - ds$CurrentY
ds$groRev <- ds$NR + ds$TC
ds$plm <- as.factor(ds$plw)
levels(ds$plm)[levels(ds$plm) %in% 1:4] <- "January"
levels(ds$plm)[levels(ds$plm) %in% 5:8] <- "February"
levels(ds$plm)[levels(ds$plm) %in% 9:13] <- "March"
levels(ds$plm)[levels(ds$plm) %in% 14:17] <- "April"
levels(ds$plm)[levels(ds$plm) %in% 18:22] <- "May"
levels(ds$plm)[levels(ds$plm) %in% 23:26] <- "June"
levels(ds$plm)[levels(ds$plm) %in% 27:30] <- "July"
levels(ds$plm)[levels(ds$plm) %in% 31:35] <- "August"
levels(ds$plm)[levels(ds$plm) %in% 36:39] <- "September"
levels(ds$plm)[levels(ds$plm) %in% 40:43] <- "October"
levels(ds$plm)[levels(ds$plm) %in% 44:48] <- "November"
levels(ds$plm)[levels(ds$plm) %in% 49:53] <- "December"
if(country=="NG"){
ds$rateUrea <- ds$urea
ds$rateNPK151515 <- ds$NPK15_15_15
}else{
ds$rateUrea <- ds$urea
ds$rateNPK171717 <- ds$NPK17_17_17
ds$rateDAP <- ds$DAP
}
return(ds)
}
FR_NG_FCY1_plm <- addplm(ds=FR_NG_FCY1, country = "NG") ## NG if user current yield is level 1
FR_NG_FCY2_plm <- addplm(ds=FR_NG_FCY2, country = "NG") ## NG if user current yield is level 2
FR_NG_FCY3_plm <- addplm(ds=FR_NG_FCY3, country = "NG") ## NG if user current yield is level 3
FR_NG_FCY4_plm <- addplm(ds=FR_NG_FCY4, country = "NG") ## NG if user current yield is level 4
FR_NG_FCY5_plm <- addplm(ds=FR_NG_FCY5, country = "NG") ## NG if user current yield is level 5
###########################################################################
## select FCY and read the corresponding file
## NG: Subsetting for the user defined Region and selecting a coordinate to put the state name in the map
###########################################################################
removeModal()
#.................................................................................................................
#Dashboard activity starts here
#.............................................................................
observeEvent(input$btn_go, {
#define reactive values
country <- input$country
FCY_ha <- input$FCY_ha
print(FCY_ha)
FCY_acre <- input$FCY_acre
print(FCY_acre)
Selection <- input$selection
usecase <- input$usecase
plantMonth <- input$plntmth
lgaGroups <- input$lga_Groups
cities <- input$city
unit <- input$unit_loc
UreaPrice <- as.numeric(input$UreaPrice)
NPK151515Price <- as.numeric(input$NPK151515Price)
CassavaPrice <- as.numeric(input$CassavaPrice)
costs <- input$costs
print(unit)
print(plantMonth)
#specify yield categories
if(unit == 'hectare'){
yield_level <- ifelse( FCY_ha == "0-7.5 t/hectare", "a low yield level",
ifelse( FCY_ha == "7.5-15 t/hectare","a normal yield level",
ifelse( FCY_ha == "15-22.5 t/hectare","a medium yield level",
ifelse( FCY_ha == "22.5-30 t/hectare","a high yield level",
ifelse( FCY_ha == ">30 t/hectare","a very high yield level"
)))))
}else if(unit == 'acre'){
yield_level <- ifelse( FCY_acre == "0-3 t/acre","a low yield level",
ifelse( FCY_acre == "3-6 t/acre","a normal yield level",
ifelse( FCY_acre == "6-9 t/acre","a medium yield level",
ifelse( FCY_acre == "9-12 t/acre","a high yield level",
ifelse( FCY_acre == ">12 t/acre","a very high yield level")
))))
}
#lgaGroups = "Edo"
lgaGroups <- input$lga_Groups
lgaGroups2 <- input$lga_Groups
if (unit == "hectare"){
FCY <- FCY_ha
if(FCY == "7.5-15 t/hectare" ){
ds <- FR_NG_FCY2_plm
}else if(FCY == "0-7.5 t/hectare" ){
ds <- FR_NG_FCY1_plm
}else if(FCY == "15-22.5 t/hectare" ){
ds <- FR_NG_FCY3_plm
}else if(FCY == "22.5-30 t/hectare"){
ds <- FR_NG_FCY4_plm
}else if(FCY == ">30 t/hectare" ){
ds <- FR_NG_FCY5_plm
}
}else if(unit == "acre"){
FCY <- FCY_acre
if(FCY == "3-6 t/acre" ){
ds <- FR_NG_FCY2_plm
}else if(FCY == "0-3 t/acre" ){
ds <- FR_NG_FCY1_plm
}else if(FCY == "6-9 t/acre" ){
ds <- FR_NG_FCY3_plm
}else if(FCY == "9-12 t/acre" ){
ds <- FR_NG_FCY4_plm
}else if(FCY == ">12 t/acre" ){
ds <- FR_NG_FCY5_plm
}
}
#######################################################################################################
#Subset by state for every filter option presented by users
Oyo <- droplevels(ds[ds$STATE == "Oyo", ])
Oyolabel <- data.frame(state= c("Oyo"), lon=c(3.3), lat=c(9))
Ogun <- droplevels(ds[ds$STATE == "Ogun", ])
Ogunlabel <- data.frame(state= c("Ogun"), lon=c(3.4), lat=c(7.65))
Kogi <- droplevels(ds[ds$STATE == "Kogi", ])
Kogilabel <- data.frame(state= c("Kogi"), lon=c(6.63), lat=c(8.56))
Kwara <- droplevels(ds[ds$STATE %in% c("Kwara"), ])
Kwaralabel <- data.frame(state= c( "Kwara"), lon=c(4.9), lat=c(9.5))
Taraba <- droplevels(ds[ds$STATE %in% c("Taraba"), ])
Tarabalabel <- data.frame(state= c( "Taraba"), lon=c(10.2), lat=c(9.05))
CrossRiver <- droplevels(ds[ds$STATE %in% c("Cross River"), ])
Crossriver_label <- data.frame(state= c("Cross River"), lon=c(8), lat=c(8.2) )
Benue <- droplevels(ds[ds$STATE %in% c("Benue"), ])
Benue_label <- data.frame(state= c("Benue"), lon=c(9.5), lat=c(8))
Edo <- droplevels(ds[ds$STATE %in% c("Edo"), ])
Edolabel <- data.frame(state= c("Edo"), lon=c(5.3), lat=c(7))
Delta <- droplevels(ds[ds$STATE %in% c("Delta"), ])
Deltalabel <- data.frame(state= c("Delta"), lon=c(6), lat=c(5))
Akwa_Ibom <- droplevels(ds[ds$STATE %in% c("Akwa Ibom"), ])
Akwa_Ibomlabel <- data.frame(state= c( "Akwa Ibom"), lon=c(8), lat=c(5.45))
Imo <- droplevels(ds[ds$STATE %in% c("Imo"), ])
Imolabel <- data.frame(state= c( "Imo"), lon=c(6.9), lat=c(5.9))
Abia <- droplevels(ds[ds$STATE %in% c("Abia"), ])
Abialabel <- data.frame(state= c( "Abia"), lon=c(7.7), lat=c(5.9))
Ondo <- droplevels(ds[ds$STATE %in% c("Ondo"), ])
Ondolabel <- data.frame(state= c( "Ondo"), lon=c(5.3), lat=c(6.6))
Ekiti <- droplevels(ds[ds$STATE %in% c("Ekiti"), ])
Ekitilabel <- data.frame(state= c( "Ekiti"), lon=c(5.3), lat=c(8.1))
Osun <- droplevels(ds[ds$STATE == "Osun", ])
Osunlabel <- data.frame(state= c("Osun"), lon=c(4.2), lat=c(8.05))
Anambra <- droplevels(ds[ds$STATE %in% c("Anambra"), ])
Anambralabel <- data.frame(state= c( "Anambra"), lon=c(7.15), lat=c(6.45))
Ebonyi <- droplevels(ds[ds$STATE %in% c("Ebonyi"), ])
Ebonyilabel <- data.frame(state= c( "Ebonyi"), lon=c(7.83), lat=c(6.67))
Anambra <- droplevels(ds[ds$STATE %in% c("Anambra"), ])
Anambra_label <- data.frame(state= c("Anambra"), lon=c(6.7), lat=c(5.9))
Enugu <- droplevels(ds[ds$STATE %in% c("Enugu"), ])
Enugulabel <- data.frame(state= c("Enugu"), lon=c(7), lat=c(7.1))
Ebonyi <- droplevels(ds[ds$STATE %in% c("Ebonyi"), ])
Ebonyilabel <- data.frame(state= c("Ebonyi"), lon=c(8.25), lat=c(6.9))
#specify other key values for getting recommendations
if(lgaGroups == "Benue"){
cities <- c("Makurdi")
LGApoints <- Benue
stateLabel <- Benue_label
textangle <- 0
couple = "One"
}else if(lgaGroups =="Cross River"){
cities <- c("Calabar")
LGApoints <- Benue
stateLabel <- Benue_label
textangle <- 0
couple = "One"
}else if(lgaGroups =="Enugu"){
cities <- c("Enugu")
LGApoints <- Enugu
stateLabel <- Enugulabel
textangle <- 0
couple = "One"
}else if(lgaGroups =="Delta"){
cities = c("Asaba")
LGApoints <- Delta
stateLabel <- Deltalabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Edo"){
cities = c("Benin City")
LGApoints <- Edo
stateLabel <- Edolabel
textangle <- 0
couple <- "One"
} else if(lgaGroups == "Imo"){
cities = c("Owerri")
LGApoints <- Imo
stateLabel <- Imolabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Abia"){
cities = c("Umuahia")
LGApoints <- Abia
stateLabel <- Abialabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Akwa Ibom"){
cities = c("Uyo")
LGApoints <- Akwa_Ibom
stateLabel <- Akwa_Ibomlabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Ekiti"){
cities = c("Ado Ekiti")
LGApoints <- Ekiti
stateLabel <- Ekitilabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Ondo"){
cities = c("Akure")
LGApoints <- Ondo
stateLabel <- Ondolabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Osun"){
cities = c("Osogbo")
LGApoints <- Osun
stateLabel <- Osunlabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Anambra"){
cities = c("Awka")
LGApoints <- Anambra
stateLabel <- Anambralabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Ebonyi"){
cities = c("Ebonyi")
LGApoints <- Ebonyi
stateLabel <- Ebonyilabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Taraba"){
cities = "Taraba"
LGApoints <- Taraba
stateLabel <- Tarabalabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Kogi"){
cities = "Kogi"
LGApoints <- Taraba
stateLabel <- Tarabalabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Kwara"){
cities = "Kwara"
LGApoints <- Kwara
stateLabel <- Kwaralabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Oyo"){
cities = "Oyo"
LGApoints <- Oyo
stateLabel <- Oyolabel
textangle <- 0
couple <- "One"
}else if(lgaGroups == "Ogun"){
cities = "Abeokuta"
LGApoints == Ogun
stateLabel <- Ogunlabel
textangle <- 0
couple <- "One"
}
#filter by month and couple and state
#plantMonth = "June"
plotData <- droplevels(LGApoints[LGApoints$plm == plantMonth, ])
if(couple == "Two"){
lgaGroups <- c(strsplit(lgaGroups, "_")[[1]][1], strsplit(lgaGroups, "_")[[1]][2])
}
if(couple == "Three"){
lgaGroups <- c(strsplit(lgaGroups, "_")[[1]][1], strsplit(lgaGroups, "_")[[1]][2], strsplit(lgaGroups, "_")[[1]][3])
}
plotData <- droplevels(plotData[plotData$STATE %in% lgaGroups, ])
#incorporate GIS layers
AOI <- lgaGroups
AOIMapS <- subset(boundaryNG, NAME_1 %in% AOI )
AOIMap <- subset(ngstate, NAME_1 %in% AOI )
AOIMap <- AOIMap[,c("NAME_1", "NAME_2")]
LGAnames <- as.data.frame(AOIMap)
LGAnames <- cbind(LGAnames, coordinates(AOIMap))
colnames(LGAnames) <- c("STATE","LGA","long","lat" )
LGAnames <- LGAnames[!LGAnames$LGA %in% c("IbadanNorth-West","IbadanNorth-East","IbadanSouth-West", "IbadanSouth-East"),]
LGAnames$LGA <- gsub("Egbado /", "", LGAnames$LGA )
crop_ngstate <- subset(ngstate, NAME_1 %in% AOI )
towns <- as.data.frame(TownsNG)
towns <- towns[towns$name %in% cities & towns$fclass %in% c("town", "city"),]
crop_RiversNG <- crop(RiversNG, extent(crop_ngstate))
crop_RiversNG <- crop_RiversNG[crop_RiversNG$fclass == "river", ]
LGAaverage <- ddply(plotData, .(LGA, STATE), summarize,
LGAUrea = round(mean(rateUrea), digits=0),
LGANPK151515 = round(mean(rateNPK151515), digits=0),
LGAdY = round(mean(respY), digits=0))
dss <- LGAaverage
dss$LGAUrea <- dss$LGAUrea / 2.47105
dss$LGANPK151515 <- dss$LGANPK151515 / 2.47105
dss$LGAdY <- dss$LGAdY / 2.47105
if(unit == 'acre'){
LGAaverage <- dss
}
plotData <- merge(plotData, LGAaverage, by=c("LGA", "STATE"))
if(unit == "hectare"){
plotData$Urea <- round(plotData$LGAUrea/25)*25
plotData$NPK15_15_15 <- round(plotData$LGANPK151515/50)*50
plotData$dY <- round(plotData$LGAdY/2)*2
}else{
plotData$Urea <- round(plotData$LGAUrea/10)*10
plotData$NPK15_15_15 <- round(plotData$LGANPK151515/20)*20
plotData$dY <- round(plotData$LGAdY/1)*1
}
#csv tables naming
fileNameCsv <- paste("tables", ".csv", sep="")
AOIMap2 <- merge(AOIMap, unique(plotData[, c("LGA", "Urea", "NPK15_15_15","dY", "LGAdY")]),by.x="NAME_2" ,by.y="LGA")
AOIMap2$month <- plantMonth
AOIMap2 <- AOIMap2[!is.na(AOIMap2$Urea), ]
plotData$month <- plantMonth
#generate table
Currency <- "Naira"
tt <- unique(as.data.frame(plotData[, c("STATE","LGA", "Urea", "NPK15_15_15", "LGAdY", "month")]))
tt$LGAdY <- round(tt$LGAdY, digits = 1)
tt <- tt[order(tt$STATE, tt$LGA, tt$month), ]
tt2 <- dplyr::select(tt, c(STATE, LGA, Urea, NPK15_15_15,LGAdY))
colnames(tt2) <- c("State","LGA", "Urea (kg/hectare)", "NPK 15:15:15 (kg/hectare)", "Expected yield increase (t)")
#subset by cost information in a reactive environment
if(costs == "No"){
output$tabletext_naira <- renderText({
paste("AKILIMO advice for planting in ", plantMonth, ". Your current yield is ", FCY, ".", sep="")
})
output$mytable <- renderDT({tt2},
rownames = FALSE,
extensions = c('Buttons','FixedColumns'),
options = list(dom = 'Bfrtip',
pageLength = nrow(tt2),
initComplete = DT::JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': 'black', 'color': '#fff'});",
"}"),
buttons = list(
list(extend = 'excel',
filename = paste('AKILIMO advice', '_', lgaGroups2, '_', plantMonth),
title = paste("AKILIMO advice for planting in ", plantMonth, ". Your current yield is between ", FCY, ".", sep="")),
list(extend = 'pdf',
filename = paste('AKILIMO advice', '_', lgaGroups2, '_', plantMonth),
title = paste("AKILIMO advice for planting in ", plantMonth, ". Your current yield is between ", FCY, ".", sep=""),
header = TRUE)
)
)
)
# data_output <- function(df) {
# DT::datatable(df, rownames= FALSE, options = list( dom = 'Bfrtip', buttons = c('excel','pdf','print','colvis'), pageLength = nrow(df), initComplete = DT::JS(
# "function(settings, json) {",
# "$(this.api().table().header()).css({'background-color': '#369BE9', 'color': '#fff'});",
# "}") ), list(extend = 'pdf',