mod_instant_risk_overview.R 3.50 KiB
#' instant_risk_overview UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
#' @importFrom shinyjs useShinyjs disabled
mod_instant_risk_overview_ui <- function(id){
  ns <- NS(id)
  choices <- seq(length(rvgest::rulesets$rules))
  names(choices) <- paste0(choices, ". ", rvgest::rulesets$rules)
  tagList(
    useShinyjs(),
    fluidRow(
      column(width = 6,
        selectInput(
          ns("ruleset"),
          "Rule set",
          choices,
          selected = NULL,
          multiple = FALSE,
          selectize = TRUE,
          width = NULL,
          size = NULL
        )),
      column(width = 6,
        dateInput(ns("date"), "Date:", value = Sys.Date()))),
    fluidRow(
      column(width = 8,
             radioButtons(ns("radio_mode_select"),
                          "Reservoir storage mode",
                          c("manual" = "manual",
                            "objective curve" = "objective",
                            "real time value" = "RT"),
                          selected = "RT",
                          inline=T),
             offset = 2)
    fluidRow(
      lapply(seq.int(nrow(rvgest::lakes)), function(i) {
        column(width = 3,
          disabled(
            sliderInput(ns(paste0("V", i)),
                        label = paste(rvgest::lakes$name[i], "lake (hm3)"),
                        value = round(getObjectiveStorage()[rvgest::lakes$name[i]]),
                        min = rvgest::lakes$min[i],
                        max = rvgest::lakes$max[i]))
    # tableOutput(ns("table"))
    plotOutput(ns("plot"))
#' instant_risk_overview Server Function
#' @noRd
#' @import ggplot2
mod_instant_risk_overview_server <- function(id, con, SGL_RT_storage) {
  moduleServer(id, function(input, output, session){
    ns <- session$ns
    observeEvent({
        input$radio_mode_select
        input$date
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
}, { if(input$radio_mode_select == "manual") { fAction <- shinyjs::enable } else { fAction <- shinyjs::disable if(input$radio_mode_select == "objective") { V <- round(getObjectiveStorage(input$date)) } else { # Real time storage iDate <- as.numeric(as.POSIXct(input$date)) timeDeviation <- abs(SGL_RT_storage[,1] - iDate) i <- which(timeDeviation == min(timeDeviation)) V <- SGL_RT_storage[i, -1] } attr(V, "names") <- NULL # Otherwise we pass an object to javascript... lapply(seq.int(4), function(i) { updateSliderInput(session, inputId = paste0("V", i), value = V[i]) }) } lapply(paste0("V", seq.int(4)), fAction) }) df <- reactive({ storages <- c(input$V1, input$V2, input$V3, input$V4) names(storages) <- lakes$name golem::print_dev(storages) calcInstantRisk(con, input$ruleset, input$date, storages) }) # output$table <- renderTable(df()) output$plot <- renderPlot( ggplot(df(), aes(x = reorder(objective, prob), y = prob)) + geom_col(aes(fill = level)) + scale_y_continuous(name = "Failure risk", labels = scales::percent_format(accuracy = 1)) + scale_x_discrete(name = "Objectives downstream the reservoirs") + coord_flip()) }) } ## To be copied in the UI # mod_instant_risk_overview_ui("instant_risk_overview_ui_1") ## To be copied in the server # mod_instant_risk_overview_server("instant_risk_overview_ui_1")