diff --git a/.buildlibrary b/.buildlibrary index 977188a..9c6dcd3 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '2158950' +ValidationKey: '2341080' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 29055b5..6a24fc3 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: 'mrdrivers: Create GDP and Population Scenarios' -version: 1.1.1 -date-released: '2023-04-03' +version: 1.2.0 +date-released: '2023-06-01' abstract: Create GDP and population scenarios This package constructs the GDP and population scenarios used as drivers in both the REMIND and MAgPIE models. authors: diff --git a/DESCRIPTION b/DESCRIPTION index 8386361..6c45e24 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: mrdrivers Type: Package Title: Create GDP and Population Scenarios -Version: 1.1.1 +Version: 1.2.0 Authors@R: c(person(given = "Johannes", family = "Koch", email = "jokoch@pik-potsdam.de", @@ -49,6 +49,6 @@ Suggests: Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 -Date: 2023-04-03 +Date: 2023-06-01 Config/testthat/edition: 3 VignetteBuilder: knitr diff --git a/R/calcGDP.R b/R/calcGDP.R index 74009cf..d743e96 100644 --- a/R/calcGDP.R +++ b/R/calcGDP.R @@ -19,11 +19,11 @@ #' @param unit A string specifying the unit of GDP. Can be either: #' \itemize{ #' \item "constant 2005 Int$PPP" (default): Scenarios are constructed in constant 2005 Int$PPP. -#' \item "constant 2005 US$MER": Scenarios are constructed in constant 2005 Int$PPP and converted before being -#' returned with [GDPuc::convertGDP()]. +#' \item "constant 2005 US$MER": Scenarios are constructed in constant 2005 Int$PPP and then converted with +#' [GDPuc::convertGDP()]. #' \item "constant 2017 Int$PPP": Scenarios are constructed in constant 2017 Int$PPP. -#' \item "constant 2017 US$MER": Scenarios are constructed in constant 2017 Int$PPP and converted before being -#' returned with [GDPuc::convertGDP()]. +#' \item "constant 2017 US$MER": Scenarios are constructed in constant 2017 Int$PPP and then converted with +#' [GDPuc::convertGDP()]. #' } #' In all cases, GDP is returned in millions. #' @@ -42,8 +42,6 @@ #' } #' #' @examples \dontrun{ -#' library(mrdrivers) -#' #' # Return default scenarios #' calcOutput("GDP") #' @@ -76,7 +74,7 @@ calcGDP <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), supplementary = TRUE, ...) - if (average2020 && grepl("SSPsOld", scenario)) { + if (average2020 && any(grepl("SSPsOld", scenario))) { warning("Average 2020 is not compatible with SSPsOld. Setting to FALSE.") average2020 <- FALSE } diff --git a/R/calcGDPHarmonized.R b/R/calcGDPHarmonized.R index f207443..dfb58ea 100644 --- a/R/calcGDPHarmonized.R +++ b/R/calcGDPHarmonized.R @@ -8,7 +8,8 @@ calcGDPHarmonized <- function(args) { "GDPpcWithPop" = toolMultiplyGDPpcWithPop(args$scenario, args$unit), "calibSSP2EU" = toolGDPHarmonizeSSP2EU(args$past, args$future, args$unit), "past_transition" = toolHarmonizePastTransition(args$past$x, args$future$x, yEnd = 2050, aslist = TRUE), - stop(glue("Bad input for calcGDPHarmonized. Argument harmonization = '{args$harmonization}' is invalid.")) + stop(glue("Bad input for calcGDPHarmonized. Argument harmonization = '{args$harmonization}' is invalid. \\ + Possible values are: 'GDPpcWithPop', 'calibsSSP2EU' or 'past_transition'.")) ) list(x = harmonizedData$x, weight = NULL, unit = glue("mil. {args$unit}"), description = harmonizedData$description) } diff --git a/R/calcGDPpc.R b/R/calcGDPpc.R index 5ce23bd..bb45055 100644 --- a/R/calcGDPpc.R +++ b/R/calcGDPpc.R @@ -1,6 +1,5 @@ #' @rdname calcGDP #' @examples \dontrun{ -#' library(mrdrivers) #' calcOutput("GDPpc") #' } #' @@ -27,7 +26,7 @@ calcGDPpc <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), supplementary = TRUE, ...) - if (average2020 && grepl("SSPsOld", scenario)) { + if (average2020 && any(grepl("SSPsOld", scenario))) { warning("Average 2020 is not compatible with SSPsOld. Setting to FALSE.") average2020 <- FALSE } @@ -53,6 +52,7 @@ calcGDPpc <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), aggregate = FALSE, years = 2020) gdppc2020 <- gdp2020 / pop2020 + gdppc2020[is.nan(gdppc2020)] <- 0 getNames(gdppc2020) <- getNames(gdppc$x) gdppc$x[, 2020, ] <- gdppc2020 gdppc$description <- paste(gdppc$description, "|| 2020 value averaged over 2018-2022 time period.") diff --git a/R/calcLabour.R b/R/calcLabour.R index f6dad51..adcba12 100644 --- a/R/calcLabour.R +++ b/R/calcLabour.R @@ -1,4 +1,7 @@ #' @rdname calcPopulation +#' @examples \dontrun{ +#' calcOutput("Labour") +#' } calcLabour <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), ...) { toolCheckUserInput(driver = "Labour", args = c(list(...), as.list(environment()))) calcOutput("Driver", driver = "Labour", scenario = scenario, aggregate = FALSE, supplementary = TRUE, ...) diff --git a/R/calcPopulation.R b/R/calcPopulation.R index 3b8e4a7..6b2ac51 100644 --- a/R/calcPopulation.R +++ b/R/calcPopulation.R @@ -20,7 +20,6 @@ #' @inherit calcGDP seealso #' #' @examples \dontrun{ -#' library(mrdrivers) #' # Return the default scenarios #' calcOutput("Population") #' diff --git a/R/calcRatioPPP2MER.R b/R/calcRatioPPP2MER.R index afa938e..dbb939f 100644 --- a/R/calcRatioPPP2MER.R +++ b/R/calcRatioPPP2MER.R @@ -8,7 +8,6 @@ #' @inherit madrat::calcOutput return #' @seealso [madrat::calcOutput()] #' @examples \dontrun{ -#' library(mrdrivers) #' calcOutput("RatioPPP2MER") #' } #' diff --git a/R/calcUrban.R b/R/calcUrban.R index f6ee579..301f6c9 100644 --- a/R/calcUrban.R +++ b/R/calcUrban.R @@ -2,7 +2,6 @@ #' @param asShare If TRUE (default) urban population shares are returned. If FALSE, then urban population in millions is #' returned. #' @examples \dontrun{ -#' library(mrdrivers) #' calcOutput("Urban") #' } #' diff --git a/R/readPopulationTWN.R b/R/readPopulationTWN.R index c72af49..8b5b1bb 100644 --- a/R/readPopulationTWN.R +++ b/R/readPopulationTWN.R @@ -11,20 +11,20 @@ #' #' @seealso [madrat::readSource()] #' -#' @examples \dontrun{ -#' library(mrdrivers) -#' readSource("PopulationTWN", subtype = "medium")} +#' @examples \dontrun{ +#' readSource("PopulationTWN", subtype = "medium") +#' } #' @keywords internal readPopulationTWN <- function(subtype) { files <- c(medium = "A1. Population Projections - Medium Variant.xlsx", high = "A2. Population Projections - High Variant.xlsx", low = "A3. Population Projections - Low Variant.xlsx") - + file <- toolSubtypeSelect(subtype, files) - + twn <- as.data.frame(suppressMessages(readxl::read_excel(file, sheet = "M3", skip = 1))) twn <- twn[!is.na(twn[[2]]),] - + names(twn)[3] <- paste(names(twn)[2], twn[1, 3], sep="_") names(twn)[4] <- paste(names(twn)[2], twn[1, 4], sep="_") names(twn)[2] <- paste(names(twn)[2], twn[1, 2], sep="_") @@ -34,14 +34,14 @@ readPopulationTWN <- function(subtype) { names(twn)[9] <- paste(names(twn)[8], twn[1, 9], sep="_") names(twn)[10] <- paste(names(twn)[8], twn[1, 10], sep="_") names(twn)[8] <- paste(names(twn)[8], twn[1, 8], sep="_") - + twn <- twn[-1, ] twn <- tidyr::pivot_longer(twn, -"Year", names_to = "variable") twn$value <- as.numeric(twn$value) twn$variable <- gsub(" +", "_", twn$variable) twn$variable <- gsub("_years", " years", twn$variable, fixed = TRUE) - - + + x <- as.magpie(twn) x } diff --git a/R/toolCheckUserInput.R b/R/toolCheckUserInput.R index 862a4b8..13f7107 100644 --- a/R/toolCheckUserInput.R +++ b/R/toolCheckUserInput.R @@ -7,7 +7,8 @@ toolCheckUserInput <- function(driver, args) { overrideScen <- if (all(c("pastData", "futureData", "harmonization") %in% names(args))) TRUE else FALSE # Check population scenario availability for any GDPpc scenario - if (!overrideScen && driver == "GDPpc" && ! args$scenario %in% toolGetScenarioDefinition("Population")$scenario) { + if (!overrideScen && driver == "GDPpc" && + !all(args$scenario %in% toolGetScenarioDefinition("Population")$scenario)) { stop("GDPpc scenarios require equivalent population scenarios to use as weight.") } @@ -34,9 +35,10 @@ toolCheckUserInput <- function(driver, args) { } # Check parallel map-reduce compatibility - if (any(purrr::map_lgl(args, ~ !is.null(.x) && - length(.x) != 1 && - length(.x) != max(purrr::map_dbl(args, length))))) { + if (any(purrr::map_lgl(args, + ~ !is.null(.x) && + length(.x) != 1 && + length(.x) != max(purrr::map_dbl(args, length))))) { stop(glue("Arguments to calc{driver} need to be either length 1 or equal to the length of the longest argument.")) } } diff --git a/R/toolGetScenarioDefinition.R b/R/toolGetScenarioDefinition.R index 30354a7..0fd5d13 100644 --- a/R/toolGetScenarioDefinition.R +++ b/R/toolGetScenarioDefinition.R @@ -71,13 +71,17 @@ toolGetScenarioDefinition <- function(driver = NULL, scen = NULL, aslist = FALSE # End of scenario-design section s <- scenarios + if (exists("mrdrivers_scenarios")) { + message("Also considering user defined scenarios in mrdrivers_scenarios.") + s <- dplyr::bind_rows(s, get("mrdrivers_scenarios")) + } if (!is.null(driver)) { availableDrivers <- dplyr::pull(scenarios, driver) %>% unique() if (!all(driver %in% availableDrivers)) { stop(glue::glue("Unknown driver. Available drivers are: {paste(availableDrivers, collapse = ', ')}")) } - s <- dplyr::filter(scenarios, driver %in% !!driver) + s <- dplyr::filter(s, driver %in% !!driver) } if (!is.null(scen)) { diff --git a/README.md b/README.md index 68602bf..5305312 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Create GDP and Population Scenarios -R package **mrdrivers**, version **1.1.1** +R package **mrdrivers**, version **1.2.0** [![CRAN status](https://www.r-pkg.org/badges/version/mrdrivers)](https://cran.r-project.org/package=mrdrivers) [![R build status](https://pik-piam.github.io/mrdrivers/workflows/check/badge.svg)](https://pik-piam.github.io/mrdrivers/actions) [![codecov](https://codecov.io/gh/mrdrivers/branch/master/graph/badge.svg)](https://app.codecov.io/gh/mrdrivers) [![r-universe](https://pik-piam.r-universe.dev/badges/mrdrivers)](https://pik-piam.r-universe.dev/builds) @@ -103,7 +103,7 @@ In case of questions / problems please contact Johannes Koch . +Koch J, Soergel B, Leip D, Benke F, Dietrich J (2023). _mrdrivers: Create GDP and Population Scenarios_. R package version 1.2.0, . A BibTeX entry for LaTeX users is @@ -112,7 +112,7 @@ A BibTeX entry for LaTeX users is title = {mrdrivers: Create GDP and Population Scenarios}, author = {Johannes Koch and Bjoern Soergel and Deborra Leip and Falk Benke and Jan Philipp Dietrich}, year = {2023}, - note = {R package version 1.1.1}, + note = {R package version 1.2.0}, url = {https://pik-piam.github.io/mrdrivers}, url = {https://github.com/pik-piam/mrdrivers}, } diff --git a/man/calcGDP.Rd b/man/calcGDP.Rd index 32a8e53..7b20f42 100644 --- a/man/calcGDP.Rd +++ b/man/calcGDP.Rd @@ -26,11 +26,11 @@ calcGDPpc( \item{unit}{A string specifying the unit of GDP. Can be either: \itemize{ \item "constant 2005 Int$PPP" (default): Scenarios are constructed in constant 2005 Int$PPP. -\item "constant 2005 US$MER": Scenarios are constructed in constant 2005 Int$PPP and converted before being -returned with \code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. +\item "constant 2005 US$MER": Scenarios are constructed in constant 2005 Int$PPP and then converted with +\code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. \item "constant 2017 Int$PPP": Scenarios are constructed in constant 2017 Int$PPP. -\item "constant 2017 US$MER": Scenarios are constructed in constant 2017 Int$PPP and converted before being -returned with \code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. +\item "constant 2017 US$MER": Scenarios are constructed in constant 2017 Int$PPP and then converted with +\code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. } In all cases, GDP is returned in millions.} @@ -61,8 +61,6 @@ See the vignette: \code{vignette("scenarios")} for scenario options, definitions } \examples{ \dontrun{ -library(mrdrivers) - # Return default scenarios calcOutput("GDP") @@ -74,7 +72,6 @@ calcOutput("GDP", scenario = "SSPsOld", extension2150 = "constant", average2020 } \dontrun{ -library(mrdrivers) calcOutput("GDPpc") } diff --git a/man/calcGDPPast.Rd b/man/calcGDPPast.Rd index 9ed1bd4..72de2ef 100644 --- a/man/calcGDPPast.Rd +++ b/man/calcGDPPast.Rd @@ -30,11 +30,11 @@ See the "Combining data sources with '-'" section below for how to combine data \item{unit}{A string specifying the unit of GDP. Can be either: \itemize{ \item "constant 2005 Int$PPP" (default): Scenarios are constructed in constant 2005 Int$PPP. -\item "constant 2005 US$MER": Scenarios are constructed in constant 2005 Int$PPP and converted before being -returned with \code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. +\item "constant 2005 US$MER": Scenarios are constructed in constant 2005 Int$PPP and then converted with +\code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. \item "constant 2017 Int$PPP": Scenarios are constructed in constant 2017 Int$PPP. -\item "constant 2017 US$MER": Scenarios are constructed in constant 2017 Int$PPP and converted before being -returned with \code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. +\item "constant 2017 US$MER": Scenarios are constructed in constant 2017 Int$PPP and then converted with +\code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. } In all cases, GDP is returned in millions.} diff --git a/man/calcPopulation.Rd b/man/calcPopulation.Rd index ec703e3..c16f106 100644 --- a/man/calcPopulation.Rd +++ b/man/calcPopulation.Rd @@ -42,7 +42,6 @@ See the vignette: \code{vignette("scenarios")} for scenario options, definitions } \examples{ \dontrun{ -library(mrdrivers) # Return the default scenarios calcOutput("Population") @@ -53,7 +52,9 @@ calcOutput("Population", scenario = "SSP2EU") calcOutput("Population", scenario = "ISIMIP", extension2150 = "none", aggregate = FALSE) } \dontrun{ -library(mrdrivers) +calcOutput("Labour") +} +\dontrun{ calcOutput("Urban") } diff --git a/man/calcRatioPPP2MER.Rd b/man/calcRatioPPP2MER.Rd index 1823b60..a6de2be 100644 --- a/man/calcRatioPPP2MER.Rd +++ b/man/calcRatioPPP2MER.Rd @@ -22,7 +22,6 @@ Use the when argument to switch the year of the conversion factor. } \examples{ \dontrun{ -library(mrdrivers) calcOutput("RatioPPP2MER") } diff --git a/man/readPopulationTWN.Rd b/man/readPopulationTWN.Rd index 261166a..84d0a19 100644 --- a/man/readPopulationTWN.Rd +++ b/man/readPopulationTWN.Rd @@ -18,9 +18,9 @@ A magpie object of the TWN population data Read-in Population projections for Taiwan } \examples{ -\dontrun{ -library(mrdrivers) -readSource("PopulationTWN", subtype = "medium")} +\dontrun{ +readSource("PopulationTWN", subtype = "medium") +} } \seealso{ \code{\link[madrat:readSource]{madrat::readSource()}} diff --git a/vignettes/scenarios.Rmd b/vignettes/scenarios.Rmd index 7b7113d..045935c 100644 --- a/vignettes/scenarios.Rmd +++ b/vignettes/scenarios.Rmd @@ -29,7 +29,21 @@ library(magrittr) toolGetScenarioDefinition() %>% print(n = 200) ``` -So for example, there are currently 6 GDPpc scenarios available: the SSPs, the SDPs, SSP2EU, noCovid, longCovid and shortCovid scenarios. +So for example, there are currently 7 GDPpc scenarios available: the SSPs, the SDPs, SSP2EU, noCovid, longCovid, shortCovid and SSPsOld scenarios. + +# User Defined Scenarios + +The user can create custom scenarios by creating a tibble called "mrdivers_scenarios" in the global environment, and filling it with the desired scenario definitions. The structure of the "mrdivers_scenarios" object should be identical to that of the return object of `toolGetScenarioDefinition()`, and the scenario building blocks have to be available. For example, say the user wanted to create SSP scenarios, but without using the Missing Islands data-set. The following command executed in the global environment would make the "nomi" (no-missing islands) scenario available. + +```{r} +mrdrivers_scenarios <- tibble::tribble( + ~driver, ~scenario, ~pastData, ~futureData, ~harmonization, + "GDPpc", "nomi", "WDI", "SSPsOld", "calibSSPs", + "Population", "nomi", "WDI", "SSPs", "withPEAPandFuture", + "GDP", "nomi", "-", "-", "GDPpcWithPop" +) +``` + # Default Scenarios