From d7e73db7b61bb78cb7bb1561d03ebd578f7fd53e Mon Sep 17 00:00:00 2001 From: Michaja Pehl Date: Tue, 9 Jul 2024 14:12:45 +0200 Subject: [PATCH 1/3] plotstyle() does not strip units from variable names, based on strip_units argument or plotstyle.strip_units option --- DESCRIPTION | 5 +++-- R/mip-package.R | 12 ++++++++---- R/plotstyle.R | 15 ++++++++++++--- tests/testthat/test-plotstyle.R | 15 +++++++++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 tests/testthat/test-plotstyle.R diff --git a/DESCRIPTION b/DESCRIPTION index 9b2624e..dcbd4a1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,7 +25,6 @@ Depends: magclass, quitte (>= 0.3072) Imports: - RColorBrewer, data.table, dplyr, ggplot2, @@ -33,12 +32,14 @@ Imports: htmltools, lusweave (>= 1.43.2), plotly, + RColorBrewer, reshape2, rlang, shiny, + stringr, tidyr, trafficlight, - stringr + withr, Suggests: gdxrrw, knitr, diff --git a/R/mip-package.R b/R/mip-package.R index 01d621d..d5f09eb 100644 --- a/R/mip-package.R +++ b/R/mip-package.R @@ -1,14 +1,18 @@ #' The MIP R package -#' +#' #' Contains the routines for plotting multi model and multi scenario comparisons -#' +#' #' \tabular{ll}{ Package: \tab mip\cr Type: \tab Package\cr Version: \tab #' 7.6\cr Date: \tab 2016-06-13\cr License: \tab LGPL-3\cr LazyLoad: \tab #' yes\cr } -#' +#' #' @name mip-package #' @aliases mip-package mip #' @author David Klein -#' +#' #' Maintainer: Anastasis Giannousakis "_PACKAGE" + +ignore_unused_imports <- function() { + withr::with_options # used in tests +} diff --git a/R/plotstyle.R b/R/plotstyle.R index 2ba054e..82b73ff 100644 --- a/R/plotstyle.R +++ b/R/plotstyle.R @@ -3,6 +3,7 @@ #' Returns a named vector (using entity names) with style codes (e.g. colors) #' for given entities. #' +#' @md #' @param ... One or more strings or a vector of strings with names of entities #' (regions, variable names, etc.). Units in brackets "(US$2005/GJ)" will be #' ignored. If left empty all available entities will be used @@ -21,6 +22,9 @@ #' entities are expanded, non-matching entities are returned as the original #' expression. Does not generate default color maps. Implies \code{plot = #' FALSE} and \code{verbosity = 0}. +#' @param strip_units If `TRUE` everything from the first opening +#' brace (`'('`) on is stripped from the entity names. Defaults to `TRUE` and +#' can be set globally using the `plotstyle.strip_units` option. #' @return Plot styles for given entities #' @section Colors for unknown entities: #' \if{html}{\figure{colors.png}{options: width="100\%"}} @@ -49,8 +53,11 @@ #' @importFrom grDevices colorRampPalette #' @importFrom stats runif -plotstyle <- function(..., out = "color", unknown = NULL, plot = FALSE, verbosity = getOption("plotstyle.verbosity"), - regexp = FALSE) { +plotstyle <- function(..., out = "color", unknown = NULL, plot = FALSE, + verbosity = getOption("plotstyle.verbosity"), + regexp = FALSE, + strip_units = getOption("plotstyle.strip_units", + default = TRUE)) { luplot <- list() luplot$plotstyle <- read.csv2( @@ -73,7 +80,9 @@ plotstyle <- function(..., out = "color", unknown = NULL, plot = FALSE, verbosit entity <- row.names(luplot$plotstyle) } else { entity[is.na(entity)] <- "NA" - entity <- unlist(lapply(strsplit(entity, " \\("), function(x) x[1])) + if (isTRUE(strip_units)) { + entity <- unlist(lapply(strsplit(entity, " \\("), function(x) x[1])) + } } uqEntity <- unique(entity) diff --git a/tests/testthat/test-plotstyle.R b/tests/testthat/test-plotstyle.R new file mode 100644 index 0000000..c830980 --- /dev/null +++ b/tests/testthat/test-plotstyle.R @@ -0,0 +1,15 @@ +test_that( + "plotstyle() does not strip units if told not to do so", + { + expect_identical(plotstyle("Forcing|CO2 (W/m2)"), + c(`Forcing|CO2` = "#e6194B")) + + expect_identical(plotstyle("Forcing|CO2 (W/m2)", strip_units = FALSE), + c(`Forcing|CO2 (W/m2)` = "#e6194B")) + + withr::with_options( + list('plotstyle.strip_units' = FALSE), + expect_identical(plotstyle("Forcing|CO2 (W/m2)"), + c(`Forcing|CO2 (W/m2)` = "#e6194B")) + ) + }) From 9d42b287f44f8abdcf4ac52c557682fc08de6d38 Mon Sep 17 00:00:00 2001 From: Michaja Pehl Date: Tue, 9 Jul 2024 14:41:10 +0200 Subject: [PATCH 2/3] lucode hubbub --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 6 +++--- README.md | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index e72c8be..101f341 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '295026826' +ValidationKey: '29670370' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index 1763c9c..4516c56 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ cff-version: 1.2.0 message: If you use this software, please cite it using the metadata from this file. type: software title: 'mip: Comparison of multi-model runs' -version: 0.148.21 -date-released: '2024-07-02' +version: 0.149.0 +date-released: '2024-07-09' abstract: Package contains generic functions to produce comparison plots of multi-model runs. authors: diff --git a/DESCRIPTION b/DESCRIPTION index dcbd4a1..dc1c44f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mip Title: Comparison of multi-model runs -Version: 0.148.21 -Date: 2024-07-02 +Version: 0.149.0 +Date: 2024-07-09 Authors@R: c( person("David", "Klein", , "dklein@pik-potsdam.de", role = c("aut", "cre")), person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = "aut"), @@ -49,4 +49,4 @@ VignetteBuilder: knitr Encoding: UTF-8 LazyData: yes -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 diff --git a/README.md b/README.md index e9e97d7..6a3c46d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Comparison of multi-model runs -R package **mip**, version **0.148.21** +R package **mip**, version **0.149.0** [![CRAN status](https://www.r-pkg.org/badges/version/mip)](https://cran.r-project.org/package=mip) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158586.svg)](https://doi.org/10.5281/zenodo.1158586) [![R build status](https://github.com/pik-piam/mip/workflows/check/badge.svg)](https://github.com/pik-piam/mip/actions) [![codecov](https://codecov.io/gh/pik-piam/mip/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mip) [![r-universe](https://pik-piam.r-universe.dev/badges/mip)](https://pik-piam.r-universe.dev/builds) @@ -47,7 +47,7 @@ In case of questions / problems please contact David Klein , R package version 0.148.21, . +Klein D, Dietrich J, Baumstark L, Humpenoeder F, Stevanovic M, Wirth S, Führlich P, Richters O, Rüter T (2024). _mip: Comparison of multi-model runs_. doi:10.5281/zenodo.1158586 , R package version 0.149.0, . A BibTeX entry for LaTeX users is @@ -56,7 +56,7 @@ A BibTeX entry for LaTeX users is title = {mip: Comparison of multi-model runs}, author = {David Klein and Jan Philipp Dietrich and Lavinia Baumstark and Florian Humpenoeder and Miodrag Stevanovic and Stephen Wirth and Pascal Führlich and Oliver Richters and Tonn Rüter}, year = {2024}, - note = {R package version 0.148.21}, + note = {R package version 0.149.0}, url = {https://github.com/pik-piam/mip}, doi = {10.5281/zenodo.1158586}, } From 2720140cfdb869048532ea9943aa1ccf7d89dfaf Mon Sep 17 00:00:00 2001 From: orichters Date: Tue, 9 Jul 2024 14:59:43 +0200 Subject: [PATCH 3/3] add strip_unit=FALSE to mipBarYearData and mipArea to avoid fails --- R/mipArea.R | 2 +- R/mipBarYearData.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/mipArea.R b/R/mipArea.R index 236ef6a..72467f2 100644 --- a/R/mipArea.R +++ b/R/mipArea.R @@ -217,7 +217,7 @@ mipArea <- function(x, stack_priority = c("variable", "region"), total = TRUE, s # get the same order of colors for elements that are not defined in plotstyle. if (!is.factor(x[[dimToStack]])) x[[dimToStack]] <- factor(x[[dimToStack]], levels = unique(x[[dimToStack]])) # use plotstyle colours and labels by default - p <- p + scale_fill_manual(values = plotstyle(levels(x[[dimToStack]])), + p <- p + scale_fill_manual(values = plotstyle(levels(x[[dimToStack]]), strip_units = FALSE), name = "") # increase y-axis limits to hide all-zero data that was set to -1e-36 diff --git a/R/mipBarYearData.R b/R/mipBarYearData.R index 6f652c5..d1e63c6 100644 --- a/R/mipBarYearData.R +++ b/R/mipBarYearData.R @@ -98,7 +98,7 @@ mipBarYearData <- function(x, colour = NULL, ylab = NULL, xlab = NULL, title = N } if (is.null(colour)) { - colour <- plotstyle(levels(x$variable)) + colour <- plotstyle(levels(x$variable), strip_units = FALSE) } # make plot