-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
590 lines (514 loc) · 20.3 KB
/
server.R
File metadata and controls
590 lines (514 loc) · 20.3 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
server <- function(input, output, session) {
isloaded= try(load_vpalmr())
output$loading= renderText({
if(isloaded==0){
"Vpalmr is up-to-date"
}else if(isloaded==1){
"Vpalmr was successfully updated"
}else if(isloaded==2|inherits(isloaded,"try-error")){
paste("Could not install Vpalmr (could not fetch from Github.com at VEZY/Vpalmr., or could not install).",
"VPALM-IDE will use the current local version of Vpalmr. \n")
}
})
shinyjs::toggle("loading_page")
shinyjs::toggle("loaded")
map= reactive({
paste("Month after planting=", input$map)
})
output$architecture1= renderText({map()})
output$architecture2= renderText({map()})
# Architecture parameter fit: ---------------------------------------------
# Either read from file:
Palm_Param_previous=
eventReactive(input$params_previous, {
message("Load previous parameters fit\n")
readRDS(input$params_previous$datapath)
})
# Or read the inputs local folder or files:
# Find the right Palm_data_* according to the choices of the user ---------------
# NB: choices are either loading from default folder, loading from user-chosen folder,
# or by uploading each files separately.
Palm_data= reactive({
switch(input$load,
"Load data from default folder" = Palm_data_folder(),
"Load data from folder" = Palm_data_folder(),
"Load each file separately" = Palm_data_files())
})
volumes_data= c(Home = fs::path_home(), getVolumes()(), WD= getwd(),
"R Installation" = R.home())
shinyDirChoose(input, "dirdata", roots = volumes_data, session = session,
restrictions = system.file(package = "base"))
dir_data= reactive(input$dirdata)
# Files in the data folder:
output$files_data= renderPrint(list.files(parseDirPath(volumes_data,dir_data())))
output$dir_data= renderPrint({
parseDirPath(volumes_data, dir_data())
})
# Display info about the user-defined path to the data:
observeEvent(input$dirdata, {
output$data_path_info1= renderText({
paste("Here is the folder you chose:",
parseDirPath(volumes_data, dir_data()))
})
output$data_path_info2= renderText({
"Here is a list of the data in the folder:"
})
output$data_path_info3= renderPrint({
list.files(parseDirPath(volumes_data,dir_data()))
})
})
# If import from folder, either use the default path, or one specified by the user:
folder_path=
reactive({
if(input$load == "Load data from default folder"){
file.path(getwd(), "0-data/Archi")
}else{
parseDirPath(volumes_data, dir_data())
}
})
Palm_data_folder=
eventReactive(input$load_data_folder, {
dir_path= folder_path()
data_path=
list(
parameter= file.path(dir_path,'ParameterSimu.csv'),
development= file.path(dir_path,'Development_Rep4_SMSE.csv'),
phylotaxy= file.path(dir_path,'Stem_SMSE14.csv'),
declination= file.path(dir_path,'AnglesC&A_SMSE_Nov14.csv'),
curvature= file.path(dir_path,'LeafCurvature_SMSE14.csv'),
leaf_area= file.path(dir_path,'LeafArea_monitoring_SMSE.csv'),
axial_angle= file.path(dir_path,'LeafDispositionComp_SMSE14.csv'),
petiole_width= file.path(dir_path,'Petiole_SMSE14.csv'),
twist= file.path(dir_path,'Torsion_SMSE14.csv')
)
map_num= as.numeric(input$map)
if(!is.na(map_num)){
data_path$map= map_num
}else{
return('Error')
}
Inputs=
tryCatch(
do.call(Vpalmr::import_data, data_path),
error = function(out){
return('Error')
})
if(all(Inputs!="Error")){
message("Data successfully imported")
}
Inputs
})
# Or read the inputs from user-inputed files:
Palm_data_files=
eventReactive(
input$submit_upload,
{
cat("\nImporting data for ", input$map, " months after planting\n")
tryCatch(
do.call(Vpalmr::import_data, list(parameter= input$param_file$datapath,
development= input$param_dev$datapath,
phylotaxy= input$param_phylotaxy$datapath,
declination= input$param_dec$datapath,
curvature= input$param_curv$datapath,
leaf_area= input$param_la$datapath,
axial_angle= input$param_axial_angle$datapath,
petiole_width= input$param_petiole_width$datapath,
twist= input$param_twist$datapath, map= input$map)),
error = function(out){
message("Error during Vpalmr::import_data execution")
message("Original function error:")
message(out)
})
})
# trigger the display of the Parameter data.frame from the data just read (for control):
output$data_trigger=
renderText({
if(isTruthy(Palm_data())){
if(all(Palm_data()=="Error")){
showNotification("Given MAP does not exist in the data")
'notok'
}else if(is.data.frame(Palm_data()$Parameter)&&
nrow(Palm_data()$Area)>0){
showNotification("Data successfully imported")
'ok'
}else{
showNotification("Given MAP does not yield enough data")
'notok'
}
}else{
'notyet'
}
})
outputOptions(output, "data_trigger", suspendWhenHidden = FALSE)
output$progeny_filt = renderUI({
checkboxGroupInput(inputId = 'prog_filt', label = 'Filter progenies:',
choices = unique(Palm_data()$Parameter$Progeny),
selected= unique(Palm_data()$Parameter$Progeny))
})
# Filter the data according to prog_filt (output$progeny_filt):
Palm_data_filt= reactive({
lapply(Palm_data(), function(x){
if(is.data.frame(x)){
x[x$Progeny%in%input$prog_filt,]
}else{
x
}
})
})
output$datashow=
DT::renderDataTable({
Palm_data_filt()$Parameter
}, options = list(pageLength = 5))
output$data_filt_trigger=
renderText({
if(!is.null(Palm_data_filt())){
is_data=
lapply(Palm_data_filt()[-grep("MAP_requested",names(Palm_data_filt()))],nrow)%>%
unlist
}else{
is_data=1
}
if(any(is_data<1)){
nodata= names(is_data[is_data<1])
# showNotification(paste("Missing data for selected Progenies"))
# "notok"
paste(nodata, collapse= ", ")
}else{
"ok"
}
})
outputOptions(output, "data_filt_trigger", suspendWhenHidden = FALSE)
mods=
eventReactive(input$updatearchi, {
# updateSliderInput(session, "map", value = input$map,
# min = min(Palm_data_filt()$Parameter$MAP),
# max = max(Palm_data_filt()$Parameter$MAP))
if(!is.null(Palm_data_filt()$Parameter)){
# Create a Progress object
progress_obj <- shiny::Progress$new()
progress_obj$set(message = "Computing data", value = 0)
# Close the progress when this reactive exits (even if there's an error)
on.exit(progress_obj$close())
# Fit the models on data:
models= mod_all(x= Palm_data_filt(), progress =
function(x){
updateProgress(detail = x, progress_obj = progress_obj,
steps= 21)})
}else{
models= NULL
}
models
})
output$modout=
renderText({
is(mods()) # trigger when mods is created
paste("Architectural parameters successfully computed !")
})
Palm_Param_computed= reactive({
list(input= isolate(Palm_data_filt()), model= mods())
})
output$parameters_description=
renderDataTable({default_params()},escape = FALSE)
# Reading the parameters template:
template= reactive(readRDS("0-data/VPalm_list/vpalm_template.rds"))
Palm_Param_custom=
reactive({
param_list= template()
# param_list$nbFronds_M= input$nbleaves_custom
param_list$MAP_requested= input$MAP_custom
param_list$nbLeafEmitted= round(input$MAP_custom*2.5)
param_list$rachisLengthIntercept=
input$rachisLengthRank1 - input$rachisLengthSlope * param_list$nbLeafEmitted
param_list$rachisLengthSlope= input$rachisLengthSlope
param_list$nbMax= input$nbMax
param_list$nbSlope= input$nbSlope
param_list$nbInfl= input$nbInfl
param_list$lenfletLengthAtBIntercept=
input$B_length_Rank1 - input$rachisLengthRank1 * input$leafletLengthAtBSlope
param_list$leafletLengthAtBSlope= input$leafletLengthAtBSlope
param_list$bWidthIntercept=
input$B_width_Rank1 - input$rachisLengthRank1 * input$bWidthSlope
param_list$bWidthSlope= input$bWidthSlope
param_list$xm_intercept= input$xm_intercept
param_list$xm_slope= input$xm_slope
param_list$ym_intercept= input$ym_intercept
param_list$ym_slope= input$ym_slope
param_list$petioleRachisRatio_M= input$petioleRachisRatio_M
param_list$decMaxA= input$decMaxA
param_list$decSlopeA= input$decSlopeA
param_list$decInflA= input$decInflA
param_list$decliCintercept= input$decliCintercept
param_list$decliCslope= input$decliCslope
list(custom= param_list)
})
# Find the right Palm_Param_* according to the choices of the user ---------
# NB: either load previous, compute it or user-defined values
Palm_Param= reactive({
switch(input$previous,
"Load previous computation" = Palm_Param_previous(),
"Compute new parameters" = Palm_Param_computed(),
"User-defined parameters (exploration)" = Palm_Param_custom())
})
observeEvent(Palm_Param_computed(), {
showNotification("Parameters successfully computed")
})
observeEvent(Palm_Param_previous(), {
showNotification("Parameters successfully loaded")
})
# Return names of input and models from loaded model.Rdata for checking:
observeEvent(input$params_previous,{
output$title_data_archi <- renderText({
"Architectural data:"
})
output$data_archi <- renderText({
paste(names(Palm_Param()$input), collapse= ', ')
})
output$title_data_models <- renderText({
"Models:"
})
output$data_models <- renderText({
paste(names(Palm_Param()$model), collapse= ', ')
})
})
# trigger the display of the download button when parameters are available:
output$param_trigger=
renderText({
ifelse(length(Palm_Param())==2|input$save_vpalm>0,'ok','notok')
})
outputOptions(output, "param_trigger", suspendWhenHidden = FALSE)
# Make the data downloadable:
output$downloadData= downloadHandler(
filename = function() {
paste0("models_MAP_",input$map,"_Prog_",
paste(unique(Palm_Param()$input$Parameter$Progeny), collapse = "_"),
".RData")
},
content = function(file) {
saveRDS(Palm_Param(), file)
}
)
# Page 2: Call VPalm and build OPF/OPS files ------------------------------
output$progeny = renderUI({
if(length(Palm_Param())==2){
Progs= unique(Palm_Param()$input$Parameter$Progeny)
}else if(input$save_vpalm>0){
Progs= "custom"
}
Progs_choices=
if(length(Progs)>1){
c("All progenies","Average progeny",Progs)
}else{
list(Progs)
}
selectInput(inputId = 'prog', 'Progeny', Progs_choices)
})
# directory where to write the outputs:
volumes= c(Home = fs::path_home(), getVolumes()(), WD= getwd(), "R Installation" = R.home())
shinyDirChoose(input, "dir", roots = volumes, session = session, restrictions = system.file(package = "base"))
dir= reactive(input$dir)
output$dir= renderPrint({
parseDirPath(volumes, input$dir)
})
output$dirtrigger= renderText({
if(isTruthy(dir())){"Here is the folder you chose for the outputs:"}else{""}
})
output$dirtrigger2=
renderText({
if(isTruthy(dir())){"Now you can set the parameters:"}else{""}
})
# Make the output paths available for ui for display as information:
output$dir_vpalm_inputs= renderText({
paste("Vpalm input files written in:",
file.path(parseDirPath(volumes, input$dir),"VPalm_inputs"))
})
output$dir_scenes= renderText({
paste("Vpalm output files (OPS and OPF files) written in:",
file.path(parseDirPath(volumes, input$dir),"scenes"))
})
# Update the initial (default) seed value according to the number of trees.
observeEvent(input$nbtrees, {
updateTextInput(session, "seed",
value =
if(input$nbtrees==0|!isTruthy(input$nbtrees)){
NULL
}else{
paste(1:input$nbtrees, collapse = ",")
}
)
})
seeds= reactive(
if(isTruthy(input$seed)){
as.numeric(unlist(strsplit(input$seed,",")))
}else{
NULL
}
)
output$design_ex=
renderTable(
Vpalmr::design_plot(rows = 2, cols = 1, x_dist = 9.21)$design%>%
dplyr::select(.data$x,.data$y,.data$z,.data$xmin, .data$ymin,
.data$xmax, .data$ymax,.data$scale,
.data$inclinationAzimut,.data$inclinationAngle,
.data$stemTwist)
)
custom_design= reactive(
if(isTruthy(input$planting_design)){
data.table::fread(file = input$planting_design$datapath, data.table = FALSE)
}else{
NULL
}
)
scenes=
eventReactive(
input$makescene,
{
progress_obj <- shiny::Progress$new()
progress_obj$set(message = "Making scene:", value = 0)
# Close the progress when this reactive exits (even if there's an error)
on.exit(progress_obj$close())
if(length(Palm_Param())==2){
scene=
Vpalmr::make_scene(data = Palm_Param(),
nleaves = input$nleaves,
path = parseDirPath(volumes, dir()),
Progeny =
if(input$prog=="All progenies"){
NULL
}else if(input$prog=="Average progeny"){
"Average"
}else{
input$prog
},
AMAPStudio = getwd(),
ntrees = ifelse(is.na(input$nbtrees),0,input$nbtrees),
plant_dist = input$plant_dist,
plot_design=
if(!isTruthy(input$planting_design)){
NULL
}else{
custom_design()
},
seed= if(is.na(input$nbtrees)){NULL}else{seeds()},
progress = function(x){
updateProgress(detail = x, progress_obj = progress_obj,
steps = 7)
})
message("Vpalmr::extract_progeny() ran successfully")
}else if(input$save_vpalm>0){
scene=
make_scene_custom(x = Palm_Param()$custom,
path = parseDirPath(volumes, dir()),
AMAPStudio = getwd(),
planting_design=
if(!isTruthy(input$planting_design)){
NULL
}else{
custom_design()
},
plant_dist= input$plant_dist,
name= if(isTruthy(input$custom_name)){
input$custom_name
}else{
NULL
},
progress=function(x){
updateProgress(detail = x, progress_obj = progress_obj,
steps = 7)
})
}else{
scene= NULL
}
scene
})
# trigger the display of the paths where the outputs were written:
output$scene_trigger=
renderText({
ifelse(!is.null(scenes()),'ok','notok')
})
outputOptions(output, "scene_trigger", suspendWhenHidden = FALSE)
observeEvent(scenes(), {
showNotification("Scene successfully created and written")
})
output$plot_design <- renderPlot({
plot_design= scenes()$plot_design
ranges= range(plot_design$xmax,plot_design$ymax,plot_design$xmin,plot_design$ymin)
plot_design%>%
mutate(image= 'www/palm_tiny.png')%>%
ggplot2::ggplot(ggplot2::aes(x= x, y= y))+
ggplot2::coord_fixed()+
ggplot2::geom_point()+
ggimage::geom_image(ggplot2::aes(image= image), size= input$palm_size)+
ggplot2::geom_point(ggplot2::aes(color= "Palm tree center"))+
ggplot2::geom_polygon(data= polygon_coord(plot_design),
ggplot2::aes(x= x, y= y, fill= "Plot limits",
color= "Plot limits"), alpha= 0.2)+
ggplot2::ylim(low= ranges[1], high= ranges[2])+
ggplot2::xlim(low= ranges[1], high= ranges[2])+
ggplot2::labs(colour = "", fill= "")+
ggplot2::theme(legend.position="bottom")
})
output$plot_info <- renderText({
paste0(
"Value on click: ", xy_str(input$plot_click),
"Value on hover: ", xy_str(input$plot_hover),
"Diagonal length: ", xy_length(input$plot_brush),
"Selection: ", xy_range_str(input$plot_brush)
)
})
output$plot_design_rep <- renderPlot({
plot_design= scenes()$plot_design
# Matrix of the design (each cell is a Voronoi):
mat_plot= expand.grid(Row= 1:input$voronois,
Col= 1:input$voronois)
# Full design:
design=
mapply(function(Row,Col){
plot_design%>%
select(x,y,xmax,ymax,xmin,ymin)%>%
mutate(xmin= xmax*(Col-1), ymin= ymax*(Row-1),
x= x+xmin, y= y+ymin,
xmax= xmax*Col, ymax= ymax*Row,
Col= Col, Row= Row)
}, Row= mat_plot$Row, Col= mat_plot$Col)%>%t()%>%as_tibble()%>%
tidyr::unnest()
ranges_full= range(design$xmax,design$ymax,design$xmin,design$ymin)
voronoi_stands=
design%>%
mutate(group= paste0('x:',Col,", y:",Row))%>%
group_by(group)%>%
summarise(coords=
list(expand.grid(x= unique(c(xmin,xmax)),
y= unique(c(ymin,ymax))))
)%>%
mutate(v_id= as.factor(1:n()))%>%
tidyr::unnest()%>%
group_by(v_id)%>%
mutate(pos= ifelse(x==min(x)&y==min(y),1,
ifelse(x==min(x)&y==max(y),2,
ifelse(x==max(x)&y==min(y),4,3))))%>%
arrange(v_id,pos)
design%>%
mutate(image= 'www/palm_tiny.png')%>%
ggplot2::ggplot(ggplot2::aes(x= .data$x, y= .data$y))+
ggimage::geom_image(ggplot2::aes(image= image), size= input$palm_size/3)+
ggplot2::geom_point(ggplot2::aes(color= "Palm tree center"))+
ggplot2::ylim(low= ranges_full[1], high= ranges_full[2])+
ggplot2::xlim(low= ranges_full[1], high= ranges_full[2])+
ggplot2::theme(legend.position="bottom")+
ggplot2::labs(fill= "Voronoï index", x= "x coordinate (m)", y= "y coordinate (m)")+
ggplot2::geom_polygon(data= voronoi_stands,
ggplot2::aes(x= x, y= y, fill= v_id, color= v_id), alpha= 0.2)+
ggplot2::guides(color= FALSE)+
ggplot2::coord_fixed()
})
output$plot_info_rep <- renderText({
paste0(
"Value on click: ", xy_str(input$plot_click_rep),
"Value on hover: ", xy_str(input$plot_hover_rep),
"Diagonal length: ", xy_length(input$plot_brush),
"Selection: ", xy_range_str(input$plot_brush_rep)
)
})
}
# Run the application
# shinyApp(ui = ui, server = server)