Skip to content
Snippets Groups Projects
server.R 39.4 KiB
Newer Older
Marie-Bocage's avatar
Marie-Bocage committed
server <- function(input, output, session){
Marie-Bocage's avatar
Marie-Bocage committed

  ##############################################
thierrychambert's avatar
thierrychambert committed
  ##--------------------------------------------

  ## Fatalities
  output$hide_fatalities <- eventReactive({
    input$button_fatalities
  },{
    if(input$button_fatalities%%2 == 1) TRUE else FALSE
  }, ignoreInit = TRUE)

  outputOptions(output, "hide_fatalities", suspendWhenHidden = FALSE)


  ## Population Size
thierrychambert's avatar
thierrychambert committed
  output$hide_pop_size <- eventReactive({
    input$button_pop_size
  },{
    if(input$button_pop_size%%2 == 1) TRUE else FALSE
  }, ignoreInit = TRUE)

  outputOptions(output, "hide_pop_size", suspendWhenHidden = FALSE)


  ## Population Growth
  output$hide_pop_growth <- eventReactive({
    input$button_pop_growth
  },{
    if(input$button_pop_growth%%2 == 1) TRUE else FALSE
  }, ignoreInit = TRUE)

  outputOptions(output, "hide_pop_growth", suspendWhenHidden = FALSE)


  ## Carrying capacity
  output$hide_carrying_cap <- eventReactive({
    input$button_carrying_cap
  },{
    if(input$button_carrying_cap%%2 == 1) TRUE else FALSE
  }, ignoreInit = TRUE)

  outputOptions(output, "hide_carrying_cap", suspendWhenHidden = FALSE)

  # Display Carrying capacity Unit Info
  output$carrying_cap_unit_info <- renderText({
    if(input$pop_size_unit == "Npair"){
      paste0("Nombre de couple")
    } else {
      paste0("Effectif total")
    }
  })


  ##############################################
thierrychambert's avatar
thierrychambert committed
  ##--------------------------------------------
thierrychambert's avatar
thierrychambert committed
  observe({
    shinyjs::hide("fatalities_mean")
    shinyjs::hide("fatalities_se")
    shinyjs::hide("fatalities_lower")
    shinyjs::hide("fatalities_upper")
Marie-Bocage's avatar
Marie-Bocage committed
    shinyjs::hide("fatalities_mat_expert")
Marie-Bocage's avatar
Marie-Bocage committed
    shinyjs::hide("fatalities_run_expert")
Marie-Bocage's avatar
Marie-Bocage committed
    shinyjs::hide("farm_number_cumulated")
    shinyjs::hide("fatalities_mat_cumulated")
    shinyjs::hide("pop_size_lower")
    shinyjs::hide("pop_size_upper")
Marie-Bocage's avatar
Marie-Bocage committed
    shinyjs::hide("pop_size_mean")
    shinyjs::hide("pop_size_se")
    shinyjs::hide("pop_size_mat_expert")
Marie-Bocage's avatar
Marie-Bocage committed
    shinyjs::hide("pop_size_run_expert")
    shinyjs::hide("pop_growth_lower")
    shinyjs::hide("pop_growth_upper")
Marie-Bocage's avatar
Marie-Bocage committed
    shinyjs::hide("pop_growth_mean")
    shinyjs::hide("pop_growth_se")
    shinyjs::hide("pop_growth_mat_expert")
Marie-Bocage's avatar
Marie-Bocage committed
    shinyjs::hide("pop_growth_run_expert")
Marie-Bocage's avatar
Marie-Bocage committed
    shinyjs::hide("pop_trend")
    shinyjs::hide("pop_trend_strength")
    shinyjs::hide("carrying_capacity")
    shinyjs::hide("carrying_cap_mat_expert")
    shinyjs::hide("carrying_cap_run_expert")

Marie-Bocage's avatar
Marie-Bocage committed
    shinyjs::hide("mat_fill_vr")

    #------------
    # Show some
    #------------
    # Show inputs for fatalities part
Marie-Bocage's avatar
Marie-Bocage committed
    if(input$button_fatalities%%2 == 1){
thierrychambert's avatar
thierrychambert committed
      #shinyjs::show("fatal_constant")
Marie-Bocage's avatar
Marie-Bocage committed

      # Show inputs for none cumulated impacts scenario

      if(input$analysis_choice == "scenario"){
        shinyjs::show("fatalities_input_type")
        if(input$fatalities_input_type == "itvl"){
          shinyjs::show("fatalities_lower")
          shinyjs::show("fatalities_upper")
Marie-Bocage's avatar
Marie-Bocage committed
        }
        if(input$fatalities_input_type == "val"){
          shinyjs::show("fatalities_mean")
          shinyjs::show("fatalities_se")
        }
thierrychambert's avatar
thierrychambert committed
        if(input$fatalities_input_type == "eli_exp"){
Marie-Bocage's avatar
Marie-Bocage committed
          shinyjs::show("fatalities_mat_expert")
Marie-Bocage's avatar
Marie-Bocage committed
          shinyjs::show("fatalities_run_expert")
Marie-Bocage's avatar
Marie-Bocage committed
        }
      }

      # Show inputs for cumulated scenario

      if(input$analysis_choice == "cumulated"){
        shinyjs::hide("fatalities_input_type")
Marie-Bocage's avatar
Marie-Bocage committed
        shinyjs::show("farm_number_cumulated")
        shinyjs::show("fatalities_mat_cumulated")
thierrychambert's avatar
thierrychambert committed
      }
Marie-Bocage's avatar
Marie-Bocage committed

Marie-Bocage's avatar
Marie-Bocage committed
    # Show inputs for population size part
    if(input$button_pop_size%%2 == 1){
      shinyjs::show("pop_size_input_type")
      if(input$pop_size_input_type == "itvl"){
        shinyjs::show("pop_size_lower")
        shinyjs::show("pop_size_upper")
      }
thierrychambert's avatar
thierrychambert committed
      if(input$pop_size_input_type == "val"){
Marie-Bocage's avatar
Marie-Bocage committed
        shinyjs::show("pop_size_mean")
        shinyjs::show("pop_size_se")
      }
thierrychambert's avatar
thierrychambert committed
      if(input$pop_size_input_type == "eli_exp"){
Marie-Bocage's avatar
Marie-Bocage committed
        shinyjs::show("pop_size_mat_expert")
Marie-Bocage's avatar
Marie-Bocage committed
        shinyjs::show("pop_size_run_expert")
    # Show inputs for population trend/growth part
    if(input$button_pop_growth%%2 == 1){
      shinyjs::show("pop_growth_input_type")

      if(input$pop_growth_input_type == "itvl"){
        shinyjs::show("pop_growth_lower")
        shinyjs::show("pop_growth_upper")
      }
thierrychambert's avatar
thierrychambert committed
      if(input$pop_growth_input_type == "val"){
Marie-Bocage's avatar
Marie-Bocage committed
        shinyjs::show("pop_growth_mean")
        shinyjs::show("pop_growth_se")
      }
thierrychambert's avatar
thierrychambert committed
      if(input$pop_growth_input_type == "eli_exp"){
Marie-Bocage's avatar
Marie-Bocage committed
        shinyjs::show("pop_growth_mat_expert")
Marie-Bocage's avatar
Marie-Bocage committed
        shinyjs::show("pop_growth_run_expert")
Marie-Bocage's avatar
Marie-Bocage committed
      }
thierrychambert's avatar
thierrychambert committed
      if(input$pop_growth_input_type == "trend"){
Marie-Bocage's avatar
Marie-Bocage committed
        shinyjs::show("pop_trend")
        if(input$pop_trend != "stable"){
          shinyjs::show("pop_trend_strength")
        }
Marie-Bocage's avatar
Marie-Bocage committed
      }
    # Show inputs for carrying capacity part
    if(input$button_carrying_cap%%2 == 1){
      shinyjs::show("carrying_cap_input_type")
      if(input$carrying_cap_input_type == "val"){
        shinyjs::show("carrying_capacity")
      }
      if(input$carrying_cap_input_type == "eli_exp"){
        shinyjs::show("carrying_cap_mat_expert")
        shinyjs::show("carrying_cap_run_expert")
      }
    }
Marie-Bocage's avatar
Marie-Bocage committed

Marie-Bocage's avatar
Marie-Bocage committed
    if(input$button_vital_rates%%2 == 1){
thierrychambert's avatar
thierrychambert committed
      shinyjs::show("mat_fill_vr")
Marie-Bocage's avatar
Marie-Bocage committed
    }

  }) # en observe show/hide
  ###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~###
  ##############################################
thierrychambert's avatar
thierrychambert committed
  ##--------------------------------------------
  ready <- reactiveValues(fatalities = TRUE, pop_size = TRUE, pop_growth = TRUE, carrying_capacity = TRUE)
Marie-Bocage's avatar
Marie-Bocage committed
  param <- reactiveValues(N1 = NULL,
                          cumulated_impacts = FALSE,
Marie-Bocage's avatar
Marie-Bocage committed
                          fatalities_mean = NULL,
                          fatalities_mean_use = NULL,
                          fatalities_se = NULL,
                          onset_time = NULL,
                          onset_year = NULL,
                          out_fatal = NULL,

                          pop_size_mean = NULL,
                          pop_size_se = NULL,
thierrychambert's avatar
thierrychambert committed
                          pop_size_unit = NULL,

                          pop_growth_mean = NULL,
                          pop_growth_se = NULL,

Marie-Bocage's avatar
Marie-Bocage committed
                          fecundities = NULL,
                          survivals = NULL,
                          s_calibrated = NULL,
                          f_calibrated = NULL,
                          vr_calibrated = NULL,
Marie-Bocage's avatar
Marie-Bocage committed
                          carrying_capacity = NULL,
thierrychambert's avatar
thierrychambert committed
                          rMAX_species = NULL,

                          model_demo = NULL,
                          time_horzion = NULL,
                          coeff_var_environ = NULL,
                          fatal_constant = NULL,

Marie-Bocage's avatar
Marie-Bocage committed
                          fatalities_eli_result = NULL,
                          pop_size_eli_result = NULL,
                          pop_growth_eli_result = NULL,
                          carrying_cap_eli_result = NULL
  ###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~###


  ##############################################
  ## Update matrix cumulated impact
  ##-------------------------------------------
  observeEvent({
    input$farm_number_cumulated
  }, {
thierrychambert's avatar
thierrychambert committed

    nfarm <- input$farm_number_cumulated
    init_cumul_new  <- init_cumul[1:nfarm,]
    updateMatrixInput(session, inputId = "fatalities_mat_cumulated",
                      value =  matrix(init_cumul_new, nrow = nfarm, ncol = 3, byrow = FALSE,
                                      dimnames = list(paste("Parc", c(1:nfarm)),
                                                      c("Moyenne",
                                                        "Erreur-type",
Marie-Bocage's avatar
Marie-Bocage committed

  ##--------------------------------------------
  ##  Run expert elicitation
  ##--------------------------------------------
  # Function to run the elication analysis
  func_eli <- function(mat_expert){
    t_mat_expert <- t(mat_expert)
    vals <- t_mat_expert[2:4,]
    Cp <- t_mat_expert[5,]
    weights <- t_mat_expert[1,]

    out <- elicitation(vals, Cp, weights)
    return(list(out = out, mean = out$mean_smooth, SE = sqrt(out$var_smooth)))
  }

  # Function to plot the elication analysis output
  plot_expert <- function(out, show_se = TRUE, ...){
    plot_elicitation(out, ylab = "", xlab = "Valeur du paramtre", cex.lab = 1.2, yaxt = "n")
    mtext(text = "Densit de probabilit", side = 2, line = 2, cex = 1.2)

    y2 <- dgamma(x = out$mean_smooth, shape = out$shape_smooth, rate = out$rate_smooth)
    xx <- qgamma(p = c(0.01,0.99), shape = out$shape_smooth, rate = out$rate_smooth)
    clip(xx[1], xx[2], -100, y2)
    abline(v = out$mean_smooth, lwd = 3, col = "darkblue")

    mtext(text = paste("Moyenne = ", round(out$mean_smooth,2)), side = 3, line = 2.5, cex = 1.2, adj = 0)
    if(show_se) mtext(text = paste("Erreur-type = ", round(sqrt(out$var_smooth), 2)), side = 3, line = 1, cex = 1.2, adj = 0)
  }

  ########################
  ## Fatalities
  ##----------------------
  observeEvent({
    input$fatalities_run_expert
  }, {
    if( all(!is.na(input$fatalities_mat_expert)) ) {
Marie-Bocage's avatar
Marie-Bocage committed

      ## run elicitation analysis
Marie-Bocage's avatar
Marie-Bocage committed
      param$fatalities_eli_result <- func_eli(input$fatalities_mat_expert)

      ## plot distribution
      output$title_distri_plot <- renderText({ "Mortalits annuelles" })
      output$distri_plot <- renderPlot({ plot_expert(param$fatalities_eli_result$out) })

    } else {
      print("missing value")
    } # end if
  }) # end observeEvent
Marie-Bocage's avatar
Marie-Bocage committed

  ########################
  ## Population size
  ##----------------------
  observeEvent({
    input$pop_size_run_expert
  }, {
    if(all(!is.na(input$pop_size_mat_expert))) {

      ## run elicitation analysis
Marie-Bocage's avatar
Marie-Bocage committed
      param$pop_size_eli_result <- func_eli(input$pop_size_mat_expert)

      ## plot distribution
      output$title_distri_plot <- renderText({ "Taille de population" })
      output$distri_plot <- renderPlot({ plot_expert(param$pop_size_eli_result$out) })

    } else {
      print("missing value")
    } # end if
  }) # end observeEvent
Marie-Bocage's avatar
Marie-Bocage committed

  ########################
  ## Population growth
  ##----------------------
  observeEvent({
    input$pop_growth_run_expert
  },{
    if(all(!is.na(input$pop_growth_mat_expert))) {

      ## run elicitation analysis
Marie-Bocage's avatar
Marie-Bocage committed
      param$pop_growth_eli_result <- func_eli(input$pop_growth_mat_expert)

      ## plot distribution
      output$title_distri_plot <- renderText({ "Taux de croissance de la population" })
      output$distri_plot <- renderPlot({ plot_expert(param$pop_growth_eli_result$out) })
Marie-Bocage's avatar
Marie-Bocage committed

    } else {
      print("missing value")
    } # end if
  }) # end observeEvent

  ########################
  ## Carrying capacity
  ##----------------------
  observeEvent({
    input$carrying_cap_run_expert
  },{
    if(all(!is.na(input$carrying_cap_mat_expert))) {
Marie-Bocage's avatar
Marie-Bocage committed

Marie-Bocage's avatar
Marie-Bocage committed
      param$carrying_cap_eli_result <- func_eli(input$carrying_cap_mat_expert)

      ## run elicitation analysis
      output$title_distri_plot <- renderText({ "Capacit de charge" })
      output$distri_plot <- renderPlot({ plot_expert(param$carrying_cap_eli_result$out, show_se = FALSE) })
thierrychambert's avatar
thierrychambert committed

    } else {
      print("missing value")
    } # end if
  }) # end observeEvent
thierrychambert's avatar
thierrychambert committed

  ###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~###

  #####
  ##--------------------------------------------
  ##  Display parameter distribution
  ##--------------------------------------------
thierrychambert's avatar
thierrychambert committed

  # Function to plot a gamma distribution
  plot_gamma <- function(mu, se, show_mode = TRUE, show_mean = TRUE, show_se = TRUE, ...){
thierrychambert's avatar
thierrychambert committed

    ## Define shape and scale parameter of gamma distribution
    shape = (mu/se)^2
    scale = se^2/mu
thierrychambert's avatar
thierrychambert committed

    ## Plot the curve
    par(mar = c(5, 4, 6, 2))
    curve(dgamma(x, shape=shape, scale=scale), from = max(0,mu-3*se), to = mu+4*se, lwd = 3, col = "darkblue", yaxt = "n",
          ylab = "", xlab = "Valeur du paramtre", cex.lab = 1.2)
    mtext(text = "Densit de probabilit", side = 2, line = 2, cex = 1.2)
    # show mode
    MU <- (shape-1)*scale
    y_MU <- dgamma(x = MU, shape = shape, scale = scale)
    xx <- qgamma(p = c(0.01,0.99), shape = shape, scale = scale)
    clip(xx[1], xx[2], -100, y_MU)
    abline(v = MU, lwd = 3, col = "darkblue")

    # show mean
    y_mu <- dgamma(x = mu, shape = shape, scale = scale)
    clip(xx[1], xx[2], -100, y_mu)
    abline(v = mu, lwd = 2, col = "darkblue", lty = 2)
Loading
Loading full blame...