From 8f2867b28bcbc2fbd0c9aea248eecbb583d9bd0e Mon Sep 17 00:00:00 2001
From: thierrychambert <thierry.chambert@gmail.com>
Date: Thu, 12 Aug 2021 13:49:39 +0200
Subject: [PATCH] =?UTF-8?q?Cleaning=20shiny=20Esp=C3=A8ce=20g=C3=A9n=C3=A9?=
 =?UTF-8?q?rique=20moved=20at=20end=20of=20list=20This=20version=20work=20?=
 =?UTF-8?q?well.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 inst/ShinyApp/server.R         | 38 +++++++++++++++++-----------------
 inst/ShinyApp/species_list.csv |  2 +-
 inst/ShinyApp/ui.R             | 11 ++--------
 run_analysis.R                 |  6 ++++++
 4 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/inst/ShinyApp/server.R b/inst/ShinyApp/server.R
index 6c07612..b583e13 100644
--- a/inst/ShinyApp/server.R
+++ b/inst/ShinyApp/server.R
@@ -1,7 +1,9 @@
 server <- function(input, output, session){
 
-  # Hide all inputs excepted actionButtons
 
+  ##--------------------------------------------
+  ##  Hide all inputs excepted actionButtons  --
+  ##--------------------------------------------
   observe({
     shinyjs::hide("fatal_constant")
     shinyjs::hide("fatalities_input_type")
@@ -113,10 +115,10 @@ server <- function(input, output, session){
   }) # en observe show/hide
 
 
-
-
-  # Elicitation experts part
-
+  ##--------------------------------------------
+  ##  Functions                               --
+  ##--------------------------------------------
+  # Function to extract value from elicitation matrix and run the elication analysis
   func_eli <- function(mat_expert){
     t_mat_expert <- t(mat_expert)
     vals = t_mat_expert[3:5,]
@@ -127,12 +129,16 @@ server <- function(input, output, session){
     return(list(out = out, mean = out$mean_smooth, SE = sqrt(out$var_smooth)))
   }
 
+  # Function to plot output from elicitation analysis
   func_eli_plot <- function(out){
     plot_elicitation(out)
   }
 
-  ## Output
 
+
+  ##--------------------------------------------
+  ##  Reactive values                         --
+  ##--------------------------------------------
   param <- reactiveValues(N1 = NULL,
                           fatalities_mean = NULL,
                           fecundities = NULL,
@@ -144,7 +150,7 @@ server <- function(input, output, session){
                           onset_time = NULL,
                           onset_year = NULL,
                           carrying_capacity = NULL,
-                          rMAX_species = rMAX_species,
+                          rMAX_species = NULL,
                           theta = theta,
                           fatalities_eli_result = NULL,
                           pop_size_eli_result = NULL,
@@ -156,7 +162,9 @@ server <- function(input, output, session){
                           pop_growth_se = NULL,
                           carrying_cap_eli_result = NULL)
 
-  # Elicitation
+
+
+
 
   ## Fatalities
 
@@ -236,9 +244,8 @@ server <- function(input, output, session){
     }
   })
 
-  # Population size
+  # Observe pop size value
   ## Mean, se and type
-
   observeEvent({input$run},{
     if(input$pop_size_input_type == "Elicitation d'expert"){
       if(!(is.null(param$pop_size_eli_result))){
@@ -255,8 +262,6 @@ server <- function(input, output, session){
   })
 
   # Observe pop growth value
-  ##  Avoid unrealistic scenarios
-
   observeEvent({input$run}, {
     if(input$lambda_input_type == "Elicitation d'expert"){
       if(!(is.null(param$pop_growth_eli_result))){
@@ -298,19 +303,14 @@ server <- function(input, output, session){
   observeEvent({input$run}, {
       param$survivals <- input$mat_fill_vr[,1]
       param$fecundities <- input$mat_fill_vr[,2]
+      param$rMAX_species <- rMAX_spp(surv = tail(param$survivals,1), afr = min(which(param$fecundities != 0)))
   }) # end observeEvent
 
   # Survival and fecundity calibration
 
   observeEvent({
     input$run
-    # input$species_choice
-    # input$pop_growth_mean
   },{
-
-    ##  Avoid unrealistic scenarios
-    #param$pop_growth_mean <- min(1 + param$rMAX_species, input$pop_growth_mean)
-
     param$vr_calibrated <- calibrate_params(
       inits = init_calib(s = param$survivals, f = param$fecundities, lam0 = param$pop_growth_mean),
       f = param$fecundities, s = param$survivals, lam0 = param$pop_growth_mean
@@ -349,7 +349,7 @@ server <- function(input, output, session){
     withProgress(message = 'Simulation progress', value = 0, {
 
       param$N1 <- run_simul_shiny(nsim = input$nsim,
-                                  cumuated_impacts = param$cumulated_impacts,
+                                  cumulated_impacts = param$cumulated_impacts,
 
                                   fatalities_mean = param$fatalities_mean,
                                   fatalities_se = param$fatalities_se,
diff --git a/inst/ShinyApp/species_list.csv b/inst/ShinyApp/species_list.csv
index 62ef529..e24e302 100644
--- a/inst/ShinyApp/species_list.csv
+++ b/inst/ShinyApp/species_list.csv
@@ -1,5 +1,4 @@
 NomEspece,OrdreFamille
-Espèce générique,NA
 Aigle de Bonelli,Accipitriformes
 Aigle royal,Accipitriformes
 Autour des palombes,Accipitriformes
@@ -56,3 +55,4 @@ Cigogne blanche,Pelecaniformes
 Cigogne noire,Pelecaniformes
 Grand Duc d'Europe,Strigiformes
 Hibou des marais,Strigiformes
+Espèce générique,NA
diff --git a/inst/ShinyApp/ui.R b/inst/ShinyApp/ui.R
index f0bfac0..69395b7 100644
--- a/inst/ShinyApp/ui.R
+++ b/inst/ShinyApp/ui.R
@@ -20,8 +20,6 @@ data_sf <- read.csv("./inst/ShinyApp/survivals_fecundities_species.csv", sep = "
 nsim = 10
 coeff_var_environ = 0.10
 time_horzion = 30
-init_survivals <- c(0.5, 0.5, 0.5)
-init_fecundities <- c(0, 0.8, 1.2)
 
 #####################
 ### Pre-fill data ###
@@ -61,16 +59,11 @@ init_cumul <- c(10, 5, 8,
 init_cumul_add <- c(3, 0.05, 2020)
 
 
-
-# vital rates
-init_vr = c(init_survivals, init_fecundities)
-
 # DD parameters
 theta = 1
 
 # Define theoretical rMAX for the species
-rMAX_species <- rMAX_spp(surv = tail(init_survivals,1), afr = min(which(init_fecundities != 0)))
-rMAX_species
+#rMAX_species <- rMAX_spp(surv = tail(init_survivals,1), afr = min(which(init_fecundities != 0)))
 
 
 ##--------------------------------------------
@@ -327,7 +320,7 @@ ui <- fluidPage(
                    label = "Paramètres démographiques"),
 
       matrixInput(inputId = "mat_fill_vr",
-                  value = matrix(data = init_vr, 3, 2,
+                  value = matrix(data = NA, 3, 2,
                                  dimnames = list(c("Juv 1", "Juv 2", "Adulte"), c("Survie", "Fécondité"))),
                   class = "numeric",
                   rows = list(names = TRUE),
diff --git a/run_analysis.R b/run_analysis.R
index e24bb75..9a581d0 100644
--- a/run_analysis.R
+++ b/run_analysis.R
@@ -64,6 +64,12 @@ lambda( build_Leslie(s = s_calibrated, f = f_calibrated) )
 
 
 
+pop_size_mean = 200
+pop_growth_mean = 1.1
+fatal_constant = "M"
+pop_size_type = "Ntotal"
+carrying_capacity = 500
+
 
 ##==============================================================================
 ##                         Analyses (simulations)                             ==
-- 
GitLab