mod_instant_risk_overview.R 1.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
#' instant_risk_overview UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
10
#' @importFrom rvgest lakes rulesets
11
12
mod_instant_risk_overview_ui <- function(id){
  ns <- NS(id)
13
14
  choices <- seq(length(rulesets$rules))
  names(choices) <- paste0(choices, ". ", rulesets$rules)
15
  tagList(
16
17
18
19
20
21
22
23
24
25
26
    selectInput(
      "ruleset",
      "Rule set",
      choices,
      selected = NULL,
      multiple = FALSE,
      selectize = TRUE,
      width = NULL,
      size = NULL
    ),
    dateInput("date", "Date:", value = Sys.Date()),
27
    lapply(seq.int(nrow(lakes)), function(i) {
28
      numericInput(paste0("V", i),
29
30
31
32
33
34
35
                   lakes$name[i],
                   round(getObjectiveStorage()[lakes$name[i]]),
                   min = lakes$min[i],
                   max = lakes$max[i])
    }),
    # tableOutput(ns("table"))
    plotOutput(ns("plot"))
36
  )
37
  
38
39
40
41
42
}
    
#' instant_risk_overview Server Function
#'
#' @noRd 
43
44
#' @import ggplot2
mod_instant_risk_overview_server <- function(input, output, session, con){
45
  ns <- session$ns
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  
  df <- reactive({
    df <- calcInstantRisk(con, 1, Sys.Date(), getObjectiveStorage())
    storages <- c(input$v1, input$v2, input$v3, input$v4)
    if(!is.null(unlist(storages))) {
      names(storages) <- lakes$name
      print(storage)
      df <- calcInstantRisk(con, input$ruleset, input$date, storages)
    }
    df <- head(df[order(df$prob, decreasing = TRUE),], 10)
    df
  })
  # output$table <- renderTable(df())
  output$plot <- renderPlot(
    ggplot(df(), aes(x = objective, y = prob)) +
      geom_col(aes(fill = level)) +
      coord_flip())
63
64
65
66
67
68
69
70
}
    
## To be copied in the UI
# mod_instant_risk_overview_ui("instant_risk_overview_ui_1")
    
## To be copied in the server
# callModule(mod_instant_risk_overview_server, "instant_risk_overview_ui_1")