From 74cc374ffae87340b5e043bd6a08e2da9acc516c Mon Sep 17 00:00:00 2001
From: Dorchies David <david.dorchies@inrae.fr>
Date: Fri, 21 May 2021 17:10:46 +0200
Subject: [PATCH] refactor: rename DiagramGRiwrm into plot.GRiwrm

Fix #12
---
 .Rbuildignore                                 |  1 +
 NAMESPACE                                     |  2 +-
 R/{DiagramGRiwrm.R => plot.GRiwrm.R}          | 19 ++++----
 man/DiagramGRiwrm.Rd                          | 34 --------------
 man/plot.GRiwrm.Rd                            | 47 +++++++++++++++++++
 vignettes/V01_Structure_SD_model.Rmd          |  2 +-
 vignettes/V03_Open-loop_influenced_flow.Rmd   |  2 +-
 .../V04_Closed-loop_regulated_withdrawal.Rmd  |  2 +-
 vignettes/seinebasin/V01_First_network.Rmd    |  6 +--
 .../V04_Open-loop_influenced_flow.Rmd         |  2 +-
 ..._Open-loop_influenced_flow_calibration.Rmd |  2 +-
 ...-loop_influenced_flow_calibration_GR6J.Rmd |  2 +-
 .../V06_Naturalised_flow_simulation.Rmd       |  4 +-
 13 files changed, 71 insertions(+), 54 deletions(-)
 rename R/{DiagramGRiwrm.R => plot.GRiwrm.R} (50%)
 delete mode 100644 man/DiagramGRiwrm.Rd
 create mode 100644 man/plot.GRiwrm.Rd

diff --git a/.Rbuildignore b/.Rbuildignore
index b70b365..911f877 100644
--- a/.Rbuildignore
+++ b/.Rbuildignore
@@ -10,3 +10,4 @@
 ^_pkgdown\.yml$
 ^pkgdown
 ^docs
+^vignettes/seinebasin$
diff --git a/NAMESPACE b/NAMESPACE
index a8fa8b6..ef702fe 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -18,6 +18,7 @@ S3method(RunModel,GRiwrmInputsModel)
 S3method(RunModel,InputsModel)
 S3method(RunModel,SD)
 S3method(RunModel,Supervisor)
+S3method(plot,GRiwrm)
 S3method(plot,GRiwrmOutputsModel)
 S3method(plot,Qm3s)
 export(Calibration)
@@ -29,7 +30,6 @@ export(CreateInputsCrit)
 export(CreateInputsModel)
 export(CreateRunOptions)
 export(CreateSupervisor)
-export(DiagramGRiwrm)
 export(GRiwrm)
 export(RunModel)
 export(createControl)
diff --git a/R/DiagramGRiwrm.R b/R/plot.GRiwrm.R
similarity index 50%
rename from R/DiagramGRiwrm.R
rename to R/plot.GRiwrm.R
index 02097d2..6d4649b 100644
--- a/R/DiagramGRiwrm.R
+++ b/R/plot.GRiwrm.R
@@ -1,8 +1,11 @@
 #' Display a diagram representing the network structure of a GRiwrm object
 #'
-#' @param griwrm the GRiwrm object to display.
-#' @param display if `TRUE` displays the diagram with `DiagrammeR::mermaid`, return the mermaid code otherwise.
-#' @param orientation Orientation of the graph. "LR" by default.
+#' @param x a GRiwrm object to display (See [GRiwrm])
+#' @param display if `TRUE` displays the diagram with [DiagrammeR::mermaid], return the mermaid code otherwise
+#' @param orientation a [character] describing the orientation of the graph. Possible values are "LR" (left-right), "RL" (right-left), "TB" (top-bottom), or "BT" (bottom-top). "LR" by default
+#' @param width The width of the resulting graphic in pixels (See [DiagrammeR::mermaid])
+#' @param height The height of the resulting graphic in pixels (See [DiagrammeR::mermaid])
+#' @param ... Other arguments and parameters you would like to send to JavaScript (See [DiagrammeR::mermaid])
 #'
 #' @details This function only works inside RStudio because the HTMLwidget produced by DiagrammeR
 #' is not handled on some platforms
@@ -14,19 +17,19 @@
 #' @examples
 #' \dontrun{
 #' # Display diagram
-#' DiagramGRiwrm(griwrm)
+#' plot.GRiwrm(griwrm)
 #' # Is the same as
-#' DiagrammeR::mermaid(DiagramGRiwrm(griwrm, display = FALSE), width = "100%", height = "100%")
+#' DiagrammeR::mermaid(plot.GRiwrm(griwrm, display = FALSE), width = "100%", height = "100%")
 #' }
 #'
-DiagramGRiwrm <- function(griwrm, display = TRUE, orientation = "LR") {
+plot.GRiwrm <- function(x, display = TRUE, orientation = "LR", width = "100%", height = "100%", ...) {
   if(Sys.getenv("RSTUDIO") != "1") {
     return()
   }
   if(!"DiagrammeR" %in% rownames(utils::installed.packages())) {
     stop("The 'DiagrammeR' package should be installed. Type: install.packages('DiagrammeR')")
   }
-  g2 <- griwrm[!is.na(griwrm$down),]
+  g2 <- x[!is.na(x$down),]
   nodes <- paste(
     g2$id,
     "-->|",
@@ -38,7 +41,7 @@ DiagramGRiwrm <- function(griwrm, display = TRUE, orientation = "LR") {
   styleDF <- paste("style", unique(g2$id[is.na(g2$model)]), "fill:#fcc")
   diagram <- paste(c(paste("graph", orientation), nodes, styleSD, styleDF), collapse = "\n")
   if(display) {
-    DiagrammeR::mermaid(diagram, width = "100%", height = "100%")
+    DiagrammeR::mermaid(diagram = diagram, width, height, ...)
   } else {
     return(diagram)
   }
diff --git a/man/DiagramGRiwrm.Rd b/man/DiagramGRiwrm.Rd
deleted file mode 100644
index e3578de..0000000
--- a/man/DiagramGRiwrm.Rd
+++ /dev/null
@@ -1,34 +0,0 @@
-% Generated by roxygen2: do not edit by hand
-% Please edit documentation in R/DiagramGRiwrm.R
-\name{DiagramGRiwrm}
-\alias{DiagramGRiwrm}
-\title{Display a diagram representing the network structure of a GRiwrm object}
-\usage{
-DiagramGRiwrm(griwrm, display = TRUE, orientation = "LR")
-}
-\arguments{
-\item{griwrm}{the GRiwrm object to display.}
-
-\item{display}{if \code{TRUE} displays the diagram with \code{DiagrammeR::mermaid}, return the mermaid code otherwise.}
-
-\item{orientation}{Orientation of the graph. "LR" by default.}
-}
-\value{
-Mermaid code of the diagram id display is \code{FALSE}.
-}
-\description{
-Display a diagram representing the network structure of a GRiwrm object
-}
-\details{
-This function only works inside RStudio because the HTMLwidget produced by DiagrammeR
-is not handled on some platforms
-}
-\examples{
-\dontrun{
-# Display diagram
-DiagramGRiwrm(griwrm)
-# Is the same as
-DiagrammeR::mermaid(DiagramGRiwrm(griwrm, display = FALSE), width = "100\%", height = "100\%")
-}
-
-}
diff --git a/man/plot.GRiwrm.Rd b/man/plot.GRiwrm.Rd
new file mode 100644
index 0000000..071c8a8
--- /dev/null
+++ b/man/plot.GRiwrm.Rd
@@ -0,0 +1,47 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/plot.GRiwrm.R
+\name{plot.GRiwrm}
+\alias{plot.GRiwrm}
+\title{Display a diagram representing the network structure of a GRiwrm object}
+\usage{
+\method{plot}{GRiwrm}(
+  x,
+  display = TRUE,
+  orientation = "LR",
+  width = "100\%",
+  height = "100\%",
+  ...
+)
+}
+\arguments{
+\item{x}{a GRiwrm object to display (See \link{GRiwrm})}
+
+\item{display}{if \code{TRUE} displays the diagram with \link[DiagrammeR:mermaid]{DiagrammeR::mermaid}, return the mermaid code otherwise}
+
+\item{orientation}{a \link{character} describing the orientation of the graph. Possible values are "LR" (left-right), "RL" (right-left), "TB" (top-bottom), or "BT" (bottom-top). "LR" by default}
+
+\item{width}{The width of the resulting graphic in pixels (See \link[DiagrammeR:mermaid]{DiagrammeR::mermaid})}
+
+\item{height}{The height of the resulting graphic in pixels (See \link[DiagrammeR:mermaid]{DiagrammeR::mermaid})}
+
+\item{...}{Other arguments and parameters you would like to send to JavaScript (See \link[DiagrammeR:mermaid]{DiagrammeR::mermaid})}
+}
+\value{
+Mermaid code of the diagram id display is \code{FALSE}.
+}
+\description{
+Display a diagram representing the network structure of a GRiwrm object
+}
+\details{
+This function only works inside RStudio because the HTMLwidget produced by DiagrammeR
+is not handled on some platforms
+}
+\examples{
+\dontrun{
+# Display diagram
+plot.GRiwrm(griwrm)
+# Is the same as
+DiagrammeR::mermaid(plot.GRiwrm(griwrm, display = FALSE), width = "100\%", height = "100\%")
+}
+
+}
diff --git a/vignettes/V01_Structure_SD_model.Rmd b/vignettes/V01_Structure_SD_model.Rmd
index c3f270d..580ad95 100644
--- a/vignettes/V01_Structure_SD_model.Rmd
+++ b/vignettes/V01_Structure_SD_model.Rmd
@@ -66,7 +66,7 @@ griwrm
 The diagram of the network structure is represented below with in blue the upstream nodes with a GR4J model and in green the intermediate nodes with an SD (GR4J + LAG) model.
 
 ```{r diagram}
-DiagramGRiwrm(griwrm)
+plot(griwrm)
 ```
 
 
diff --git a/vignettes/V03_Open-loop_influenced_flow.Rmd b/vignettes/V03_Open-loop_influenced_flow.Rmd
index f5562ca..7b27435 100644
--- a/vignettes/V03_Open-loop_influenced_flow.Rmd
+++ b/vignettes/V03_Open-loop_influenced_flow.Rmd
@@ -58,7 +58,7 @@ The diagram of the network structure is represented below with:
   * in red the node with direct flow injection (no hydrological model)
 
 ```{r diagram}
-DiagramGRiwrm(griwrmV03)
+plot(griwrmV03)
 ```
 
 ### Generate the GRiwrmInputsModel object
diff --git a/vignettes/V04_Closed-loop_regulated_withdrawal.Rmd b/vignettes/V04_Closed-loop_regulated_withdrawal.Rmd
index ef0f430..8212f9b 100644
--- a/vignettes/V04_Closed-loop_regulated_withdrawal.Rmd
+++ b/vignettes/V04_Closed-loop_regulated_withdrawal.Rmd
@@ -60,7 +60,7 @@ griwrmV04 <- rbind(
     area = NA
   )
 )
-DiagramGRiwrm(griwrmV04)
+plot(griwrmV04)
 ```
 Nodes in red color are direct injection points (positive or negative flow) in the model, green nodes simulate either sub-basin rainfall-runoff and routing of upstream node flows and grey nodes simulate sub-basin rainfall-runoff flows only.
 
diff --git a/vignettes/seinebasin/V01_First_network.Rmd b/vignettes/seinebasin/V01_First_network.Rmd
index 3b1427e..872a885 100644
--- a/vignettes/seinebasin/V01_First_network.Rmd
+++ b/vignettes/seinebasin/V01_First_network.Rmd
@@ -53,15 +53,15 @@ griwrm
 
 The diagram of the network structure is represented below with in blue the upstream nodes with a GR4J model and in green the intermediate nodes with an SD (GR4J + LAG) model.
 
-```{r DiagramGRiwrm, fig.height = 7, fig.width = 8}
-DiagramGRiwrm(griwrm)
+```{r plot, fig.height = 7, fig.width = 8}
+plot(griwrm)
 ```
 
 
 
 ## Observation time series
 
-The daily mean precipitation and potential evaporation at the scale of the intermediate sub-basins are extracted from the SAFRAN reanalysis [@vidal50yearHighresolutionAtmospheric2010]. 
+The daily mean precipitation and potential evaporation at the scale of the intermediate sub-basins are extracted from the SAFRAN reanalysis [@vidal50yearHighresolutionAtmospheric2010].
 
 The daily naturalised flow is provided by @hydratecActualisationBaseDonnees2011a.
 
diff --git a/vignettes/seinebasin/V04_Open-loop_influenced_flow.Rmd b/vignettes/seinebasin/V04_Open-loop_influenced_flow.Rmd
index 93c45c8..4794d47 100644
--- a/vignettes/seinebasin/V04_Open-loop_influenced_flow.Rmd
+++ b/vignettes/seinebasin/V04_Open-loop_influenced_flow.Rmd
@@ -176,7 +176,7 @@ reservoir_connections$model <- NA
 reservoir_connections$area <- NA
 load("_cache/V01.RData")
 griwrm2 <- rbind(griwrm, reservoir_connections[, names(griwrm)])
-DiagramGRiwrm(griwrm2)
+plot(griwrm2)
 ```
 
 
diff --git a/vignettes/seinebasin/V05_Open-loop_influenced_flow_calibration.Rmd b/vignettes/seinebasin/V05_Open-loop_influenced_flow_calibration.Rmd
index 2fe2084..8e268d8 100644
--- a/vignettes/seinebasin/V05_Open-loop_influenced_flow_calibration.Rmd
+++ b/vignettes/seinebasin/V05_Open-loop_influenced_flow_calibration.Rmd
@@ -37,7 +37,7 @@ Remove extra items from global configuration
 selectedNodes <- c("MARNE_P23", "STDIZ_04", "LOUVE_19", "VITRY_25", "MARNE_P28", "MARNE_R25", "CHALO_21", "MONTR_18", "NOISI_17")
 griwrm3 <- griwrm2[griwrm2$id %in% selectedNodes,]
 griwrm3[griwrm3$id == "NOISI_17", c("down", "length")] = NA # Downstream station instead of PARIS_05
-DiagramGRiwrm(griwrm3)
+plot(griwrm3)
 ```
 
 Generate `GRiwrmInputsModel` object
diff --git a/vignettes/seinebasin/V05b_Open-loop_influenced_flow_calibration_GR6J.Rmd b/vignettes/seinebasin/V05b_Open-loop_influenced_flow_calibration_GR6J.Rmd
index 58383c6..3ad0830 100644
--- a/vignettes/seinebasin/V05b_Open-loop_influenced_flow_calibration_GR6J.Rmd
+++ b/vignettes/seinebasin/V05b_Open-loop_influenced_flow_calibration_GR6J.Rmd
@@ -38,7 +38,7 @@ selectedNodes <- c("MARNE_P23", "STDIZ_04", "LOUVE_19", "VITRY_25", "MARNE_P28",
 griwrm3 <- griwrm2[griwrm2$id %in% selectedNodes,]
 griwrm3$model[!is.na(griwrm3$model)] <- "RunModel_GR6J"
 griwrm3[griwrm3$id == "NOISI_17", c("down", "length")] = NA # Downstream station instead of PARIS_05
-DiagramGRiwrm(griwrm3)
+plot(griwrm3)
 ```
 
 Generate `GRiwrmInputsModel` object
diff --git a/vignettes/seinebasin/V06_Naturalised_flow_simulation.Rmd b/vignettes/seinebasin/V06_Naturalised_flow_simulation.Rmd
index 06f0bc8..eca9066 100644
--- a/vignettes/seinebasin/V06_Naturalised_flow_simulation.Rmd
+++ b/vignettes/seinebasin/V06_Naturalised_flow_simulation.Rmd
@@ -22,7 +22,7 @@ library(seinebasin)
 
 This vignette is an example of flow naturalisation by modelling [@terrierStreamflowNaturalizationMethods2020].
 
-By using the model calibrated with influenced flow on the Marne River, it's now possible to model naturalised flows by dropping the connections to the Marne reservoir from the model. 
+By using the model calibrated with influenced flow on the Marne River, it's now possible to model naturalised flows by dropping the connections to the Marne reservoir from the model.
 
 ## Set the data
 
@@ -49,7 +49,7 @@ Remove extra items from global configuration
 selectedNodes <- c("STDIZ_04", "LOUVE_19", "VITRY_25", "CHALO_21", "MONTR_18", "NOISI_17")
 griwrm4 <- griwrm[griwrm$id %in% selectedNodes,]
 griwrm4[griwrm4$id == "NOISI_17", c("down", "length")] = NA # Downstream station instead of PARIS_05
-DiagramGRiwrm(griwrm4)
+plot(griwrm4)
 ```
 
 Generate `GRiwrmInputsModel` object
-- 
GitLab