mod_one_objective_focus.R 3.22 KB
Newer Older
1
2
3
4
5
6
#' one_objective_focus UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
7
#' @noRd
8
#'
9
#' @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
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])
69
70
71
          objCurve <- data.frame(day = objectiveStorageCurves$day, V = round(objectiveStorageCurves[[lake]]))
          objCurve <- objCurve[seq.int(1, 365,3),]
          p <- ggplot(dfDiscretProbs, aes(x = day, y = V)) +
72
73
            geom_tile(aes(fill = prob)) +
            scale_fill_continuous(low = "green", high = "red", name = "Failure probability") +
74
            geom_point(data = objCurve, color = "black", size = 1, shape = 21, fill = "grey") +
75
76
77
78
79
80
            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)
        }
      })
81

82
    })
83

84
85
86
87
88
89
90
91
92
    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)
     })
  })
93
}
94

95
96
## To be copied in the UI
# mod_one_objective_focus_ui("one_objective_focus_ui_1")
97

98
## To be copied in the server
99
# mod_one_objective_focus_server("one_objective_focus_ui_1")