Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle missing years in magclass object #28

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ValidationKey: '2027862'
ValidationKey: '2072720'
AutocreateReadme: no
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
AcceptedNotes: ~
AcceptedNotes: 'Namespace in Imports field not imported from: ‘crayon’'
allowLinterWarnings: yes
enforceVersionUpdate: no
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.4
date-released: '2024-07-26'
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.4
Date: 2024-07-26
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
14 changes: 8 additions & 6 deletions R/check_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ check_user_input <- function(gdp,
verbose,
return_cfs) {

check_gdp(gdp)
require_year_column <- any(grepl("current", c(unit_in, unit_out)))

check_gdp(gdp, require_year_column)
check_unit_in_out(unit_in, unit_out)
source <- check_source(source)
check_use_USA_deflator_for_all(use_USA_deflator_for_all, unit_in, unit_out)
Expand All @@ -27,7 +29,7 @@ check_user_input <- function(gdp,


# Check 'gdp' input parameter
check_gdp <- function(gdp) {
check_gdp <- function(gdp, require_year_column) {
if (is.data.frame(gdp)) {
if (! "value" %in% colnames(gdp)) {
abort("Invalid 'gdp' argument. 'gdp' does not have the required 'value' column.")
Expand All @@ -38,12 +40,12 @@ 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.")
if (is.null(magclass::getYears(gdp)) && require_year_column) {
abort("Invalid 'gdp' argument. No year information in {crayon::bold('magpie')} object.")
}

} else {
abort("Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")
}
Expand Down
2 changes: 1 addition & 1 deletion 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
24 changes: 15 additions & 9 deletions R/transform_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ transform_user_input <- function(gdp, unit_in, unit_out, source, use_USA_deflato

# Convert to tibble, if necessary
if (class(gdp)[1] == "magpie") {

tb <- tibble::as_tibble(gdp)

if (is.null(magclass::getItems(gdp, dim = 2))) {
tb <- tb %>% tibble::add_column(year = 0, .after = 1)
}

# 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)
if (magclass::ndim(gdp, dim = 1) == 1) {
tb <- tb %>% dplyr::rename("iso3c" = 1, "year" = 2)
} else {
gdp <- gdp %>% dplyr::rename("iso3c" = 1, "spatial2" = 2, "year" = 3)
tb <- tb %>% dplyr::rename("iso3c" = 1, "spatial2" = 2, "year" = 3)
}

gdp <- tb
}

# Extract base years if they exist, and adjust string
Expand Down Expand Up @@ -126,11 +133,10 @@ transform_internal <- function(x, gdp, with_regions, require_year_column) {
# Transform into original gdp type
if (class(gdp)[1] == "magpie") {
# Check if the original magpie object had 2 spatial dimensions
spat2 <- all(grepl("\\.", magclass::getItems(gdp, dim = 1)))
if (!spat2) {
x <- magclass::as.magpie(x, spatial = "iso3c", temporal = "year")
if (magclass::ndim(gdp, dim = 1) == 1) {
x <- magclass::as.magpie(x, spatial = "iso3c", temporal = "year", datacol = "value")
} else {
x <- magclass::as.magpie(x, spatial = c("iso3c", "spatial2"), temporal = "year")
x <- magclass::as.magpie(x, spatial = c("iso3c", "spatial2"), temporal = "year", datacol = "value")
}
magclass::getSets(x) <- magclass::getSets(gdp)
return(x)
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.

17 changes: 11 additions & 6 deletions tests/testthat/test-01_check_user_input.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
test_that("gdp argument", {
gdp <- tibble::tibble("iso3c" = "EUR", "year" = 2010, "value2" = 100)
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. 'gdp' does not have the required 'value' column.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"Invalid 'gdp' argument. 'gdp' does not have the required 'value' column.")

gdp <- tibble::tibble("iso3c" = "EUR", "year" = 2007, "value" = "100")
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. The 'value' column is not numeric.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"Invalid 'gdp' argument. The 'value' column is not numeric.")

gdp <- "blabla"
expect_error(check_user_input(gdp), "Invalid 'gdp' argument. 'gdp' is neither a data-frame nor a 'magpie' object.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"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.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"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.")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = "constant 2017 Int$PPP"),
"Invalid 'gdp' argument. No year information in magpie object.")
}
})

test_that("unit arguments", {
gdp <- tibble::tibble("iso3c" = "EUR", "year" = 2010, "value" = 100)
expect_error(check_user_input(gdp, unit_in = "blabla"), "Invalid 'unit_in' argument")
expect_error(check_user_input(gdp, unit_in = "blabla", unit_out = "blabla"), "Invalid 'unit_in' argument")
expect_error(check_user_input(gdp, unit_in = "current LCU", unit_out = 2), "Invalid 'unit_out' argument")
})

Expand Down
9 changes: 8 additions & 1 deletion tests/testthat/test-05_convertGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,21 @@ 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
Loading