Skip to content

Commit

Permalink
Bump version with lucode2 and fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskoch94 committed Dec 15, 2023
1 parent c2bfe50 commit a886737
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '2742180'
ValidationKey: '2955900'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
gamstransfer=?ignore
any::lucode2
any::covr
any::madrat
Expand Down Expand Up @@ -56,6 +57,8 @@ jobs:
- name: Test coverage
shell: Rscript {0}
run: covr::codecov(quiet = FALSE)
run: |
nonDummyTests <- setdiff(list.files("./tests/testthat/"), c("test-dummy.R", "_snaps"))
if(length(nonDummyTests) > 0) covr::codecov(quiet = FALSE)
env:
NOT_CRAN: "true"
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exclude: '^tests/testthat/_snaps/.*$'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-case-conflict
- id: check-json
Expand All @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9019
rev: v0.3.2.9025
hooks:
- id: parsable-R
- id: deps-in-desc
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.4.0
date-released: '2023-08-18'
version: 1.5.0
date-released: '2023-12-15'
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.4.0
Version: 1.5.0
Authors@R: c(person(given = "Johannes",
family = "Koch",
email = "[email protected]",
Expand Down Expand Up @@ -48,6 +48,6 @@ Suggests:
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Date: 2023-08-18
Date: 2023-12-15
Config/testthat/edition: 3
VignetteBuilder: knitr
16 changes: 8 additions & 8 deletions R/calcGDPFuture.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ calcInternalGDPFutureSSPs <- function(unit) {
GDPuc::convertGDP("constant 2005 Int$PPP", unit, replace_NAs = c("linear", "no_conversion"))

y2 <- getYears(data2005PPP)[getYears(data2005PPP, as.integer = TRUE) > c15 &
getYears(data2005PPP, as.integer = TRUE) < 2100]
getYears(data2005PPP, as.integer = TRUE) < 2100]
dataFarFut <- data2005PPP[, y2, ] * NA

# Convert to 2017 Int$PPP using the 2017 value of base 2005 GDP deflator
Expand All @@ -86,22 +86,22 @@ calcInternalGDPFutureSSPs <- function(unit) {

data2017PPP <- mbind(dataNearFut, dataFarFut, data2100)

q <- data2005PPP / data2017PPP
ratio <- data2005PPP / data2017PPP
# For interpolation to work, the last and first values have to be non-NA/non-NaN
q[, 2100, ][is.na(q[, 2100, ])] <- 0
ratio[, 2100, ][is.na(ratio[, 2100, ])] <- 0
# The first 2 years of the SSP data set are incomplete. For countries that only lack data in these first 2 years,
# set NaN to 0.
q[, 2000, ][is.nan(q[, 2000, ]) & !is.nan(q[, 2010, ])] <- 0
ratio[, 2000, ][is.nan(ratio[, 2000, ]) & !is.nan(ratio[, 2010, ])] <- 0

q <- as.data.frame(q, rev = 2) %>%
ratio <- as.data.frame(ratio, rev = 2) %>%
dplyr::rename("value" = ".value") %>%
dplyr::arrange(.data$year) %>%
dplyr::group_by(.data$iso3c, .data$variable) %>%
dplyr::mutate(value = zoo::na.approx(.data$value)) %>%
dplyr::ungroup() %>%
as.magpie(tidy = TRUE)

data2017PPP <- data2005PPP / q
data2017PPP <- data2005PPP / ratio
data2017PPP[is.na(data2017PPP)] <- data2005PPP[is.na(data2017PPP)]
# Above should probably be "<- 0"
##################
Expand All @@ -110,7 +110,7 @@ calcInternalGDPFutureSSPs <- function(unit) {

# If unit was in $MER
if (constructUnit != unit) {
data <- GDPuc::convertGDP(data, constructUnit, unit, replace_NAs = c("linear", "no_conversion"))
data <- GDPuc::convertGDP(data, constructUnit, unit, replace_NAs = c("linear", "no_conversion"))
}

list(x = data, weight = NULL, unit = glue("mil. {unit}"), description = "SSP projections")
Expand All @@ -128,7 +128,7 @@ calcInternalGDPFutureSDPs <- function(unit) {

calcInternalGDPFutureSSP2EU <- function(unit) {
dataSSP2EU <- readSource("ARIADNE", "gdp_corona") %>%
GDPuc::convertGDP("constant 2005 Int$PPP", unit, replace_NAs = c("linear", "no_conversion"))
GDPuc::convertGDP("constant 2005 Int$PPP", unit, replace_NAs = c("linear", "no_conversion"))
dataSSP <- calcOutput("InternalGDPFutureSSPs", unit = unit, aggregate = FALSE)

# Get EU-27 countries
Expand Down
10 changes: 5 additions & 5 deletions R/calcGDPHarmonized.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ toolGDPHarmonizeSSP2EU <- function(past, future, unit) {
combined <- combined[, getYears(combined)[getYears(combined, as.integer = TRUE) <= 2100], ]

list(x = combined,
description = glue("equal to SSP2 in all countries except for EU countries. \\
For EU countries use {past$description} until 2021, \\
growth rates from Eurostat until 2024, \\
growth rates from {future$description} until 2070, \\
and converge to 2150 (bezier-extended) SSP2 values thereafter."))
description = glue("equal to SSP2 in all countries except for EU countries. \\
For EU countries use {past$description} until 2021, \\
growth rates from Eurostat until 2024, \\
growth rates from {future$description} until 2070, \\
and converge to 2150 (bezier-extended) SSP2 values thereafter."))
}
3 changes: 2 additions & 1 deletion R/calcPopulationFuture.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ calcInternalPopulationFuture <- function(PopulationFuture) { # nolint
"UN_PopDiv" = calcOutput("InternalPopulationFutureUN_PopDiv", aggregate = FALSE, supplementary = TRUE),
"MI" = calcOutput("InternalPopMI", aggregate = FALSE, supplementary = TRUE),
"SSPsOld" = calcOutput("InternalPopulationFutureSSPsOld", aggregate = FALSE, supplementary = TRUE),
stop("Bad input for PopulationFuture. Invalid 'PopulationFuture' argument."))
stop("Bad input for PopulationFuture. Invalid 'PopulationFuture' argument.")
)

data$x <- toolFinishingTouches(data$x)
data
Expand Down
6 changes: 3 additions & 3 deletions R/readIMF.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ readIMF <- function(subtype = "current_account", subset = "WEOOct2022all.xls") {
dplyr::mutate(value = gsub(",", "", .data$value),
dplyr::across(.cols = c("year", "value"),
~ suppressWarnings(as.double(.x))),
# The warnings that are being suppressed above, come from
# character strings that can't be converted to numeric, and
# are thus returned as NA.
# The warnings that are being suppressed above, come from
# character strings that can't be converted to numeric, and
# are thus returned as NA.
value = tidyr::replace_na(.data$value, 0)) %>%
tidyr::pivot_wider(names_from = "Subject Descriptor")

Expand Down
4 changes: 2 additions & 2 deletions R/readUN_PopDiv.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ readUN_PopDiv <- function(subtype = "estimates") { # nolint
#' @rdname readUN_PopDiv
#' @order 3
#' @param x MAgPIE object returned from readUN_PopDiv
convertUN_PopDiv <- function(x) {
convertUN_PopDiv <- function(x) { # nolint
toolGeneralConvert(x, no_remove_warning = "XKX")
}

#' @rdname readUN_PopDiv
#' @order 1
downloadUN_PopDiv <- function() { # nolint
url <- "https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/EXCEL_FILES/2_Population/WPP2022_POP_F01_1_POPULATION_SINGLE_AGE_BOTH_SEXES.xlsx"
url <- "https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/EXCEL_FILES/2_Population/WPP2022_POP_F01_1_POPULATION_SINGLE_AGE_BOTH_SEXES.xlsx" # nolint
utils::download.file(url, basename(url), quiet = TRUE)

# Compose meta data
Expand Down
6 changes: 3 additions & 3 deletions R/toolExtend2150.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ bezierExtension <- function(data, timeExtend) {

# If Bezier extension would lead to negative GDP, set bezier coordinates equal to start point
# (comes down to a constant extension instead)
for (i in 1:nr) for (j in 1:nd) if (bc[i, 2100, j] == 0 || bc[i, 2150, j] < 0 ) bc[i, , j] <- bc[i, 2100, j]
for (i in 1:nr) for (j in 1:nd) if (bc[i, 2100, j] == 0 || bc[i, 2150, j] < 0) bc[i, , j] <- bc[i, 2100, j]

x <- rep(c(2100, 2110, 2140, 2150), nr * nd)
y <- purrr::reduce(purrr::map(1:nd, ~purrr::reduce(purrr::map(1:nr, function(y) bc[y, ,.x]), c)), c)
y <- purrr::reduce(purrr::map(1:nd, ~purrr::reduce(purrr::map(1:nr, function(y) bc[y, , .x]), c)), c)
z <- purrr::reduce(purrr::map(1:(nr * nd), ~rep(.x, 4)), c)

# grid::bezierGrob returns the point in a weird graphical unit, and "only" returns 48 points, but is super fast.
bezierPoints <- grid::bezierGrob(x, y, id = z) %>% grid::bezierPoints()
cfx <- x[1] / as.numeric(bezierPoints[[1]]$x[[1]])
cfy <- y[1] / as.numeric(bezierPoints[[1]]$y[[1]])
id <- paste(purrr::reduce(purrr::map(getNames(data), ~rep(.x, nr)), c),
rep(getRegions(data), nd),
rep(getItems(data, 1), nd),
sep = "-")

extension <- purrr::map2(bezierPoints, id,
Expand Down
2 changes: 1 addition & 1 deletion R/toolGetEUcountries.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolGetEUcountries <- function(onlyWithARIADNEgdpData = FALSE) {
dplyr::pull(.data$CountryCode)

if (onlyWithARIADNEgdpData) {
x <- x[x %in% where(readSource("ARIADNE", "gdp_corona") != 0)$true$regions]
x <- x[x %in% where(readSource("ARIADNE", "gdp_corona") != 0)$true$regions]
}
x
}
12 changes: 7 additions & 5 deletions R/toolReduce.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ toolReduce <- function(x, mbindOrFillWith = "mbind") {
# Combine list elements using mbind and glue.
if (mbindOrFillWith == "mbind") {
x %>%
purrr::reduce(~ list(x = mbind(.x$x, .y$x),
weight = mbind(.x$weight, .y$weight),
unit = glue("{.x$unit} || {.y$unit}"),
description = glue("{.x$description} || {.y$description}")))
purrr::reduce(~ list(x = mbind(.x$x, .y$x),
weight = mbind(.x$weight, .y$weight),
unit = glue("{.x$unit} || {.y$unit}"),
description = glue("{.x$description} || {.y$description}")))
} else if (mbindOrFillWith == "fillWith") {

if (length(x) > 1) {
sep <- if (length(x) == 2) " (completed with" else c(" (completed with", c(rep(",", length(x) - 3), " and"))
closer <- c(rep("", length(sep) - 1), ")")
helper <- purrr::map2(sep, closer, c)
} else helper <- NULL
} else {
helper <- NULL
}

purrr::reduce2(x, helper,
~ list(x = toolFillWith(.x$x, .y$x),
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.4.0**
R package **mrdrivers**, version **1.5.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.4.0, <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.5.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.4.0},
note = {R package version 1.5.0},
url = {https://pik-piam.github.io/mrdrivers},
url = {https://github.com/pik-piam/mrdrivers},
}
Expand Down

0 comments on commit a886737

Please sign in to comment.