Skip to content

Commit

Permalink
Merge pull request #68 from johanneskoch94/main
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskoch94 authored Jun 1, 2023
2 parents c8268b1 + 03753c8 commit d45b792
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '2158950'
ValidationKey: '2341080'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]",
Expand Down Expand Up @@ -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
12 changes: 5 additions & 7 deletions R/calcGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#'
Expand All @@ -42,8 +42,6 @@
#' }
#'
#' @examples \dontrun{
#' library(mrdrivers)
#'
#' # Return default scenarios
#' calcOutput("GDP")
#'
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion R/calcGDPHarmonized.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions R/calcGDPpc.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#' @rdname calcGDP
#' @examples \dontrun{
#' library(mrdrivers)
#' calcOutput("GDPpc")
#' }
#'
Expand All @@ -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
}
Expand All @@ -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.")
Expand Down
3 changes: 3 additions & 0 deletions R/calcLabour.R
Original file line number Diff line number Diff line change
@@ -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, ...)
Expand Down
1 change: 0 additions & 1 deletion R/calcPopulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#' @inherit calcGDP seealso
#'
#' @examples \dontrun{
#' library(mrdrivers)
#' # Return the default scenarios
#' calcOutput("Population")
#'
Expand Down
1 change: 0 additions & 1 deletion R/calcRatioPPP2MER.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#' @inherit madrat::calcOutput return
#' @seealso [madrat::calcOutput()]
#' @examples \dontrun{
#' library(mrdrivers)
#' calcOutput("RatioPPP2MER")
#' }
#'
Expand Down
1 change: 0 additions & 1 deletion R/calcUrban.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
#' }
#'
Expand Down
18 changes: 9 additions & 9 deletions R/readPopulationTWN.R
Original file line number Diff line number Diff line change
Expand Up @@ -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="_")
Expand All @@ -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
}
10 changes: 6 additions & 4 deletions R/toolCheckUserInput.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

Expand All @@ -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."))
}
}
6 changes: 5 additions & 1 deletion R/toolGetScenarioDefinition.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -103,7 +103,7 @@ In case of questions / problems please contact Johannes Koch <jokoch@pik-potsdam

To cite package **mrdrivers** in publications use:

Koch J, Soergel B, Leip D, Benke F, Dietrich J (2023). _mrdrivers: Create GDP and Population Scenarios_. R package version 1.1.1, <URL: https://pik-piam.github.io/mrdrivershttps://github.com/pik-piam/mrdrivers>.
Koch J, Soergel B, Leip D, Benke F, Dietrich J (2023). _mrdrivers: Create GDP and Population Scenarios_. R package version 1.2.0, <https://pik-piam.github.io/mrdrivershttps://github.com/pik-piam/mrdrivers>.

A BibTeX entry for LaTeX users is

Expand All @@ -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},
}
Expand Down
11 changes: 4 additions & 7 deletions man/calcGDP.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/calcGDPPast.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/calcPopulation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/calcRatioPPP2MER.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/readPopulationTWN.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion vignettes/scenarios.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d45b792

Please sign in to comment.