#' one_objective_focus UI Function #' #' @description A shiny Module. #' #' @param id,input,output,session Internal parameters for {shiny}. #' #' @noRd #' #' @importFrom shiny NS tagList #' @importFrom rvgest objectives mod_one_objective_focus_ui <- function(id){ ns <- NS(id) 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 tagList( 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")) ) } #' one_objective_focus Server Functions #' #' @noRd 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]) 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)) + geom_tile(aes(fill = prob)) + scale_fill_continuous(low = "green", high = "red", name = "Failure probability") + geom_point(data = objCurve, color = "black", size = 1, shape = 21, fill = "grey") + 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) }) }) } ## To be copied in the UI # mod_one_objective_focus_ui("one_objective_focus_ui_1") ## To be copied in the server # mod_one_objective_focus_server("one_objective_focus_ui_1")