generated from centre-for-humanities-computing/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
666 lines (604 loc) · 24.3 KB
/
app.R
File metadata and controls
666 lines (604 loc) · 24.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
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
# guides
# https://mastering-shiny.org/action-dynamic.html
library(shiny)
library(sf)
library(grid)
library(gridExtra)
library(bslib)
library(zip)
library(shinyjs)
library(markdown)
source("R/utils_Rdata.R")
# loading the text for showing in the server
name_text <- paste(readLines("data/texts/name.txt"), collapse = "\n")
ui <- fluidPage(
theme = bslib::bs_theme(version = 5, bootswatch = "flatly"),
# loading css style sheet
tags$head(includeCSS("www/style.css")),
# tags$head(
# tags$link(rel = "stylesheet", type = "text/css", href = "style.css"),
# ),
useShinyjs(), # for toggling download button on and off
sidebarLayout(fluid = F,
sidebarPanel(width = 3,
HTML("<br><br>"),
h3("Settings", style = "text-align: center"),
HTML("<br>"),
id = "sidebar_id",
# h3("Settings", style = "text-align: center; margin: 20px 0px 40px 0px"),
navbarPage("", id = "side_navbar",
tabPanel("Test",
h4("Simulations with Montecristi Dataset"),
hr(style = "margin: 0px 20px 20px 0px"),
fluidPage(
numericInput("mc_simulations_1", label = HTML(
'Number of Monte Carlo simulations
<span class="info-icon">ⓘ
<div class="hoverbox">
Number of simulationes can be anything, but we recommend between 1000-5000. Beware, values below 25 and above 10.000 can cause the processing to fail.
</div>
</span>'), value = 25, min = 1, max = 10000),
br(),
numericInput("nbRobSc_1", label = HTML(
'Number of Robustness Scenarios
<span class="info-icon">ⓘ
<div class="hoverbox">
Number of robustness scenarios can be anything, but we recommend between 100-3000.
</div>
</span>'), value = 5, min = 1, max = 10000),
br(),
HTML(
'Quantile Selection
<span class="info-icon">ⓘ
<div class="hoverbox">
The most comprehensive comparison is achieved by using all options, but the processing is faster with fewer options.
</div>
</span>'),
accordion(
accordion_panel("Expand for options",
# checkboxInput("quantile_50_test", "50% quantile", FALSE),
# checkboxInput("quantile_80_test", "80% quantile", FALSE),
checkboxInput("quantile_90_test", "90% quantile", FALSE),
checkboxInput("quantile_95_test", "95% quantile", TRUE),
checkboxInput("quantile_98_test", "98% quantile", FALSE),
checkboxInput("quantile_99_test", "99% quantile", FALSE),
checkboxInput("quantile_995_test", "99.5% quantile", FALSE),
), open = FALSE
),
HTML("<br><br>"),
splitLayout(cellWidths = c("50%", "50%"),
div(actionButton("submit_button_1", "Submit", class = "btn-success"), id = "div_submit_button"),
div(disabled(downloadButton("download1")), id = "div_download_button")),
HTML("<br><br><br>"),
)
),
tabPanel("Upload",
h4("Simulations with uploaded dataset"),
hr(style = "margin: 0px 20px 20px 0px"),
fluidPage(
fileInput("upload1", label = HTML(
'Upload Spatial Points
<span class="info-icon">ⓘ
<div class="hoverbox">
Upload a .shp <i>and</i> a .shx file. Files must be uploaded simultaneously, so hold SHIFT down while selecting files.
</div>
</span>'
), accept = c(".shp", ".shx"), multiple = TRUE),
br(),
fileInput("upload2", label = HTML(
'Upload Spatial Polygons
<span class="info-icon">ⓘ
<div class="hoverbox">
Upload a .shp <i>and</i> a .shx file. Files must be uploaded simultaneously, so hold SHIFT down while selecting files.
</div>
</span>'), accept = c(".shp", ".shx"), multiple = TRUE),
br(),
numericInput("mc_simulations_2", label = HTML(
'Number of Monte Carlo simulations
<span class="info-icon">ⓘ
<div class="hoverbox">
Number of simulationes can be anything, but we recommend between 1000-5000. Beware, values below 25 can cause the processing to fail.
</div>
</span>'), value = 25, min = 1, max = 10000),
br(),
numericInput("nbRobSc_2", label = HTML(
'Number of Robustness Scenarios
<span class="info-icon">ⓘ
<div class="hoverbox">
Number of robustness scenarios can be anything, but we recommend between 100-3000.
</div>
</span>'), value = 10, min = 1, max = 10000),
br(),
HTML(
'Quantile Selection
<span class="info-icon">ⓘ
<div class="hoverbox">
The most comprehensive comparison is achieved by using all options, but the processing is faster with fewer options.
</div>
</span>'),
accordion(
accordion_panel("Expand for options",
# checkboxInput("quantile_50_upload", "50% quantile", FALSE),
# checkboxInput("quantile_80_upload", "80% quantile", FALSE),
checkboxInput("quantile_90_upload", "90% quantile", FALSE),
checkboxInput("quantile_95_upload", "95% quantile", TRUE),
checkboxInput("quantile_98_upload", "98% quantile", FALSE),
checkboxInput("quantile_99_upload", "99% quantile", FALSE),
checkboxInput("quantile_995_upload", "99.5% quantile", FALSE),
), open = FALSE
),
HTML("<br><br>"),
splitLayout(cellWidths = c("50%", "50%"),
div(actionButton("submit_button_2", "Submit", class = "btn-success"), id = "div_submit_button"),
div(disabled(downloadButton("download2")), id = "div_download_button")),
HTML("<br><br><br>"),
)
),
),
),
mainPanel(width = 9,
HTML("<br><br>"),
h2(name_text),
HTML("<br>"),
navbarPage(title = "", id = "main_tab",
tabPanel("Home",
hr(style = "margin: 0px 20px 20px 0px"),
fluidPage(
uiOutput("test"),
HTML("<i> For the best experience on the web app, we recommend zooming out the browser window to 75% or 80%. </i>"),
HTML("<br><br>"),
h4("Introduction"),
includeMarkdown("data/texts/introduction.md"),
HTML("<br>"),
actionButton("toggle_how_to", "▼ How to use", class = "header-btn"),
div(id = "how_to_content", style = "display: none;",
includeMarkdown("data/texts/how_to.md")),
HTML("<br><br>"),
h4("Results"),
includeMarkdown("data/texts/results.md"),
HTML("<br>"),
h4("Creators and Contact"),
includeMarkdown("data/texts/creators.md"),
# HTML("<br><br>"),
includeMarkdown("data/texts/contact.md"),
HTML("<br>"),
h4("Funding Information"),
includeMarkdown("data/texts/funding_information.md"),
HTML("<br><br>"),
splitLayout(cellWidths = c("33%", "33%", "34%"),
div(a(
href = "https://research-and-innovation.ec.europa.eu/funding/funding-opportunities/funding-programmes-and-open-calls/horizon-europe_en",
img(src = "images/Funded-by-the-European-Union.png", width = "70%"),
target = "_blank"
)
),
div(a(
href = "https://marie-sklodowska-curie-actions.ec.europa.eu/",
img(src = "images/logo_marie-curie.jpg", width = "30%", style = "margin-left: 20%"),
target = "_blank"
)
),
div(a(
href = "https://international.au.dk/",
img(src = "images/aarhus-university-au-3-logo.png", width = "70%"),
target = "_blank"
)
)
),
# HTML("<br><br>"),
class = "home_page")
),
tabPanel("Output",
hr(style = "margin: 0px 0px 20px 0px"),
fluidPage(
HTML("<br>"),
tabsetPanel(
tabPanel("PCF based on 100% of sites",
uiOutput("pcf_original_conditional")
),
tabPanel("Robustness PCF",
uiOutput("pcf_robust_conditional")
),
tabPanel("Comparison Tools",
uiOutput("comparison_tools_conditional")
)
),
class = "home_page"),
),
tabPanel("Methods",
hr(style = "margin: 0px 0px 20px 0px"),
fluidPage(
HTML("<br>"),
h4("Framework and background"),
includeMarkdown("data/texts/method.md"),
class = "home_page"),
),
tabPanel("More Info",
hr(style = "margin: 0px 0px 20px 0px"),
fluidPage(
HTML("<br>"),
h4("Original data"),
includeMarkdown("data/texts/original_data.md"),
h4("Original paper"),
includeMarkdown("data/texts/original_paper.md"),
h4("Code repository"),
includeMarkdown("data/texts/github.md"),
h4("Data privacy"),
includeMarkdown("data/texts/data_privacy.md"),
class = "home_page"),
), selected = "Home"
),
)
),
)
server <- function(input, output, session) {
# Reactive value to track if the function has run
function_ran <- reactiveVal(FALSE)
function_error <- reactiveVal(FALSE)
# object to hold output from the function
data_output <- reactiveValues(data = NULL)
observeEvent(input$toggle_how_to, {
toggle("how_to_content") # Shows/hides the how-to section
})
# Observer to start function processing
observeEvent(input$submit_button_1, {
shinyjs::disable("submit_button_1")
shinyjs::disable("submit_button_2")
shinyjs::disable("download1")
shinyjs::disable("download2")
function_error(FALSE)
function_ran(FALSE)
file_shp <- st_read("data/montecristi/mc-db-95-clean.shp")
file_poly <- st_read("data/montecristi/nmcpoly1.shp")
quantiles <- c()
if (input$quantile_90_test){
quantiles <- c(quantiles, 0.9)
}
if (input$quantile_95_test){
quantiles <- c(quantiles, 0.95)
}
if (input$quantile_98_test){
quantiles <- c(quantiles, 0.98)
}
if (input$quantile_99_test){
quantiles <- c(quantiles, 0.99)
}
if (input$quantile_995_test){
quantiles <- c(quantiles, 0.995)
}
data_output$data <- tryCatch({big_processing_func(file_shp = file_shp,
file_poly = file_poly,
nsim = as.integer(input$mc_simulations_1/5), # it should be divided by number of clusters
clusters = 5,
nbRobSc = input$nbRobSc_1,
quantiles = quantiles,
quantile_50 = TRUE)}, error = function(e) {return(NULL)})
if(is.null(data_output$data)){
function_error(TRUE)
} else {
function_ran(TRUE)
}
updateTabsetPanel(session, "main_tab",
selected = "Output"
)
shinyjs::enable("submit_button_1")
shinyjs::enable("submit_button_2")
shinyjs::enable("download1")
})
# Observer to start function processing
observeEvent(input$submit_button_2, {
shinyjs::disable("submit_button_1")
shinyjs::disable("submit_button_2")
shinyjs::disable("download1")
shinyjs::disable("download2")
shp_upload1 <- input$upload1
shp_upload2 <- input$upload2
tempdirname_1 <- dirname(shp_upload1$datapath[1])
tempdirname_2 <- dirname(shp_upload2$datapath[1])
# Rename files
for (i in 1:nrow(shp_upload1)) {
file.rename(
shp_upload1$datapath[i],
paste0(tempdirname_1, "/", shp_upload1$name[i])
)
}
# Rename files
for (i in 1:nrow(shp_upload2)) {
file.rename(
shp_upload2$datapath[i],
paste0(tempdirname_2, "/", shp_upload2$name[i])
)
}
file_shp <- st_read(paste(tempdirname_1, shp_upload1$name[grep(pattern = "*.shp$", shp_upload1$name)], sep = "/"))
file_poly <- st_read(paste(tempdirname_2, shp_upload2$name[grep(pattern = "*.shp$", shp_upload2$name)], sep = "/"))
quantiles <- c()
if (input$quantile_90_upload){
quantiles <- c(quantiles, 0.9)
}
if (input$quantile_95_upload){
quantiles <- c(quantiles, 0.95)
}
if (input$quantile_98_upload){
quantiles <- c(quantiles, 0.98)
}
if (input$quantile_99_upload){
quantiles <- c(quantiles, 0.99)
}
if (input$quantile_995_upload){
quantiles <- c(quantiles, 0.995)
}
data_output$data <- tryCatch({big_processing_func(file_shp = file_shp,
file_poly = file_poly,
nsim = as.integer(input$mc_simulations_2/5), # it should be divided by number of clusters
clusters = 5,
nbRobSc = input$nbRobSc_2,
quantiles = quantiles,
quantile_50 = TRUE)}, error = function(e) {return(NULL)})
if(is.null(data_output$data)){
function_error(TRUE)
} else {
function_ran(TRUE)
}
updateTabsetPanel(session, "main_tab",
selected = "Output"
)
shinyjs::enable("submit_button_1")
shinyjs::enable("submit_button_2")
shinyjs::enable("download2")
})
output$plot_100_1 <- renderPlot({
grid.draw(data_output$data[[1]])
}, res = 96)
output$plot_50_1 <- renderPlot({
grid.draw(data_output$data[[2]][["50"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_50_2 <- renderPlot({
grid.draw(data_output$data[[2]][["50"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_60_1 <- renderPlot({
grid.draw(data_output$data[[2]][["60"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_60_2 <- renderPlot({
grid.draw(data_output$data[[2]][["60"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_70_1 <- renderPlot({
grid.draw(data_output$data[[2]][["70"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_70_2 <- renderPlot({
grid.draw(data_output$data[[2]][["70"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_80_1 <- renderPlot({
grid.draw(data_output$data[[2]][["80"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_80_2 <- renderPlot({
grid.draw(data_output$data[[2]][["80"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_90_1 <- renderPlot({
grid.draw(data_output$data[[2]][["90"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_90_2 <- renderPlot({
grid.draw(data_output$data[[2]][["90"]][[sample(data_output$data[[4]], 1)]])
}, res = 96)
output$plot_compare <- renderPlot({
grid.draw(data_output$data[[3]])
}, res = 130)
# Dynamically update the content of the tabPanel based on whether the function has run
output$pcf_original_conditional <- renderUI({
# Wait until the function has run
if (function_ran()) {
tagList(
HTML("<br>"),
h4("Pair Correlation Function based on 100% of sites"),
HTML("<br>"),
plotOutput("plot_100_1", width = "100%")
)
} else if (function_error()) {
div(
class = "center-content", # Wrapper to center content
div(
class = "error-message", # Custom error styling
"An error occured. This is likely due to either a too low or too high number of Monte Carlo simulations.
Try running it again with a number of simluations between 25 and 10.000.
If the error persists then it is likely because there is no value for which the curve jumps in the original data,
meaning the data is inappropriate for this analysis. Try with a different dataset."
)
)
} else {
div(
class = "center-content", # Wrapper to center content
div(
class = "error-message", # Custom error styling
"Run the function to see the content of this tab."
)
)
}
})
output$pcf_robust_conditional <- renderUI({
# Wait until the function has run
if (function_ran()) {
tagList(
HTML("<br>"),
h4("Robustness PCF"),
HTML("<br>"),
p("Only two randonmly sampled robustness scenarios are shown here. The rest can be downloaded by clicking the download button."),
tabsetPanel(
tabPanel("90% of points",
HTML("<br>"),
plotOutput("plot_90_1", width = "100%"),
HTML("<br><br>"),
plotOutput("plot_90_2", width = "100%"),
HTML("<br><br>")
),
tabPanel("80% of points",
HTML("<br>"),
plotOutput("plot_80_1", width = "100%"),
HTML("<br><br>"),
plotOutput("plot_80_2", width = "100%"),
HTML("<br><br>")
),
tabPanel("70% of points",
HTML("<br>"),
plotOutput("plot_70_1", width = "100%"),
HTML("<br><br>"),
plotOutput("plot_70_2", width = "100%"),
HTML("<br><br>")
),
tabPanel("60% of points",
HTML("<br>"),
plotOutput("plot_60_1", width = "100%"),
HTML("<br><br>"),
plotOutput("plot_60_2", width = "100%"),
HTML("<br><br>")
),
tabPanel("50% of points",
HTML("<br>"),
plotOutput("plot_50_1", width = "100%"),
HTML("<br><br>"),
plotOutput("plot_50_2", width = "100%"),
HTML("<br><br>")
)
)
)
} else if (function_error()) {
div(
class = "center-content", # Wrapper to center content
div(
class = "error-message", # Custom error styling
"An error occured. This is likely due to either a too low or too high number of Monte Carlo simulations.
Try running it again with a number of simluations between 25 and 10.000.
If the error persists then it is likely because there is no value for which the curve jumps in the original data,
meaning the data is inappropriate for this analysis. Try with a different dataset."
)
)
} else {
div(
class = "center-content", # Wrapper to center content
div(
class = "error-message", # Custom error styling
"Run the function to see the content of this tab."
)
)
}
})
output$comparison_tools_conditional <- renderUI({
# Wait until the function has run
if (function_ran()) {
tagList(
HTML("<br>"),
h4("Comparison Tools"),
HTML("<br>"),
plotOutput("plot_compare", width = "80%", height = "1000px")
)
} else if (function_error()) {
div(
class = "center-content", # Wrapper to center content
div(
class = "error-message", # Custom error styling
"An error occured. This is likely due to either a too low or too high number of Monte Carlo simulations.
Try running it again with a number of simluations between 25 and 10.000.
If the error persists then it is likely because there is no value for which the curve jumps in the original data,
meaning the data is inappropriate for this analysis. Try with a different dataset."
)
)
} else {
div(
class = "center-content", # Wrapper to center content
div(
class = "error-message", # Custom error styling
"Run the function to see the content of this tab."
)
)
}
})
observe({
# toggleState("download1", !is.null(data_output$data))
toggleClass("download1", "btn-success", !is.null(data_output$data))
})
observe({
# toggleState("download2", !is.null(data_output$data))
toggleClass("download2", "btn-success", !is.null(data_output$data))
})
# Create the download handler for the folder
output$download1 <- downloadHandler(
filename = function() {
paste("robusta_plots_", Sys.Date(), ".zip", sep = "")
},
content = function(file) {
# Create a temporary directory
temp_dir <- tempdir()
# Initialize the list of plots to generate
plot_list <- list(
comparison_tool = data_output$data[[3]],
original_pcf = data_output$data[[1]]
)
for (key in names(data_output$data[[2]])) {
for (i in seq_along(data_output$data[[2]][[key]])) {
plot_obj <- data_output$data[[2]][[key]][[i]]
if (inherits(plot_obj, "grob")) {
plot_list[[paste0(key, "_robustness_", i)]] <- plot_obj
} else {
warning(paste("Skipping non-plot object at key", key, "index", i))
}
}
}
# File paths for temporary plot files
temp_files <- vector("character", length(plot_list))
for (i in seq_along(plot_list)) {
# Create temporary file paths
temp_file <- file.path(temp_dir, paste0(names(plot_list)[i], ".png"))
temp_files[i] <- temp_file
# Save the ggplot as a PNG file
png(temp_file, width = 1200, height = 600)
grid.draw(plot_list[[i]])
dev.off()
}
# Create the zip file
zip::zipr(file, files = temp_files)
# Cleanup temporary plot files after creating the zip
on.exit(unlink(temp_files), add = TRUE)
},
contentType = "application/zip"
)
# Create the download handler for the folder
output$download2 <- downloadHandler(
filename = function() {
paste("robusta_plots_", Sys.Date(), ".zip", sep = "")
},
content = function(file) {
# Create a temporary directory
temp_dir <- tempdir()
# Initialize the list of plots to generate
plot_list <- list(
comparison_tool = data_output$data[[3]],
original_pcf = data_output$data[[1]]
)
for (key in names(data_output$data[[2]])) {
for (i in seq_along(data_output$data[[2]][[key]])) {
plot_obj <- data_output$data[[2]][[key]][[i]]
if (inherits(plot_obj, "grob")) {
plot_list[[paste0(key, "_robustness_", i)]] <- plot_obj
} else {
warning(paste("Skipping non-plot object at key", key, "index", i))
}
}
}
# File paths for temporary plot files
temp_files <- vector("character", length(plot_list))
for (i in seq_along(plot_list)) {
# Create temporary file paths
temp_file <- file.path(temp_dir, paste0(names(plot_list)[i], ".png"))
temp_files[i] <- temp_file
# Save the ggplot as a PNG file
png(temp_file, width = 1200, height = 600)
grid.draw(plot_list[[i]])
dev.off()
}
# Create the zip file
zip::zipr(file, files = temp_files)
# Cleanup temporary plot files after creating the zip
on.exit(unlink(temp_files), add = TRUE)
},
contentType = "application/zip"
)
}
shinyApp(ui, server)