From a43340fc33d79e911fe3befdbd1ca59a18913f56 Mon Sep 17 00:00:00 2001 From: pvictor Date: Thu, 1 Mar 2018 17:41:37 +0100 Subject: [PATCH] added draw_upward_margin --- DESCRIPTION | 14 ++-- NAMESPACE | 10 +++ R/draw-upward-margin.R | 135 ++++++++++++++++++++++++++++++++++++++ inst/www/css_dygraph.css | 2 + man/draw_upward_margin.Rd | 34 ++++++++++ 5 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 R/draw-upward-margin.R create mode 100644 inst/www/css_dygraph.css create mode 100644 man/draw_upward_margin.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 387d31a..07facff 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,20 +1,26 @@ Package: antaresWeeklyMargin Title: Setup an Antares Study for Weekly Margin Simulation -Version: 0.0.0.9330 -Authors@R: person("Victor", "Perrier", email = "victor.perrier@dreamRs.fr", role = c("aut", "cre")) +Version: 0.0.0.9400 +Authors@R: c( + person("Victor", "Perrier", email = "victor.perrier@dreamRs.fr", role = c("aut")), + person("Fabiola", "Aravena-Rojas", email = "fabiola.aravena-rojas@rte-france.com", role = c("aut", "cre")), + person("RTE", role = c("cph", "fnd")) + ) Description: Read hydro and thermal XML files from suppliers, retrieve data from RTE APIs. Depends: R (>= 3.1.0) License: GPL (>= 2) | file LICENSE Encoding: UTF-8 LazyData: true -Imports: xml2, +Imports: + xml2, data.table, stringr, readxl, jsonlite, crul, curl, - base64enc + base64enc, + dygraphs RoxygenNote: 6.0.1 Suggests: testthat diff --git a/NAMESPACE b/NAMESPACE index 0a72ca3..8407536 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(draw_upward_margin) export(get_eco2mix) export(get_hydraulique_fil_de_l_eau_eclusee) export(get_ntc) @@ -29,6 +30,15 @@ importFrom(data.table,rbindlist) importFrom(data.table,setcolorder) importFrom(data.table,setnames) importFrom(data.table,setorderv) +importFrom(dygraphs,"%>%") +importFrom(dygraphs,dyAxis) +importFrom(dygraphs,dyCSS) +importFrom(dygraphs,dyHighlight) +importFrom(dygraphs,dyLegend) +importFrom(dygraphs,dyOptions) +importFrom(dygraphs,dyRangeSelector) +importFrom(dygraphs,dySeries) +importFrom(dygraphs,dygraph) importFrom(jsonlite,fromJSON) importFrom(readxl,excel_sheets) importFrom(readxl,read_excel) diff --git a/R/draw-upward-margin.R b/R/draw-upward-margin.R new file mode 100644 index 0000000..ddaaf49 --- /dev/null +++ b/R/draw-upward-margin.R @@ -0,0 +1,135 @@ + +#' Draw remaining capacity line chart +#' +#' @param upward_margin a \code{data.table}, first column must be the datetime, +#' the others results of Monte-Carlo simulations. +#' @param area Name of the area, used in chart title. +#' @param type Initial or final remaining capacity. +#' @param nb_MC Number of Monte-Carlo years to draw. +#' @param num_week Week number to be displayed in chart title. +#' +#' @return a \code{dygraphs} htmlwidget. +#' @export +#' +#' @importFrom dygraphs dygraph dyRangeSelector dyLegend dyHighlight dyCSS dySeries dyOptions dyAxis %>% +#' @importFrom data.table := +#' +#' @examples +#' \dontrun{ +#' +#' # TODO +#' +#' } +draw_upward_margin <- function (upward_margin, area = "fr", type = c("inter", "seul"), + nb_MC = ncol(upward_margin) - 1, num_week = NULL) { + type <- match.arg(type) + #Calcul des differentes percentiles + centil1 <- give_percentile(upward_margin, nb_MC, 1) + centil10 <- give_percentile(upward_margin, nb_MC, 10) + centil50 <- give_percentile(upward_margin, nb_MC, 50) + + #Calcul des marges deterministes (vision GRT). Cette ligne peut ĂȘtre commentee. + #marge_prev <- upward_margin_prev(area, nb_MC, num_week, "all", date_i) + + #Si besoin d'ajouter la courbe des donnees du realise + #(Ce ne sont pas vraiement les donnees du realise, mais une simulation sur ANTARES avec les donnees du realise) + # marge_rt <- upward_margin_rt(area, nb_MC, num_week, "all", date_i) + + #Configuration du graphique si les marges a graphiquer sont les marges area seul + #Les lignes de code associes a la courbe du realise sont desactivees (commentees) + if (type == "seul"){ + upward_margin <- upward_margin[, `:=`( + PERCENTIL_1 = centil1 + ,PERCENTIL_10 = centil10 + ,MEDIAN = centil50 + #,IRC = marge_prev$MARGE_SEUL_PREV + # ,EFFECTIVE_IRC = marge_rt$MARGE_SEUL_RT + )] + + pal_couleurs <- c(rep("gray", nb_MC) + , "red" + , "blue" + , "green" + , "black" + # , "orange" + ) + + graph_margin <- dygraph(upward_margin, paste0("Initial Remaining Capacity ",toupper(area), " - Week", num_week)) %>% + dyRangeSelector() %>% + dyLegend(show="always") %>% + dyHighlight(highlightCircleSize = 3)%>% + dyLegend(show = "always")%>% + dyCSS(css = system.file('www/css_dygraph.css', package = 'antaresWeeklyMargin'))%>% + dySeries("PERCENTIL_1", strokeWidth = 2) %>% + dySeries("PERCENTIL_10", strokeWidth = 2) %>% + dySeries("MEDIAN", strokeWidth = 2) %>% + #dySeries("IRC", strokeWidth = 2) %>% + #dySeries("EFFECTIVE_IRC", strokeWidth = 2) %>% + dyOptions(colors = c(pal_couleurs)) %>% + dyOptions(useDataTimezone = TRUE) %>% + dyAxis("y", label = "MW") + + #Configuration du graphique si les marges a graphiquer sont les marges area interconnecte + #Les lignes de code associes a la courbe du realise sont desactivees (commentees) + } else if (type == "inter") { + upward_margin <- upward_margin[, `:=`(PERCENTIL_1 = centil1 + ,PERCENTIL_10 = centil10 + ,MEDIAN = centil50 + #,FRC = marge_prev$MARGE_INTER_PREV + # ,EFFECTIVE_FRC = marge_rt$MARGE_INTER_RT + )] + + pal_couleurs <- c(rep("gray", nb_MC) + , "red" + , "blue" + , "green" + , "black" + # , "orange" + ) + + graph_margin <- dygraph(upward_margin, paste0("Final Remaining Capacity ",toupper(area)," - Week", num_week)) %>% + dyRangeSelector() %>% + dyLegend(show="always") %>% + dyHighlight(highlightCircleSize = 3)%>% + dyLegend(show = "always")%>% + dyCSS(css = system.file('www/css_dygraph.css', package = 'antaresWeeklyMargin'))%>% + dySeries("PERCENTIL_1", strokeWidth = 2) %>% + dySeries("PERCENTIL_10", strokeWidth = 2) %>% + dySeries("MEDIAN", strokeWidth = 2) %>% + #dySeries("FRC", strokeWidth = 2) %>% + # dySeries("EFFECTIVE_FRC", strokeWidth = 2) %>% + dyOptions(colors = c(pal_couleurs)) %>% + dyOptions(useDataTimezone = TRUE) %>% + dyAxis("y", label = "MW") + } + + #Pour montrer le graphique sur la fĂȘnetre VIEWER + return(graph_margin) +} + + + + + +###################################################################################################### +#Fonction qui determine un centil donne de marges hebdomadaires, calcules a partir de scenarios MC +#Il faut le preciser quel type de marge on considere (seul ou interconnecte), le nombre des MC_years et le centil +# +#Il retourne une chronique correspondant au centil des marges +###################################################################################################### + +give_percentile <- function(margin, nb_MC, num_centil) { #margin sous forme d'un data.table dont la 1ere colonne est la date et les colonne ssuivantes les marges. + + #Calcul de centil inferieur et superieur + centil_inf <- floor(num_centil*nb_MC/100) + centil_sup <- ceiling(num_centil*nb_MC/100) + + #Determiner la chronique correspondant au centil donne + percent <- sapply(1:168, function(x){ + temp <- sapply(1:nb_MC, function(y){margin[[x,y+1]]}) + inf <- sort(temp, partial = centil_inf)[centil_inf] + sup <- sort(temp, partial = centil_sup)[centil_sup] + inf + (nb_MC/100-floor(nb_MC/100))*(sup-inf)}) + + return(percent) +} diff --git a/inst/www/css_dygraph.css b/inst/www/css_dygraph.css new file mode 100644 index 0000000..6ab035c --- /dev/null +++ b/inst/www/css_dygraph.css @@ -0,0 +1,2 @@ +.dygraph-legend > span { display: none; } +.dygraph-legend > span.highlight { display: inline;} \ No newline at end of file diff --git a/man/draw_upward_margin.Rd b/man/draw_upward_margin.Rd new file mode 100644 index 0000000..d672025 --- /dev/null +++ b/man/draw_upward_margin.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/draw-upward-margin.R +\name{draw_upward_margin} +\alias{draw_upward_margin} +\title{Draw remaining capacity line chart} +\usage{ +draw_upward_margin(upward_margin, area = "fr", type = c("inter", "seul"), + nb_MC = ncol(upward_margin) - 1, num_week = NULL) +} +\arguments{ +\item{upward_margin}{a \code{data.table}, first column must be the datetime, +the others results of Monte-Carlo simulations.} + +\item{area}{Name of the area, used in chart title.} + +\item{type}{Initial or final remaining capacity.} + +\item{nb_MC}{Number of Monte-Carlo years to draw.} + +\item{num_week}{Week number to be displayed in chart title.} +} +\value{ +a \code{dygraphs} htmlwidget. +} +\description{ +Draw remaining capacity line chart +} +\examples{ +\dontrun{ + +# TODO + +} +}