Skip to content

Commit

Permalink
Merge pull request #29 from johanneskoch94/main
Browse files Browse the repository at this point in the history
Allow magclass objects without years
  • Loading branch information
johanneskoch94 authored Aug 1, 2024
2 parents d685b2f + 12f9b9f commit 597c2c6
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '2027862'
ValidationKey: '2053408'
AutocreateReadme: no
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: 'GDPuc: Easily Convert GDP Data'
version: 1.0.2
date-released: '2024-06-07'
version: 1.0.3
date-released: '2024-08-01'
abstract: Convert GDP time series data from one unit to another. All common GDP units
are included, i.e. current and constant local currency units, US$ via market exchange
rates and international dollars via purchasing power parities.
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: GDPuc
Title: Easily Convert GDP Data
Version: 1.0.2
Date: 2024-06-07
Version: 1.0.3
Date: 2024-08-01
Authors@R:
person("Johannes", "Koch", , "[email protected]", role = c("aut", "cre"))
Description: Convert GDP time series data from one unit to
Expand Down Expand Up @@ -42,4 +42,4 @@ VignetteBuilder:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
6 changes: 0 additions & 6 deletions R/check_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ check_gdp <- function(gdp) {
} else if (inherits(gdp, "magpie")) {
# Check for magclass package
rlang::check_installed("magclass", reason = "in order for magpie objects to be recognized.")
# Check if there is years info
if (is.null(magclass::getYears(gdp))) {
# I need at least one explizit call to a crayon:: function... otherwise I get a CRAN note
h <- crayon::bold("magpie")
abort("Invalid 'gdp' argument. No year information in {h} object.")
}
} else {
abort("Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")
}
Expand Down
9 changes: 4 additions & 5 deletions R/convertGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#' \item "regional_average": missing conversion factors in the source object are replaced with
#' the regional average of the region to which the country belongs. This requires a region-mapping to
#' be passed to the function, see the with_regions argument.
#' \item "with USA": missing conversion factors in the source object are replaced with
#' \item "with_USA": missing conversion factors in the source object are replaced with
#' the conversion factors of the USA.
#' }
#' Can also be a vector with "linear" as first element, e.g. c("linear", 0) or c("linear", "no_conversion"),
Expand Down Expand Up @@ -142,10 +142,9 @@ convertGDP <- function(gdp,
{if ("base_x" %in% names(internal)) c(., "base_x" = internal$base_x) else .} %>%
{if ("base_y" %in% names(internal)) c(., "base_y" = internal$base_y) else .}


cli_inform(function() cli::cli_alert_info(
"Converting GDP with conversion factors from {crayon::blue(internal$source_name)}:")
)
# At least one explicit call to a crayon:: function is required to avoid CRAN note.
h <- crayon::blue(internal$source_name)
cli_inform(function() cli::cli_alert_info("Converting GDP with conversion factors from {h}:"))

# Call function
x <- do.call(f, a)
Expand Down
15 changes: 8 additions & 7 deletions R/transform_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ transform_user_input <- function(gdp, unit_in, unit_out, source, use_USA_deflato
# Convert to tibble, if necessary
if (class(gdp)[1] == "magpie") {
# Check if the magpie object has 2 spatial dimensions
spat2 <- all(grepl("\\.", magclass::getItems(gdp, dim = 1)))
gdp <- tibble::as_tibble(gdp)
if (!spat2) {
gdp <- gdp %>% dplyr::rename("iso3c" = 1, "year" = 2)
} else {
gdp <- gdp %>% dplyr::rename("iso3c" = 1, "spatial2" = 2, "year" = 3)
}
spat2 <- magclass::ndim(gdp, dim = 1) == 2
# Check if the magpie object has year info
hasYears <- !is.null(magclass::getYears(gdp))
# Transform to tibble and rename columns
gdp <- gdp %>% tibble::as_tibble() %>% dplyr::rename("iso3c" = 1)
if (spat2) gdp <- dplyr::rename(gdp, "spatial2" = 2)
if (hasYears && !spat2) gdp <- dplyr::rename(gdp, "year" = 2)
if (hasYears && spat2) gdp <- dplyr::rename(gdp, "year" = 3)
}

# Extract base years if they exist, and adjust string
Expand Down
2 changes: 1 addition & 1 deletion man/convertGDP.Rd

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

5 changes: 0 additions & 5 deletions tests/testthat/test-01_check_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ test_that("gdp argument", {
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")
gdp <- array()
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")

if (rlang::is_installed("magclass")) {
gdp <- magclass::new.magpie()
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. No year information in magpie object.")
}
})

test_that("unit arguments", {
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-02_transform_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,39 @@ test_that("source and unit year compatibility", {
unit_in = "constant 2010 LCU",
unit_out = "constant 2100 LCU",
source = "wb_wdi",
use_USA_deflator_for_all = FALSE,
with_regions = NULL,
replace_NAs = NULL))
expect_error(transform_user_input(gdp,
unit_in = "constant 2100 LCU",
unit_out = "constant 2010 LCU",
source = "wb_wdi",
use_USA_deflator_for_all = FALSE,
with_regions = NULL,
replace_NAs = NULL))

})

test_that("unit and year availability compatibility", {
gdp <- tibble::tibble("iso3c" = "DEU", "value" = 100)
gdp2 <- magclass::new.magpie("USA", years = NULL, names = c("ssp1", "ssp2"), fill = 100)

expect_error(transform_user_input(gdp,
unit_in = "current LCU",
unit_out = "constant 2010 LCU",
source = "wb_wdi",
use_USA_deflator_for_all = FALSE,
with_regions = NULL,
replace_NAs = NULL),
glue::glue("Invalid 'gdp' argument. 'gdp' does not have a 'year' column, required when \\
converting current values, and no other column could be identified in its stead."))
expect_error(transform_user_input(gdp2,
unit_in = "current LCU",
unit_out = "constant 2010 LCU",
source = "wb_wdi",
use_USA_deflator_for_all = FALSE,
with_regions = NULL,
replace_NAs = NULL),
glue::glue("Invalid 'gdp' argument. 'gdp' does not have a 'year' column, required when \\
converting current values, and no other column could be identified in its stead."))
})
8 changes: 7 additions & 1 deletion tests/testthat/test-05_convertGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,20 @@ test_that("convertGDP magpie object", {
years = c(2001, 2002),
names = c("ssp1", "ssp2"),
fill = 100)
gdp_in3 <- magclass::new.magpie("USA",
years = NULL,
names = c("ssp1", "ssp2"),
fill = 100)
gdp_conv <- convertGDP(gdp_in, "current LCU", "constant 2017 Int$PPP")
gdp_conv2 <- convertGDP(gdp_in2, "current LCU", "constant 2017 Int$PPP")

gdp_conv3 <- convertGDP(gdp_in3, "constant 2005 LCU", "constant 2017 Int$PPP")

expect_s4_class(gdp_conv, "magpie")
expect_s4_class(gdp_conv2, "magpie")
expect_s4_class(gdp_conv3, "magpie")
expect_mapequal(magclass::getSets(gdp_in), magclass::getSets(gdp_conv))
expect_mapequal(magclass::getSets(gdp_in2), magclass::getSets(gdp_conv2))
expect_mapequal(magclass::getSets(gdp_in3), magclass::getSets(gdp_conv3))
})


Expand Down

0 comments on commit 597c2c6

Please sign in to comment.