-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_AGILE_Predict_DTF.R
781 lines (781 loc) · 42.4 KB
/
app_AGILE_Predict_DTF.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
##############################################################################
########## AGILE LDP Phenology App ##########
##############################################################################
# Libraries
##############################################################################
# setwd("C:/Google Drive/gitfolder/AGILE_LDP_Phenology/PredictDTF")
library(shiny); library(shinythemes); library(plotly)
library(leaflet); library(leaflet.minicharts)
library(tidyverse) # data wrangling
library(scales) # rescale()
library(rworldmap) # mapBubbles()
library(ggpubr) # ggarrange()
library(ggbeeswarm) # geom_quasirandom()
library(FactoMineR) # PCA() & HCPC()
library(plot3D) # 3D plots
##############################################################################
# Pre-App R work
##############################################################################
# General color palettes
colors <- c("darkred", "darkorange3", "darkgoldenrod2", "deeppink3",
"steelblue", "darkorchid4", "cornsilk4", "darkgreen")
# Expts color palette
colors_Expt <- c("darkolivegreen3", "darkolivegreen4", "palegreen2", "palegreen3",
"palegreen4", "darkgreen", "orangered2", "orangered4", "coral2",
"coral4", "orange2", "orange4", "aquamarine3","aquamarine4",
"slateblue1", "slateblue4", "deepskyblue3", "deepskyblue4" )
# Locations
names_Location <- c("Rosthern, Canada", "Sutherland, Canada", "Central Ferry, USA",
"Bhopal, India", "Jessore, Bangladesh", "Bardiya, Nepal",
"Cordoba, Spain", "Marchouch, Morocco", "Metaponto, Italy" )
# Experiments
names_Expt <- c("Rosthern, Canada 2016", "Rosthern, Canada 2017",
"Sutherland, Canada 2016", "Sutherland, Canada 2017",
"Sutherland, Canada 2018", "Central Ferry, USA 2018",
"Bhopal, India 2016", "Bhopal, India 2017",
"Jessore, Bangladesh 2016", "Jessore, Bangladesh 2017",
"Bardiya, Nepal 2016", "Bardiya, Nepal 2017",
"Cordoba, Spain 2016", "Cordoba, Spain 2017",
"Marchouch, Morocco 2016", "Marchouch, Morocco 2017",
"Metaponto, Italy 2016", "Metaponto, Italy 2017" )
# Experiment short names
names_ExptShort <- c("Ro16", "Ro17", "Su16", "Su17", "Su18", "Us18",
"In16", "In17", "Ba16", "Ba17", "Ne16", "Ne17",
"Sp16", "Sp17", "Mo16", "Mo17", "It16", "It17" )
# Shape
mypch <- c(11,14,2,6,17,8, 0,5,15,18,7,9, 3,4,1,16,10,13)
#
trts <- c("DTE","DTF","DTS","DTM","VEG","REP","RDTF","DTF2","DTF2_scaled", "Tb", "Tf", "Pc", "Pf", "PTT")
# Scaling function
traitScale <- function(x, trait) {
xout <- rep(NA, nrow(x))
for(i in unique(x$Expt)) {
mn <- x %>% filter(Expt == i) %>% pull(trait) %>% min(na.rm = T)
mx <- x %>% filter(Expt == i) %>% pull(trait) %>% max(na.rm = T)
rg <- mx - mn
xout <- ifelse(x$Expt == i, rescale(x %>% pull(trait), c(1,5), c(mn,mx)), xout)
}
xout
}
# Country info
ct <- read.csv("data/data_countries.csv")
# Lentil Diversity Panel metadata
regions <- c("Africa", "Asia", "Europe", "Americas", "ICARDA", "USDA", "Unknown")
ldp <- read.csv("data/data_ldp.csv") %>%
left_join(select(ct, Origin=Country, Region, SubRegion), by = "Origin") %>%
mutate(Region = ifelse(Origin %in% c("ICARDA","USDA","Unknown"), as.character(Origin), as.character(Region)),
SubRegion = ifelse(Origin %in% c("ICARDA","USDA","Unknown"), as.character(Origin), as.character(SubRegion)),
Region = factor(Region, levels = regions),
Lat2 = ifelse(duplicated(Lat), jitter(Lat, 1, 0.1), Lat),
Lon2 = ifelse(duplicated(Lon), jitter(Lon, 1, 0.1), Lon),
Lat3 = ifelse(is.na(Lat), ct$Lat[match(Origin, ct$Country)], Lat),
Lat3 = ifelse(duplicated(Lat3), jitter(Lat3, 1, 0.1), Lat3),
Lon3 = ifelse(is.na(Lon), ct$Lon[match(Origin, ct$Country)], Lon),
Lon3 = ifelse(duplicated(Lon3), jitter(Lon3, 1, 0.1), Lon3) )
# Modeling
m1 <- read.csv("data/model_t+p_d.csv") %>%
mutate(Expt = factor(Expt, levels = names_Expt))
m2 <- read.csv("data/model_t+p_coefs.csv")
# PCA results
pca <- read.csv("data/data_pca_results.csv") %>%
mutate(Cluster = factor(Cluster)) %>% select(-Region) %>%
select(Entry, Name, Origin, Cluster, everything()) %>%
left_join(select(ldp, Entry, Region), by = "Entry")
ldp <- ldp %>% left_join(select(pca, Entry, Cluster), by = "Entry")
# Tf, Pf, PTT
ptt <- read.csv("data/data_tb_pc.csv") %>% select(Entry, Expt, Tb, Pc, Tf, Pf, PTT) %>%
mutate(Expt = factor(Expt, levels = names_Expt))
# Prep raw data
# Note: DTF2 = non-flowering genotypes <- group_by(Expt) %>% max(DTF)
rr <- read.csv("data/data_raw.csv") %>%
mutate(Rep = factor(Rep), Year = factor(Year), PlantingDate = as.Date(PlantingDate),
Location = factor(Location, levels = names_Location),
Expt = factor(Expt, levels = names_Expt),
ExptShort = plyr::mapvalues(Expt, names_Expt, names_ExptShort),
DTF2_scaled = traitScale(., "DTF2"),
RDTF = round(1 / DTF2, 6),
VEG = DTF - DTE,
REP = DTM - DTF) %>%
left_join(ptt, by = c("Entry","Expt")) %>%
left_join(select(ldp, Entry, Cluster, Origin, Region, SubRegion), by = "Entry")
# Average raw data
dd <- rr %>%
group_by(Entry, Name, Expt, Location, Year, Origin, Region, SubRegion, Cluster) %>%
summarise_at(vars(DTE, DTF, DTS, DTM, VEG, REP, RDTF, DTF2, DTF2_scaled, Tb, Tf, Pc, Pf, PTT),
funs(mean), na.rm = T) %>% ungroup() %>%
mutate(Expt = factor(Expt, levels = names_Expt),
ExptShort = plyr::mapvalues(Expt, names_Expt, names_ExptShort),
Location = factor(Location, levels = names_Location),
DTF2_scaled = traitScale(., "DTF2"))
# Prep environmental data
ee <- read.csv("data/data_env.csv") %>%
mutate(Date = as.Date(Date),
ExptShort = plyr::mapvalues(Expt, names_Expt, names_ExptShort),
ExptShort = factor(ExptShort, levels = names_ExptShort),
Expt = factor(Expt, levels = names_Expt),
Location = factor(Location, levels = names_Location),
DayLength_rescaled = rescale(DayLength, to = c(0, 40)) )
# Prep field trial info
xx <- dd %>%
group_by(Expt) %>%
summarise_at(vars(DTE, DTF, DTS, DTM), funs(min, mean, max), na.rm = T) %>%
ungroup()
ff <- read.csv("data/data_info.csv") %>%
mutate(Start = as.Date(Start), Expt = factor(Expt, levels = names_Expt)) %>%
left_join(xx, by = "Expt")
for(i in unique(ee$Expt)) {
ee <- ee %>%
filter(Expt != i | (Expt == i & DaysAfterPlanting <= ff$DTM_max[ff$Expt == i]))
}
e2 <- ee
for(i in unique(ee$Expt)) {
e2 <- e2 %>%
filter(Expt != i | (Expt == i & DaysAfterPlanting <= ff$DTF_max[ff$Expt==i]))
}
tt <- e2 %>% group_by(Location, Year) %>%
summarise(T_mean = mean(Temp_mean, na.rm = T), T_sd = sd(Temp_mean, na.rm = T),
P_mean = mean(DayLength, na.rm = T), P_sd = sd(DayLength, na.rm = T) ) %>%
ungroup() %>%
mutate(Expt = paste(Location, Year)) %>%
select(-Location, -Year)
me <- c("Temperate", "South Asia", "Mediterranean")
ff <- ff %>% left_join(tt, by = "Expt") %>%
mutate(ExptShort = plyr::mapvalues(Expt, names_Expt, names_ExptShort),
ExptShort = factor(ExptShort, levels = names_ExptShort),
MacroEnv = factor(MacroEnv, levels = me),
Expt = factor(Expt, levels = names_Expt),
T_mean = round(T_mean, 1),
P_mean = round(P_mean, 1)) %>% arrange(ExptShort)
# ggplot theme
theme_AGL <- theme_bw() + theme(strip.background = element_rect(fill = "White"))
#
# R^2 function
modelR2 <- function(x, y) {
1 - ( sum((x - y)^2, na.rm = T) / sum((x - mean(x, na.rm = T))^2, na.rm = T))
}
# RMSE function
modelRMSE <- function(x, y) {
sqrt(sum((x-y)^2) / length(x))
}
##############################################################################
# User interface
##############################################################################
ui <- fluidPage(theme = shinytheme("yeti"), br(),
sidebarLayout(
sidebarPanel(p("LDP DTF Prediction App"), hr(),
numericInput("Entry", "Entry", 1, 1, 324, 1),
tableOutput("EntryTable"),
radioButtons("Plot_Entry", "Plot Entry", c(T, F), T, inline = T),
checkboxGroupInput("MyClusters", "Clusters", c("1","2","3","4","5","6","7","8"), c("1","2","3","4","5","6","7","8"), inline = T),
checkboxGroupInput("MyRegions", "Regions", regions, regions, inline = T),
selectInput("Trait", "Trait", trts, "DTF"),
selectInput("Expt", "Expt", names_Expt, "Rosthern, Canada 2016"),
checkboxGroupInput("Expts", "Expts", names_Expt, selected = names_Expt),
width = 2),
mainPanel(
tabsetPanel(
tabPanel("Home", br(),
p("App Author: Derek Michael Wright"),
p("Contact: derek.wright@usask.ca"),
p("Last Updated: 10-08-2020"),
hr(),
p(strong("Understanding photothermal interactions can help expand production range and increase genetic diversity of lentil (", em("Lens culinaris"), " Medik.)")),
p("Derek Wright, Sandesh Neupane, Taryn Heidecker, Teketel Haile, Crystal Chan, Clarice Coyne, Sripada Udupa, Fatima Henkrar, Eleonora Barilli, Diego Rubiales, Tania Gioia, Reena Mehra, Ashutosh Sarker, Rajeev Dhakal, Babul Anwar, Debashish Sarker, Albert Vandenberg, and Kirstin E. Bett"),
hr(), p(strong("Project Collaborators:")),
p("- Department of Plant Sciences and Crop Development Centre, University of Saskatchewan, Saskatoon, Saskatchewan, Canada"),
p("- United States Department of Agriculture Western Region Plant Introduction Station, Pullman, Washington, USA"),
p("- International Center for Agriculture Research in the Dry Areas, Rabat, Morocco"),
p("- Institute for Sustainable Agriculture, Spanish National Research Council, Cordoba, Spain"),
p("- School of Agriculture, Forestry, Food and Environmental Sciences, University of Basilicata, Potenza, Italy"),
p("- International Center for Agriculture Research in the Dry Areas, New Delhi, India"),
p("- Local Initiatives for Biodiversity, Research and Development, Pokhara, Nepal"),
p("- Bangladesh Agricultural Research Institute, Jessore, Bangladesh"), hr(),
p(strong("Project Sponsors:")),
p("- Saskatchewan Pulse Growers Association"),
p("- Western Grains Research Foundation"),
p("- GenomePrairie"),
p("- GenomeCanada"),
p("- Saskatchewan Ministry of Agriculture")
), # Home tab
tabPanel("Predict DTF",
fluidRow(column(numericInput("Mean_T", "Mean Temperature",13,0,25,0.1),
numericInput("Mean_P", "Mean Photoperiod",12,0,18,0.1), width = 2),
column(numericInput("DTF_Min", "Min DTF", 30,30,200,1),
numericInput("DTF_Max", "Max DTF", 80,30,200,1), width = 2)),
tabsetPanel(
tabPanel("Clusters", plotlyOutput("Predict_DTF_Clusters", height = 500)),
tabPanel("DTFxCoef",
radioButtons("Select_Coef_DTF", "Select Coefficient", c("a","b","c"), "c", inline = T),
plotlyOutput("Predict_DTF_DTFxCoef", height = 500)),
tabPanel("cxb", plotlyOutput("Predict_DTF_cxb", height = 500)),
tabPanel("Table",
downloadButton("Adapted_DTF_Table.csv", "Download Selected Table"),
DT::dataTableOutput("Adapted_DTF_Table"))
) ),
tabPanel("\"Adaptated\"",
fluidRow(column(selectInput("Ad_Country", "Adaptation Country", unique(ldp$Origin)[order(unique(ldp$Origin))], selected = "Canada"), width = 3),
column(numericInput("sd_Mult","sd Multiplier",1,0.5,3,0.1), width = 2)),
checkboxGroupInput("Ad_Countries", "Countries", unique(ldp$Origin)[order(unique(ldp$Origin))], selected = "USA", inline = T),
radioButtons("Select_Coef_Ad", "Select Coefficient", c("a","b","c"), "c", inline = T),
tabsetPanel(
tabPanel("TraitxCoef", plotlyOutput("Adapted1", height = 500)),
tabPanel("cxb", plotlyOutput("Adapted2", height = 500)),
tabPanel("Table",
downloadButton("Adapted_Table.csv", "Download Selected Table"),
DT::dataTableOutput("Adapted_Table"))
) ),
#
tabPanel("PhotoThermal Model", tabsetPanel(
tabPanel("PhotoThermal Plane", br(), plotOutput("PhotoThermal_Plane" , height = 500)),
tabPanel("Obs vs Pre", tabsetPanel(
tabPanel("All", plotlyOutput("OxP_All", height = 500)),
tabPanel("Expt", plotlyOutput("OxP_Expt", height = 500)),
tabPanel("Expts", plotOutput("OxP_Expts", height = 500))
)),
tabPanel("Coefficients", tabsetPanel(
tabPanel("abc", plotlyOutput("abc", height = 500)),
tabPanel("TraitxCoef",
radioButtons("Select_abc", "Select Coefficient", c("a","b","c"), "a", inline = T),
plotlyOutput("TraitxCoef", height = 500)),
tabPanel("cxb", plotlyOutput("cxb", height = 500))
) ),
tabPanel("T+P Increase",
fluidRow(column(numericInput("TempIncrease","Temperature Increase",1,-10,10,0.1), width = 2),
column(numericInput("PhotoIncrease","Photoperiod Increase",1,-10,10,0.1), width = 2)), tabsetPanel(
tabPanel("Expts", plotlyOutput("T_P_Expts", height = 500)),
tabPanel("Clusters", plotOutput("T_P_Clusters")),
tabPanel("Expt", plotlyOutput("T_P_Cluster", height = 500))
) )
) ) # PhotoThermal Model tab
) ) )
) #fluidpage
##############################################################################
# Server
##############################################################################
server <- function(input, output) {
###############################################################################
# - Sidebar
###############################################################################
output$EntryTable <- renderTable({
ldp %>% filter(Entry == input$Entry) %>% select(Name, Origin)
})
###############################################################################
# - Home
###############################################################################
###############################################################################
# -PhotoThermal Model
###############################################################################
# input <- list(Entry = 3)
output$PhotoThermal_Plane <- renderPlot({
xx <- rr %>% filter(!is.na(RDTF)) %>%
left_join(select(ff, Expt, T_mean, P_mean, MacroEnv), by = "Expt")
#
x1 <- xx %>% filter(Entry == input$Entry) %>% arrange(MacroEnv) %>%
mutate(myPal = as.character(plyr::mapvalues(MacroEnv,
c("Temperate", "South Asia", "Mediterranean"), c("darkgreen", "darkorange3", "darkblue"))))
x <- x1$T_mean
y <- x1$P_mean
z <- x1$RDTF
fit <- lm(z ~ x + y)
# predict values on regular xy grid
grid.lines = 12
x.pred <- seq(min(x), max(x), length.out = grid.lines)
y.pred <- seq(min(y), max(y), length.out = grid.lines)
xy <- expand.grid( x = x.pred, y = y.pred)
z.pred <- matrix(predict(fit, newdata = xy),
nrow = grid.lines, ncol = grid.lines)
pchs <- plyr::mapvalues(x1$Expt, names_Expt, c(rep(16,6),rep(15,6),rep(17,6))) %>%
as.character() %>% as.numeric()
# fitted points for droplines to surface
fitpoints <- predict(fit)
# scatter plot with regression plane
par(mar=c(1.5, 2.5, 1.5, 0.5))
scatter3D(x, y, z, pch = pchs, cex = 2, zlim = c(0.005,0.03), main = unique(x1$Name),
col = alpha(x1$myPal,0.5), colvar = as.numeric(x1$MacroEnv), colkey = F,
theta = 40, phi = 25, ticktype = "detailed", cex.lab = 1, cex.axis = 0.5,
xlab = "Temperature", ylab = "Photoperiod", zlab = "1 / DTF", col.grid = "gray90",bty = "u",
surf = list(x = x.pred, y = y.pred, z = z.pred, col = "black",
facets = NA, fit = fitpoints))
})
# input <- list(Expts = names_Expt, Plot_Entry = T, Entry = 1, MyClusters = c("1","2","3","4"))
output$OxP_All <- renderPlotly({
xx <- m1 %>% left_join(select(pca, Entry, Origin, Region, Cluster), by = "Entry") %>%
filter(!is.na(DTF), Expt %in% input$Expts, Cluster %in% input$MyClusters)
r2 <- modelR2(xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>% pull(DTF),
xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>% pull(Predicted_DTF))
r2 <- round(r2, 3)
#
mp <- ggplot(xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions),
aes(x = DTF, y = Predicted_DTF, color = Expt)) +
geom_point(aes(key1 = Entry, key2 = Name, key3 = Origin, key4 = Cluster), alpha = 0.8) +
geom_abline() +
scale_x_continuous(limits = c(30,160)) +
scale_y_continuous(limits = c(30,160)) +
scale_color_manual(name = NULL, values = colors_Expt[levels(xx$Expt) %in% input$Expts]) +
theme_AGL +
labs(title = paste("1/DTF = a + bT + Pc | RR =", r2), y = "Predicted", x = "Observed")
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry),
aes(key1 = Entry, key2 = Name, key3 = Origin, key4 = Cluster),
color = "black", fill = "Red", size = 2, pch = 23)
}
mp
})
# input <- list(Expt = "Rosthern, Canada 2016", Plot_Entry = T, Entry = 1, MyClusters = c("1","2","3","4"), MyRegions = c("Asia","Africa"))
output$OxP_Expt <- renderPlotly({
xx <- m1 %>% left_join(select(pca, Entry, Origin, Region, Cluster), by = "Entry") %>%
filter(!is.na(DTF), Expt == input$Expt)
r2 <- modelR2(xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>% pull(DTF),
xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>% pull(Predicted_DTF))
r2 <- round(r2, 3)
mymin <- min(xx$DTF, na.rm = T)
mymax <- max(xx$DTF, na.rm = T)
#
mp <- ggplot(xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions),
aes(x = DTF, y = Predicted_DTF, color = Cluster)) +
geom_point(aes(key1 = Entry, key2 = Name, key3 = Origin), alpha = 0.8) +
geom_abline() +
scale_x_continuous(limits = c(mymin,mymax)) +
scale_y_continuous(limits = c(mymin,mymax)) +
scale_color_manual(name = NULL, values = colors[as.numeric(input$MyClusters)]) +
theme_AGL +
labs(title = paste(input$Expt, "| RR =", r2), y = "Predicted", x = "Observed")
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry),
aes(key1 = Entry, key2 = Name, key3 = Origin),
color = "Black", fill = "Red", size = 2, pch = 23)
}
mp
})
# input <- list(Plot_Entry = T, Entry = 1, MyClusters = c("1","2","3","4"), MyRegions = c("Asia","Africa"))
output$OxP_Expts <- renderPlot({
xx <- m1 %>% left_join(select(pca, Entry, Origin, Region, Cluster), by = "Entry") %>%
filter(!is.na(DTF))
mymean <- mean(xx$DTF)
r2 <- 1 - (sum((xx$DTF - xx$Predicted_DTF)^2) / (sum((xx$DTF - mymean)^2)))
r2 <- round(r2, 3)
x1 <- xx %>% group_by(Expt) %>%
summarise(Mean = mean(DTF)) %>% ungroup()
#
for(i in 1:nrow(x1)) {
xi <- xx %>% filter(Expt == x1$Expt[i], Cluster %in% input$MyClusters, Region %in% input$MyRegions)
x1[i,"r2"] <- round(1 - (sum((xi$DTF - xi$Predicted_DTF)^2) /
sum((xi$DTF - x1$Mean[i])^2)), 2)
}
#
mp <- ggplot(xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions),
aes(x = DTF, y = Predicted_DTF)) +
geom_point(aes(color = Cluster), alpha = 0.5) + geom_abline() +
geom_label(x = 26, y = 143, color = "black", hjust = 0, vjust = 0,
aes(label = r2), size = 3, data = x1) +
facet_wrap(Expt~., ncol = 6, labeller = label_wrap_gen(width = 17)) +
scale_x_continuous(limits = c(30,160)) +
scale_y_continuous(limits = c(30,160)) +
scale_color_manual(name = NULL, values = colors[as.numeric(input$MyClusters)]) +
theme_AGL +
theme(legend.position = "none") +
labs(title = paste("1/DTF = a + bT + Pc | RR =", r2), y = "Predicted", x = "Observed")
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry),
color = "Black", fill = "Red", size = 2, pch = 23)
}
mp
})
# input <- list(Plot_Entry = T, Entry = 1, MyClusters = c("a","2","3","4"))
output$abc <- renderPlotly({
xx <- m2 %>%
left_join(select(pca, Entry, Origin, Region, Cluster), by = "Entry") %>%
select(Entry, Name, Origin, Cluster, Region, a, b, c) %>%
gather(Trait, Value, a, b, c) %>%
filter(Cluster %in% input$MyClusters)
xE <- xx %>% filter(Entry == input$Entry)
mp <- ggplot(xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions),
aes(x = Cluster, y = Value * 10000) ) +
geom_violin(aes(fill = Cluster), color = NA, alpha = 0.7) +
geom_quasirandom(size = 0.5, aes(key1 = Entry, key2 = Name, key3 = Origin)) +
facet_wrap(Trait~., nrow = 1, scales = "free") +
theme_AGL +
theme(legend.position = "none", strip.text = element_text(face = "italic")) +
scale_fill_manual(name = NULL, values = colors[as.numeric(input$MyClusters)]) +
labs(y = "x 10000", x = "Cluster")
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xE, color = "Black", fill = "Red", size = 3, pch = 23,
aes(key1 = Entry, key2 = Name, key3 = Origin))
}
mp
})
# input <- list(Expt = "Rosthern, Canada 2016", Trait = "DTF", Select_abc = "c", Plot_Entry = T, Entry = 1)
output$TraitxCoef <- renderPlotly({
x1 <- dd %>% filter(Expt == input$Expt) %>%
select(Entry, Trait=input$Trait)
xx <- m2 %>% rename(Coef=input$Select_abc) %>%
left_join(select(pca, Entry, Origin, Region, Cluster), by = "Entry") %>%
left_join(x1, by = "Entry")
x1 <- xx %>%
group_by(Origin) %>%
summarise_at(vars(Trait, Coef), funs(mean, sd)) %>%
rename(Coef=Coef_mean, Trait=Trait_mean)
x2 <- x1 %>% mutate(CO = 1) %>%
filter(Origin %in% c("Syria", "Jordan", "Turkey", "Lebanon", "Israel")) %>%
select(Trait, Coef, everything())
# Plot
find_hull <- function(df) df[chull(df[,2], df[,1]), ]
polys <- plyr::ddply(x2, "CO", find_hull)
mp <- ggplot(xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions),
aes(y = Coef * 10000, x = Trait)) +
geom_polygon(data = polys, fill = NA, color = "black") +
geom_point(aes(key1 = Entry, key2 = Name, key3 = Origin, color = Cluster)) +
scale_color_manual(values = colors[as.numeric(input$MyClusters)]) +
theme_AGL +
labs(x = paste(input$Expt, input$Trait), y = paste(input$Select_abc, "* 10000"))
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry),
aes(key1 = Entry, key2 = Name, key3 = Origin, color = Cluster),
color = "Black", fill = "Red", size = 3, pch = 23)
}
mp
})
# input <- list(Plot_Entry = T, Entry = 1)
output$cxb <- renderPlotly({
xx <- m2 %>% left_join(select(pca, Entry, Origin, Region, Cluster), by = "Entry")
x1 <- xx %>%
group_by(Origin) %>%
summarise_at(vars(a, b, c), funs(mean, sd)) %>%
rename(c=c_mean, b=b_mean)
x2 <- x1 %>% mutate(CO = 1) %>%
filter(Origin %in% c("Syria", "Jordan", "Turkey", "Lebanon", "Israel"))
# Plot
find_hull <- function(df) df[chull(df[,"c"], df[,"b"]), ]
polys <- plyr::ddply(x2, "CO", find_hull)
mp <- ggplot(xx %>% filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions),
aes(x = c * 10000, y = b * 10000)) +
geom_polygon(data = polys, fill = NA, color = "black") +
geom_point(aes(key1 = Entry, key2 = Name, key3 = Origin, color = Cluster)) +
scale_color_manual(values = colors[as.numeric(input$MyClusters)]) +
theme_AGL
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry),
aes(key1 = Entry, key2 = Name, key3 = Origin, color = Cluster),
color = "Black", fill = "Red", size = 3, pch = 23)
}
mp
})
# input <- list(TempIncrease = 1, PhotoIncrease = 1, Expts = names_Expt, Plot_Entry = T, Entry = 1, MyClusters = c("a","2","3","4"))
output$T_P_Expts <- renderPlotly({
xx <- dd %>%
select(Entry, Name, Expt, ExptShort, Origin, Cluster, Region, DTF) %>%
left_join(select(m2, Entry, a, b, c), by = "Entry") %>%
left_join(select(ff, Expt, MacroEnv, T_mean, P_mean), by = "Expt") %>%
mutate(T_mean2 = T_mean + input$TempIncrease,
P_mean2 = P_mean + input$PhotoIncrease,
DTF_GW = 1 / (a + b * T_mean2 + c * P_mean2),
DTF_P = 1 / (a + b * T_mean + c * P_mean),
Diff = DTF_P - DTF_GW,
Expt = factor(Expt, levels = names_Expt)) %>%
filter(Expt %in% input$Expts, Cluster %in% input$MyClusters, Region %in% input$MyRegions)
# Plot
mp <- ggplot(xx, aes(x = ExptShort, y = Diff)) +
geom_violin(alpha = 0.3, color = NA) +
geom_quasirandom(size = 0.7, aes(key1 = Entry, key2 = Name, key3 = Origin, color = Cluster)) +
facet_grid(.~MacroEnv, scales = "free_x") + #
theme_AGL +
theme(legend.position = "none",
axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
panel.grid.major.y = element_line(colour = "grey70", size = 0.5)) +
scale_y_continuous(minor_breaks = seq(1,30,1),
breaks = seq(0,30,5)) +
scale_color_manual(values = colors[as.numeric(input$MyClusters)]) +
labs(title = paste("Temperature Increase of", input$TempIncrease),
y = "Decrease in days to flower", x = NULL)
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry),
color = "Black", fill = "Red", size = 2, pch = 23)
}
mp
})
# input <- list(TempIncrease = 1, PhotoIncrease = 1, Expt = names_Expt, Plot_Entry = T, Entry = 1, MyClusters = c("a","2","3","4"))
observe({
output$T_P_Clusters <- renderPlot({
xx <- dd %>%
select(Entry, Name, Expt, ExptShort, Cluster, Region, DTF) %>%
left_join(select(m2, Entry, a, b, c), by = "Entry") %>%
left_join(select(ff, Expt, MacroEnv, T_mean, P_mean), by = "Expt") %>%
mutate(T_mean2 = T_mean + input$TempIncrease,
P_mean2 = P_mean + input$PhotoIncrease,
DTF_GW = 1 / (a + b * T_mean2 + c * P_mean2),
DTF_P = 1 / (a + b * T_mean + c * P_mean),
Diff = DTF_P - DTF_GW,
Expt = factor(Expt, levels = names_Expt)) %>%
filter(Expt %in% input$Expts, Cluster %in% input$MyClusters, Region %in% input$MyRegions)
mp <- ggplot(xx, aes(x = Cluster, y = Diff, fill = Cluster)) +
geom_violin(color = NA, alpha = 0.5) +
geom_quasirandom() +
facet_grid(Expt~., scales = "free") +
theme_AGL +
theme(legend.position = "none") +
scale_fill_manual(name = NULL, values = colors[as.numeric(input$MyClusters)]) +
labs(y = "Decrease in days to flower", x = NULL)
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry),
color = "Black", fill = "Red", size = 3, pch = 23)
}
mp
}, height = 250*length(input$Expts) )
})
# input <- list(TempIncrease = 1, Expt = "Rosthern, Canada 2017", Plot_Entry = T, Entry = 1, MyClusters = c("a","2","3","4"))
output$T_P_Cluster <- renderPlotly({
xx <- dd %>%
select(Entry, Name, Expt, ExptShort, Origin, Region, Cluster, DTF) %>%
left_join(select(m2, Entry, a, b, c), by = "Entry") %>%
left_join(select(ff, Expt, MacroEnv, T_mean, P_mean), by = "Expt") %>%
mutate(T_mean2 = T_mean + input$TempIncrease,
P_mean2 = P_mean + input$PhotoIncrease,
DTF_GW = 1 / (a + b * T_mean2 + c * P_mean2),
DTF_P = 1 / (a + b * T_mean + c * P_mean),
Diff = DTF_P - DTF_GW,
Expt = factor(Expt, levels = names_Expt)) %>%
filter(Expt == input$Expt, Cluster %in% input$MyClusters, Region %in% input$MyRegions)
mp <- ggplot(xx, aes(x = Cluster, y = Diff)) +
geom_violin(aes(fill = Cluster), color = NA, alpha = 0.5) +
geom_quasirandom(aes(key1 = Entry, key2 = Name, key3 = Origin)) +
theme_AGL +
theme(legend.position = "none") +
scale_fill_manual(name = NULL, values = colors[as.numeric(input$MyClusters)]) +
labs(title = paste(input$Expt, "| Temperature Increase of", input$TempIncrease),
y = "Decrease in days to flower", x = NULL)
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry),
aes(key1 = Entry, key2 = Name, key3 = Origin),
color = "Black", fill = "Red", size = 3, pch = 23)
}
mp
})
# input <- list(Mean_T = 13, Mean_P = 12, DTF_Max = 80, DTF_Min = 30, Select_Coef_DTF = "c", Plot_Entry = T, Entry = 1)
output$Predict_DTF_Clusters <- renderPlotly({
xx <- m2 %>% left_join(select(ldp, Entry, Origin, Region, Cluster), by = "Entry") %>%
filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>%
mutate(DTF_Prediction = round(1 / ( a + b * input$Mean_T + c * input$Mean_P ), 1),
Adapted = ifelse(DTF_Prediction > input$DTF_Min & DTF_Prediction < input$DTF_Max, "Adapted", "Not Adapted") )
mp <- ggplot(xx, aes(x = Cluster, y = DTF_Prediction)) +
geom_rect(xmin = 0, xmax = 9, ymin = input$DTF_Min, ymax = input$DTF_Max,
fill = "grey", color = NA, alpha = 0.3) +
geom_violin(aes(fill = Cluster), color = NA, alpha = 0.7) +
geom_quasirandom(aes(key1 = Entry, key2 = Name, key3 = Origin, color = Adapted)) +
scale_fill_manual(values = colors[as.numeric(input$MyClusters)]) +
scale_color_manual(values = c("black", "grey")) +
theme_AGL +
theme(legend.position = "none") +
labs(title = paste("Mean Temperature =", input$Mean_T, "| Mean Photoperiod =", input$Mean_P),
y = "DTF Prediction")
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry), color = "Black", fill = "Red", size = 3, pch = 23)
}
mp
})
# input <- list(Mean_T = 13, Mean_P = 12, DTF_Max = 80, DTF_Min = 30, Select_Coef_DTF = "c", Plot_Entry = T, Entry = 1)
output$Predict_DTF_DTFxCoef <- renderPlotly({
xx <- m2 %>% left_join(select(ldp, Entry, Origin, Region, Cluster), by = "Entry") %>%
filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>%
mutate(DTF_Prediction = round(1 / ( a + b * input$Mean_T + c * input$Mean_P ), 1),
Adapted = ifelse(DTF_Prediction > input$DTF_Min & DTF_Prediction < input$DTF_Max, "Adapted", "Not Adapted") )
mp <- ggplot(xx, aes(x = DTF_Prediction, y = get(input$Select_Coef_DTF) * 10000)) +
geom_rect(ymin = -Inf, ymax = Inf, xmin = input$DTF_Min, xmax = input$DTF_Max,
fill = "grey", color = NA, alpha = 0.3) +
geom_point(aes(key1 = Entry, key2 = Name, key3 = Origin, color = Cluster, pch = Adapted)) +
scale_color_manual(values = colors[as.numeric(input$MyClusters)]) +
scale_shape_manual(values = c(16,1)) +
theme_AGL +
labs(title = paste("Mean Temperature =", input$Mean_T, "| Mean Photoperiod =", input$Mean_P),
y = paste(input$Select_Coef_DTF," * 10000"), x = "Predicted DTF")
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry), color = "Black", fill = "Red", size = 3, pch = 23)
}
mp
})
# input <- list(Mean_T = 13, Mean_P = 12, DTF_Max = 80, DTF_Min = 30, Plot_Entry = T, Entry = 1)
output$Predict_DTF_cxb <- renderPlotly({
xx <- m2 %>% left_join(select(ldp, Entry, Origin, Region, Cluster), by = "Entry") %>%
filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>%
mutate(DTF_Prediction = round(1 / ( a + b * input$Mean_T + c * input$Mean_P ), 1),
Adapted = ifelse(DTF_Prediction > input$DTF_Min & DTF_Prediction < input$DTF_Max, "Adapted", "Not Adapted") )
mp <- ggplot(xx, aes(x = c * 10000, y = b * 10000)) +
geom_point(aes(key1 = Entry, key2 = Name, key3 = Origin, color = Cluster, pch = Adapted)) +
scale_color_manual(values = colors[as.numeric(input$MyClusters)]) +
scale_shape_manual(values = c(16,1)) +
theme_AGL +
labs(title = paste("Mean Temperature =", input$Mean_T, "| Mean Photoperiod =", input$Mean_P))
if(input$Plot_Entry == T) {
mp <- mp + geom_point(data = xx %>% filter(Entry == input$Entry), color = "Black", fill = "Red", size = 3, pch = 23)
}
mp
})
# input <- list(Mean_T = 13, Mean_P = 12, DTF_Max = 80, DTF_Min = 30)
output$Adapted_DTF_Table <- DT::renderDataTable({
xx <- m2 %>% left_join(select(ldp, Entry, Origin, Region, Cluster), by = "Entry") %>%
filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>%
mutate(DTF_Prediction = round(1 / ( a + b * input$Mean_T + c * input$Mean_P ), 1)) %>%
filter(DTF_Prediction > input$DTF_Min, DTF_Prediction < input$DTF_Max) %>%
select(Entry, Name, Origin, Cluster, DTF_Prediction)
}, options = list(pageLength = 324))
# input <- list(Mean_T = 13, Mean_P = 12, DTF_Max = 80, DTF_Min = 30)
output$Adapted_DTF_Table.csv <- downloadHandler(
filename = function() {"Adapted_DTF_Table.csv"},
content = function(file) {
xx <- m2 %>% left_join(select(ldp, Entry, Origin, Region, Cluster), by = "Entry") %>%
filter(Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>%
mutate(DTF_Prediction = round(1 / ( a + b * input$Mean_T + c * input$Mean_P ), 1)) %>%
filter(DTF_Prediction > input$DTF_Min, DTF_Prediction < input$DTF_Max) %>%
select(Entry, Name, Origin, Cluster, DTF_Prediction)
write.csv(xx, file, row.names = F)
}
)
# input <- list(Ad_Country = "Canada", Ad_Countries = c("India"), sd_Mult = 1, Expt = "Rosthern, Canada 2016", Trait = "DTF", Select_Coef_Ad = "c", Plot_Entry = T, Entry = 1)
output$Adapted1 <- renderPlotly({
myOrigins <- unique(c(input$Ad_Country, input$Ad_Countries))
x1 <- dd %>% filter(Expt == input$Expt, Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>%
select(Entry, Trait=input$Trait)
xx <- m2 %>% rename(Coef=input$Select_Coef_Ad) %>%
left_join(select(ldp, Entry, Cluster, Origin), by = "Entry") %>%
left_join(x1, by = "Entry") %>%
mutate(Origin = as.character(Origin),
Origin2 = ifelse(Origin %in% myOrigins, Origin, "Other"),
Trait = ifelse(is.nan(Trait), max(.$Trait,na.rm = T), Trait))
yy <- xx %>%
group_by(Origin) %>%
summarise_at(vars(Trait, Coef), funs(mean, sd), na.rm = T) %>%
rename(Coef=Coef_mean, Trait=Trait_mean) %>%
filter(Origin == input$Ad_Country)
#
myYmin <- yy$Coef - input$sd_Mult * yy$Coef_sd
myYmax <- yy$Coef + input$sd_Mult * yy$Coef_sd
myXmin <- yy$Trait - input$sd_Mult * yy$Trait_sd
myXmax <- yy$Trait + input$sd_Mult * yy$Trait_sd
xx <- xx %>%
mutate(Origin2 = ifelse(Trait > myXmin & Trait < myXmax &
!Origin %in% myOrigins, "\"Adapted\"", Origin2),
Origin2 = factor(Origin2, levels = c("Other", "\"Adapted\"", myOrigins)))
#
mp <- ggplot(yy, aes(y = Coef * 10000, x = Trait)) +
geom_point(data = xx, alpha = 0.7,
aes(key1 = Entry, key2 = Name, key3 = Cluster, key4 = Origin, color = Origin2)) +
geom_point(pch = 18, size = 3) +
geom_errorbar(aes(ymin = myYmin * 10000, ymax = myYmax * 10000)) +
geom_errorbarh(aes(xmin = myXmin, xmax = myXmax)) +
theme_AGL +
scale_color_manual(values = colors[c(7,8,1,3,5,6,2)]) +
labs(x = paste(input$Expt, input$Trait), y = paste(input$Select_Coef_Ad, "* 10000"))
if(input$Plot_Entry == T) {
mp <- mp +
geom_point(data = xx %>% filter(Entry == input$Entry),
color = "Black", fill = "Red", size = 3, pch = 23,
aes(key1 = Entry, key2 = Name, key3 = Cluster, key4 = Origin))
}
mp
})
# input <- list(Ad_Country="Canada", Ad_Countries="India", sd_Mult = 2, Trait = "DTF", Select_Coef_Ad = "c", Expt = "Sutherland, Canada 2018", Plot_Entry = T, Entry = 1)
output$Adapted2 <- renderPlotly({
myOrigins <- unique(c(input$Ad_Country, input$Ad_Countries))
x1 <- dd %>% filter(Expt == input$Expt, Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>%
select(Entry, Trait=input$Trait)
xx <- m2 %>%
left_join(select(ldp, Entry, Cluster, Origin), by = "Entry") %>%
left_join(x1, by = "Entry") %>%
mutate(Origin = as.character(Origin),
Origin2 = ifelse(Origin %in% myOrigins, Origin, "Other"))
yy <- xx %>%
group_by(Origin) %>%
summarise_at(vars(c,b, Trait), funs(mean, sd), na.rm = T) %>%
rename(b=b_mean, c=c_mean, Trait=Trait_mean) %>%
filter(Origin == input$Ad_Country)
#
myYmin <- (yy$b - input$sd_Mult * yy$b_sd)
myYmax <- (yy$b + input$sd_Mult * yy$b_sd)
myXmin <- (yy$c - input$sd_Mult * yy$c_sd)
myXmax <- (yy$c + input$sd_Mult * yy$c_sd)
myDTFmin <- yy$Trait - input$sd_Mult * yy$Trait_sd
myDTFmax <- yy$Trait + input$sd_Mult * yy$Trait_sd
xx <- xx %>%
mutate(Origin2 = ifelse(Trait > myDTFmin & Trait < myDTFmax &
!Origin2 %in% myOrigins, "\"Adapted\"", Origin2),
Origin2 = factor(Origin2, levels = c("Other", "\"Adapted\"", myOrigins)))
#
mp <- ggplot(yy, aes(y = b * 10000, x = c * 10000)) +
geom_point(data = xx, aes(key1 = Entry, key2 = Name, key3 = Cluster, key4 = Origin, color = Origin2), alpha = 0.7) +
geom_point(aes(pch = Origin), size = 3) +
geom_errorbar(aes(ymin = myYmin * 10000, ymax = myYmax * 10000)) +
geom_errorbarh(aes(xmin = myXmin * 10000, xmax = myXmax * 10000)) +
theme_AGL +
scale_shape_manual(name = "Average", values = 18) +
scale_color_manual(values = colors[c(7,8,1,3,5,6,2)]) +
labs(title = input$Expt, x = "c * 10000", y = "b * 10000")
if(input$Plot_Entry == T) {
mp <- mp +
geom_point(data = xx %>% filter(Entry == input$Entry),
color = "Black", fill = "Red", size = 3, pch = 23,
aes(key1 = Entry, key2 = Name, key3 = Cluster, key4 = Origin))
}
mp
})
# input <- list(Ad_Country="Canada", Ad_Countries="India", sd_Mult = 2, Expt = "Sutherland, Canada 2018", Trait = "DTF", Select_Coef_Ad = "c")
output$Adapted_Table.csv <- downloadHandler(
filename = function() {"Adapted_Table.csv"},
content = function(file) {
myOrigins <- unique(c(input$Ad_Country, input$Ad_Countries))
x1 <- dd %>% filter(Expt == input$Expt, Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>%
select(Entry, Trait=input$Trait)
xx <- m2 %>% rename(Coef=input$Select_Coef_Ad) %>%
left_join(select(ldp, Entry, Cluster, Origin), by = "Entry") %>%
left_join(x1, by = "Entry") %>%
mutate(Origin = as.character(Origin),
Origin2 = ifelse(Origin %in% myOrigins, Origin, "Other"),
Trait = round(ifelse(is.nan(Trait), max(.$Trait,na.rm = T), Trait),1))
yy <- xx %>%
group_by(Origin) %>%
summarise_at(vars(Trait, Coef), funs(mean, sd), na.rm = T) %>%
rename(Coef=Coef_mean, Trait=Trait_mean) %>%
filter(Origin == input$Ad_Country)
#
myXmin <- yy$Trait - input$sd_Mult * yy$Trait_sd
myXmax <- yy$Trait + input$sd_Mult * yy$Trait_sd
xx <- xx %>%
mutate(Origin2 = ifelse(Trait > myXmin & Trait < myXmax &
!Origin %in% myOrigins, "\"Adapted\"", Origin2)) %>%
filter(Origin2 == "\"Adapted\"") %>%
select(Entry, Name, Origin, Cluster, Coef, Trait) %>%
mutate(Coef = round(Coef * 10000, 1))
#colnames(xx)[colnames(xx)=="Coef"] <- input$Select_Coef_Ad
#colnames(xx)[colnames(xx)=="Trait"] <- input$Trait
write.csv(xx, file, row.names = F)
}
)
# input <- list(Ad_Country="Canada", Ad_Countries="India", sd_Mult = 2, Expt = "Sutherland, Canada 2018", Trait = "DTF", Select_Coef_Ad = "c")
output$Adapted_Table <- DT::renderDataTable({
myOrigins <- unique(c(input$Ad_Country, input$Ad_Countries))
x1 <- dd %>% filter(Expt == input$Expt, Cluster %in% input$MyClusters, Region %in% input$MyRegions) %>%
select(Entry, Trait=input$Trait)
xx <- m2 %>% rename(Coef=input$Select_Coef_Ad) %>%
left_join(select(ldp, Entry, Cluster, Origin), by = "Entry") %>%
left_join(x1, by = "Entry") %>%
mutate(Origin = as.character(Origin),
Origin2 = ifelse(Origin %in% myOrigins, Origin, "Other"),
Trait = round(ifelse(is.nan(Trait), max(.$Trait,na.rm = T), Trait),1))
yy <- xx %>%
group_by(Origin) %>%
summarise_at(vars(Trait, Coef), funs(mean, sd), na.rm = T) %>%
rename(Coef=Coef_mean, Trait=Trait_mean) %>%
filter(Origin == input$Ad_Country)
#
myXmin <- yy$Trait - input$sd_Mult * yy$Trait_sd
myXmax <- yy$Trait + input$sd_Mult * yy$Trait_sd
xx <- xx %>%
mutate(Origin2 = ifelse(Trait > myXmin & Trait < myXmax &
!Origin %in% myOrigins, "\"Adapted\"", Origin2)) %>%
filter(Origin2 == "\"Adapted\"") %>%
select(Entry, Name, Origin, Cluster, Coef, Trait) %>%
mutate(Coef = round(Coef * 10000, 1))
#colnames(xx)[colnames(xx)=="Coef"] <- input$Select_Coef_Ad
#colnames(xx)[colnames(xx)=="Trait"] <- input$Trait
}, options = list(pageLength = 324))
}
# Run the application
shinyApp(ui = ui, server = server)