diff --git a/.buildlibrary b/.buildlibrary index 3185ea4..291af36 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '99145' +ValidationKey: '118980' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 86d7991..20fe8f9 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: 'piamPlotComparison: Create comparison plots for your model results' -version: 0.0.5 -date-released: '2024-04-16' +version: 0.0.6 +date-released: '2024-04-17' abstract: A frameworks to create comparison plots for your model results. authors: - family-names: Benke diff --git a/DESCRIPTION b/DESCRIPTION index ff3fe72..7d13469 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: piamPlotComparison Title: Create comparison plots for your model results -Version: 0.0.5 -Date: 2024-04-16 +Version: 0.0.6 +Date: 2024-04-17 Authors@R: c( person("Falk", "Benke", , "benke@pik-potsdam.de", role = c("aut", "cre")), person("Christof", "Schoetz", role = "aut") @@ -15,6 +15,7 @@ Imports: knitr, magclass, mip (>= 0.148.11), + piamutils, quitte, rlang, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index 9fe5937..19dc818 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,9 +2,7 @@ export(calcTimeSeriesStats) export(compareScenarios) -export(deletePlus) export(showStatsTable) -export(substitutePlaceholder) importFrom(dplyr,"%>%") importFrom(dplyr,across) importFrom(dplyr,all_of) @@ -17,12 +15,6 @@ importFrom(dplyr,left_join) importFrom(dplyr,mutate) importFrom(dplyr,select) importFrom(dplyr,summarize) -importFrom(magclass,"getNames<-") -importFrom(magclass,getNames) -importFrom(magclass,is.magpie) -importFrom(magclass,read.report) importFrom(rlang,.data) importFrom(rlang,.env) importFrom(tidyr,pivot_wider) -importFrom(utils,read.csv) -importFrom(utils,write.table) diff --git a/R/modifyVariableNames.R b/R/modifyVariableNames.R deleted file mode 100644 index 3770896..0000000 --- a/R/modifyVariableNames.R +++ /dev/null @@ -1,155 +0,0 @@ -#' Delete "+" in Variable Names. Optionally Save in File with Suffix -#' "_withoutPlus" -#' -#' The function can take a multiple of input formats. It replaces "|+|", "|++|", -#' "|+++|", ... by "|" in variable names and in this "deletes" the -#' "plus-notation". -#' -#' @param mif A path to a mif-file (might be created by confGDX2MIF), a magpie -#' object containing reporting, a quitte object, or a character vector or -#' factor of variable names. -#' @param writemif A single truth value. Only used when mif is a file path. -#' Should the new mif with the adjusted names be saved? If yes the new name -#' will have the suffix "_withoutPlus.mif". -#' @return If mif is a file path and \code{writemif} is \code{FALSE}, a MAgPIE -#' object. If mif is not a file path the result is the object \code{mif} with -#' replaced variable names. -#' -#' @examples -#' deletePlus(c( -#' "ab|cd", # "ab|cd" -#' "de|+|fgh", # "de|fgh" -#' "i|j|++|k|l|++++|mno|p", # "i|j|k|l|mno|p" -#' "++|q+|+r|+p+|u+v|+", # "++|q+|+r|+p+|u+v|+" -#' "w|+|+|x")) # "w|x" -#' @export -deletePlus <- function(mif, writemif = FALSE) { - modifyVariableNames( - mif, - modify = .deletePlusString, - writeMifSuffix = if (writemif) "_withoutPlus" else NULL - ) -} - -.deletePlusString <- function(s) { - pattern <- "\\|\\++\\|" - replacement <- "|" - while (any(grepl(pattern, s))) { - s <- gsub(pattern, replacement, s) - } - return(s) -} - -#' Substitute Placeholders in Variable Names. -#' -#' @param x A path to a mif-file (might be created by confGDX2MIF), a quitte -#' object, or a character vector or factor of variable names. -#' @param writeMifSuffix \code{NULL} or a suffix for the filename if the result -#' should be written as a mif-file. -#' @param substitutions A named character vector of placeholders with -#' replacements as names. -#' @return If \code{x} is not a file path the result is the object \code{x} with -#' replaced variable names. -#' @examples -#' substitutePlaceholder(c( -#' "3$dot$5", # "3.5" -#' "3$dot5", # "3$dot5" -#' "3.5", # "3.5" -#' "$dot$dot$dot$dot$dot$")) # ".dot.dot." -#' @export -substitutePlaceholder <- function( - x, writeMifSuffix = NULL, - substitutions = c( - "." = "$dot$" - )) { - modifyVariableNames( - x, - modify = .substitutePlaceholderString, - writeMifSuffix = writeMifSuffix, - substitutions = substitutions - ) -} - -.substitutePlaceholderString <- function(s, substitutions) { - for (i in seq_along(substitutions)) { - placeholder <- substitutions[i] - replacement <- names(substitutions)[i] - while (any(grepl(placeholder, s, fixed = TRUE))) { - s <- gsub(placeholder, replacement, s, fixed = TRUE) - } - } - - return(s) -} - -#'Modify Variable Names -#' -#' @param x A path to a mif-file (might be created by confGDX2MIF), a magpie -#' object containing reporting, a quitte object, or a character vector or -#' factor of variable names. -#' @param writeMifSuffix \code{NULL} or a suffix for the file name if the result -#' should be written as a mif-file. -#' @param modify A function that modifies a character vector. -#' @param ... Further arguments for the modify function. -#' @return If \code{x} is a file path and \code{writeMifSuffix} is \code{NULL}, -#' a MAgPIE object. If \code{x} is not a file path the result is the object -#' \code{x} with replaced variable names. -#' @importFrom utils write.table read.csv -#' @importFrom magclass read.report getNames getNames<- is.magpie -modifyVariableNames <- function(x, modify, writeMifSuffix = NULL, ...) { - - # Does x refer to a file? - if (is.character(x) && length(x) == 1 && file.exists(x)) { - - # Should the output be written to a file? - if (is.character(writeMifSuffix) == TRUE) { - - # read in mif-reporting - mifData <- read.csv(x, sep = ";", check.names = FALSE) - - # drop the empty last column of .mif format - if (all(is.na(mifData[, ncol(mifData)]))) { - mifData[, ncol(mifData)] <- "" - } - - mifData$Variable <- modify(mifData$Variable, ...) - - # save reporting with new names under a new name - oldName <- gsub("\\.mif$", "", x) - newName <- paste0(oldName, writeMifSuffix, ".mif") - write.table(mifData, file = newName, quote = FALSE, sep = ";", row.names = FALSE) - - } else { - - # read in mif-reporting - mifData <- read.report(x, as.list = FALSE) - getNames(mifData) <- modify(getNames(mifData), ...) - return(mifData) - } - - } else { # x does not refer to a file. - - if (is.magpie(x)) { - getNames(x) <- modify(getNames(x), ...) - return(x) - } - - if (quitte::is.quitte(x)) { - levels(x$variable) <- modify(levels(x$variable), ...) - return(x) - } - - if (is.factor(x)) { - levels(x) <- modify(levels(x), ...) - return(x) - } - - if (is.character(x)) { - x <- modify(x, ...) - return(x) - } - - stop("Cannot handle input format.") - } -} - diff --git a/README.md b/README.md index 43e7681..f6bc2a8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Create comparison plots for your model results -R package **piamPlotComparison**, version **0.0.5** +R package **piamPlotComparison**, version **0.0.6** [![CRAN status](https://www.r-pkg.org/badges/version/piamPlotComparison)](https://cran.r-project.org/package=piamPlotComparison) [![R build status](https://github.com/pik-piam/piamPlotComparison/workflows/check/badge.svg)](https://github.com/pik-piam/piamPlotComparison/actions) [![codecov](https://codecov.io/gh/pik-piam/piamPlotComparison/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/piamPlotComparison) [![r-universe](https://pik-piam.r-universe.dev/badges/piamPlotComparison)](https://pik-piam.r-universe.dev/builds) @@ -46,7 +46,7 @@ In case of questions / problems please contact Falk Benke To cite package **piamPlotComparison** in publications use: -Benke F, Schoetz C (2024). _piamPlotComparison: Create comparison plots for your model results_. R package version 0.0.5, . +Benke F, Schoetz C (2024). _piamPlotComparison: Create comparison plots for your model results_. R package version 0.0.6, . A BibTeX entry for LaTeX users is @@ -55,7 +55,7 @@ A BibTeX entry for LaTeX users is title = {piamPlotComparison: Create comparison plots for your model results}, author = {Falk Benke and Christof Schoetz}, year = {2024}, - note = {R package version 0.0.5}, + note = {R package version 0.0.6}, url = {https://github.com/pik-piam/piamPlotComparison}, } ``` diff --git a/inst/compareScenarios/cs_main.Rmd b/inst/compareScenarios/cs_main.Rmd index 72e46f9..b9b507c 100644 --- a/inst/compareScenarios/cs_main.Rmd +++ b/inst/compareScenarios/cs_main.Rmd @@ -90,6 +90,7 @@ library(tidyverse) suppressMessages(library(kableExtra)) library(quitte) library(mip) +library(piamUtils) ``` @@ -210,11 +211,11 @@ data <- data %>% # In the variable names, replace `$dot$` by `.`. mutate( - variable = substitutePlaceholder(variable)) %>% + variable = piamUtils::substitutePlaceholder(variable)) %>% # In the variable names, replace `|+|`, `|++|`, `|+++|`, ... by `|`. mutate( varplus = as.character(variable), - variable = deletePlus(variable)) + variable = piamUtils::removePlus(variable)) # Filter regions. if (!is.null(params$reg)) { diff --git a/man/deletePlus.Rd b/man/deletePlus.Rd deleted file mode 100644 index a046674..0000000 --- a/man/deletePlus.Rd +++ /dev/null @@ -1,36 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/modifyVariableNames.R -\name{deletePlus} -\alias{deletePlus} -\title{Delete "+" in Variable Names. Optionally Save in File with Suffix -"_withoutPlus"} -\usage{ -deletePlus(mif, writemif = FALSE) -} -\arguments{ -\item{mif}{A path to a mif-file (might be created by confGDX2MIF), a magpie -object containing reporting, a quitte object, or a character vector or -factor of variable names.} - -\item{writemif}{A single truth value. Only used when mif is a file path. -Should the new mif with the adjusted names be saved? If yes the new name -will have the suffix "_withoutPlus.mif".} -} -\value{ -If mif is a file path and \code{writemif} is \code{FALSE}, a MAgPIE - object. If mif is not a file path the result is the object \code{mif} with - replaced variable names. -} -\description{ -The function can take a multiple of input formats. It replaces "|+|", "|++|", -"|+++|", ... by "|" in variable names and in this "deletes" the -"plus-notation". -} -\examples{ -deletePlus(c( - "ab|cd", # "ab|cd" - "de|+|fgh", # "de|fgh" - "i|j|++|k|l|++++|mno|p", # "i|j|k|l|mno|p" - "++|q+|+r|+p+|u+v|+", # "++|q+|+r|+p+|u+v|+" - "w|+|+|x")) # "w|x" -} diff --git a/man/modifyVariableNames.Rd b/man/modifyVariableNames.Rd deleted file mode 100644 index 322f83f..0000000 --- a/man/modifyVariableNames.Rd +++ /dev/null @@ -1,28 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/modifyVariableNames.R -\name{modifyVariableNames} -\alias{modifyVariableNames} -\title{Modify Variable Names} -\usage{ -modifyVariableNames(x, modify, writeMifSuffix = NULL, ...) -} -\arguments{ -\item{x}{A path to a mif-file (might be created by confGDX2MIF), a magpie -object containing reporting, a quitte object, or a character vector or -factor of variable names.} - -\item{modify}{A function that modifies a character vector.} - -\item{writeMifSuffix}{\code{NULL} or a suffix for the file name if the result -should be written as a mif-file.} - -\item{...}{Further arguments for the modify function.} -} -\value{ -If \code{x} is a file path and \code{writeMifSuffix} is \code{NULL}, - a MAgPIE object. If \code{x} is not a file path the result is the object - \code{x} with replaced variable names. -} -\description{ -Modify Variable Names -} diff --git a/man/substitutePlaceholder.Rd b/man/substitutePlaceholder.Rd deleted file mode 100644 index 2b797f0..0000000 --- a/man/substitutePlaceholder.Rd +++ /dev/null @@ -1,32 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/modifyVariableNames.R -\name{substitutePlaceholder} -\alias{substitutePlaceholder} -\title{Substitute Placeholders in Variable Names.} -\usage{ -substitutePlaceholder(x, writeMifSuffix = NULL, substitutions = c(. = "$dot$")) -} -\arguments{ -\item{x}{A path to a mif-file (might be created by confGDX2MIF), a quitte -object, or a character vector or factor of variable names.} - -\item{writeMifSuffix}{\code{NULL} or a suffix for the filename if the result -should be written as a mif-file.} - -\item{substitutions}{A named character vector of placeholders with -replacements as names.} -} -\value{ -If \code{x} is not a file path the result is the object \code{x} with - replaced variable names. -} -\description{ -Substitute Placeholders in Variable Names. -} -\examples{ -substitutePlaceholder(c( - "3$dot$5", # "3.5" - "3$dot5", # "3$dot5" - "3.5", # "3.5" - "$dot$dot$dot$dot$dot$")) # ".dot.dot." -}