mod_one_objective_focus.R 3 KB
Newer Older
1
2
3
4
5
6
7
8
9
#' one_objective_focus UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
10
#' @importFrom rvgest objectives
11
12
mod_one_objective_focus_ui <- function(id){
  ns <- NS(id)
13
14
15
16
17
18
  rulesets <- seq(length(rvgest::rulesets$rules))
  names(rulesets) <- paste0(rulesets, ". ", rvgest::rulesets$rules)
  stations <- unique(rvgest::objectives$station)
  names(stations) <- stations
  levels <- unique(rvgest::objectives$level)
  names(levels) <- levels
19
  tagList(
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
    selectInput(
      ns("ruleset"),
      "Rule set",
      rulesets,
      selected = NULL,
      multiple = FALSE,
      selectize = TRUE,
      width = NULL,
      size = NULL
    ),
    selectInput(
      ns("station"),
      "Station",
      stations,
      selected = NULL,
      multiple = FALSE,
      selectize = TRUE,
      width = NULL,
      size = NULL
    ),
    selectInput(
      ns("level"),
      "Flow threshold",
      levels,
      selected = NULL,
      multiple = FALSE,
      selectize = TRUE,
      width = NULL,
      size = NULL
    ),
    uiOutput(ns("plots"))
51
52
53
  )
}
    
54
#' one_objective_focus Server Functions
55
56
#'
#' @noRd 
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
mod_one_objective_focus_server <- function(id, con){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    observe({
      objective <- rvgest::objectives[rvgest::objectives$station == input$station & rvgest::objectives$level == input$level,]
      indexMonths <- c(1,31,28,31,30,31,30,31,31,30,31,30)
      labels <- unlist(strsplit("JFMAMJJASOND", NULL))
      iBreaks <- sapply(seq.int(12), function(i) sum(indexMonths[1:i]))
      lapply(objective$lakes[[1]]$name, function(lake) {
        dfDiscretProbs <- calcRiskHeatMap(con, input$ruleset, objective$station, objective$level, lake)
        if(!is.null(dfDiscretProbs)) {
          breaks <- sapply(iBreaks, function(x) levels(dfDiscretProbs$day)[x])
          p <- ggplot(dfDiscretProbs, aes(x = day, y = V / 1e6)) + 
            geom_tile(aes(fill = prob)) +
            scale_fill_continuous(low = "green", high = "red", name = "Failure probability") +
            ggtitle(paste("Lake", lake)) +
            scale_x_discrete("Calendar days", breaks = breaks, labels = labels) +
            scale_y_continuous(name="Reservoir storage (hm3)")
          output[[paste("plot", lake, sep = "_")]] <- renderPlot({p},width = 640, height = 280)
        }
      })
      
    })
    
    output$plots <- renderUI({
      objective <- rvgest::objectives[rvgest::objectives$station == input$station & rvgest::objectives$level == input$level,]
      plot_output_list <- lapply(objective$lakes[[1]]$name, function(lake) {
        plotname <- paste("plot", lake, sep = "_")
        plotOutput(ns(plotname), height = '250px', inline=TRUE)
      })
      do.call(tagList, plot_output_list)
     })
  })
90
91
92
93
94
95
}
    
## To be copied in the UI
# mod_one_objective_focus_ui("one_objective_focus_ui_1")
    
## To be copied in the server
96
# mod_one_objective_focus_server("one_objective_focus_ui_1")