Skip to content

Commit

Permalink
Merge pull request pik-piam#27 from johanneskoch94/main
Browse files Browse the repository at this point in the history
Improve internal column detection
  • Loading branch information
johanneskoch94 authored Jun 7, 2024
2 parents b02d742 + e4c3300 commit d685b2f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '2007880'
ValidationKey: '2027862'
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.1
date-released: '2024-06-06'
version: 1.0.2
date-released: '2024-06-07'
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
4 changes: 2 additions & 2 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.1
Date: 2024-06-06
Version: 1.0.2
Date: 2024-06-07
Authors@R:
person("Johannes", "Koch", , "jokoch@pik-potsdam.de", role = c("aut", "cre"))
Description: Convert GDP time series data from one unit to
Expand Down
17 changes: 15 additions & 2 deletions R/transform_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ transform_user_input <- function(gdp, unit_in, unit_out, source, use_USA_deflato

# Convert to tibble, if necessary
if (class(gdp)[1] == "magpie") {
gdp <- tibble::as_tibble(gdp) %>% dplyr::rename(tidyselect::any_of(c("iso3c" = "region")))
# 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)
}
}

# Extract base years if they exist, and adjust string
Expand Down Expand Up @@ -118,7 +125,13 @@ transform_internal <- function(x, gdp, with_regions, require_year_column) {

# Transform into original gdp type
if (class(gdp)[1] == "magpie") {
x <- magclass::as.magpie(x, spatial = "iso3c", temporal = "year")
# 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")
} else {
x <- magclass::as.magpie(x, spatial = c("iso3c", "spatial2"), temporal = "year")
}
magclass::getSets(x) <- magclass::getSets(gdp)
return(x)
}
Expand Down
12 changes: 9 additions & 3 deletions tests/testthat/test-05_convertGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ test_that("convertGDP magpie object", {
years = c(2001, 2002),
names = c("ssp1", "ssp2"),
fill = 100)
magclass::getSets(gdp_in)[1] <- c("iso3c")
gdp_in2 <- magclass::new.magpie("USA.FRA",
years = c(2001, 2002),
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_conv <- convertGDP(gdp_in, "current LCU", "constant 2017 Int$PPP")

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


Expand Down

0 comments on commit d685b2f

Please sign in to comment.