Skip to content

Commit

Permalink
Make GDPuc a madrat package and add tool-wrappers
Browse files Browse the repository at this point in the history
Also add !!! operator to rlang imports.
  • Loading branch information
johanneskoch94 committed Oct 11, 2024
1 parent f797dc5 commit 97b6b7a
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '2400720'
ValidationKey: '2600910'
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.2.0
date-released: '2024-10-10'
version: 1.3.0
date-released: '2024-10-11'
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
7 changes: 4 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.2.0
Date: 2024-10-10
Version: 1.3.0
Date: 2024-10-11
Authors@R:
person("Johannes", "Koch", , "[email protected]", role = c("aut", "cre"))
Description: Convert GDP time series data from one unit to
Expand All @@ -13,7 +13,8 @@ URL: https://github.com/pik-piam/GDPuc,
https://pik-piam.github.io/GDPuc/
BugReports: https://github.com/pik-piam/GDPuc/issues
Depends:
R (>= 2.10)
R (>= 2.10),
madrat (>= 3.6.4)
Imports:
cli (>= 2.4.0),
crayon,
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ export(convertCPI)
export(convertGDP)
export(convertSingle)
export(print_source_info)
export(toolConvertCPI)
export(toolConvertGDP)
export(toolConvertSingle)
import(madrat)
importFrom(magrittr,"%>%")
importFrom(rlang,"!!!")
importFrom(rlang,"!!")
importFrom(rlang,":=")
importFrom(rlang,.data)
2 changes: 1 addition & 1 deletion R/GDPuc-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
#' @name GDPuc-package
#' @aliases GDPuc-package GDPuc
#' @importFrom magrittr %>%
#' @importFrom rlang !! := .data
#' @importFrom rlang !! !!! := .data
#' @keywords internal
"_PACKAGE"
52 changes: 49 additions & 3 deletions R/convertGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ convertGDP <- function(gdp,
replace_NAs = NULL,
verbose = getOption("GDPuc.verbose", default = FALSE),
return_cfs = FALSE) {
# The following line needs to be updated every time the output of convertGDP is affected by an update!
# This is a trick, so that madrat caching works correctly. For more information, see the documentation of the madrat
# R-package.
"last changes 2024-10-11"

# Save all function arguments as list
arg <- as.list(environment())

Expand Down Expand Up @@ -184,7 +189,10 @@ convertGDP <- function(gdp,
#' unit_out = "constant 2015 Int$PPP")
#'
#' @export
convertCPI <- function(...) convertGDP(..., source = "wb_wdi_cpi")
convertCPI <- function(...) {
"!# @monitor GDPuc::convertGDP"
convertGDP(..., source = "wb_wdi_cpi")
}

#' @describeIn convertGDP Convert a single value, while specifying iso3c code and year. Simpler than creating a
#' single row tibble.
Expand All @@ -199,18 +207,56 @@ convertCPI <- function(...) convertGDP(..., source = "wb_wdi_cpi")
#' unit_in = "current LCU",
#' unit_out = "constant 2015 Int$PPP")
#' @export
convertSingle <- function(x, iso3c, year = NULL, ...) {
convertSingle <- function(x, iso3c, year = NULL, unit_in, unit_out, ...) {
"!# @monitor GDPuc::convertGDP"
tib <- tibble::tibble("iso3c" = iso3c, "value" = x)

if (!is.null(year)) {
tib <- tibble::add_column(tib, "year" = year, .before = "value")
}

tib_c <- convertGDP(gdp = tib, ...)
tib_c <- convertGDP(gdp = tib, unit_in, unit_out, ...)

if (tibble::is_tibble(tib_c)) {
return(tib_c$value)
} else {
return(tib_c)
}
}

#' @describeIn convertGDP Madrat wrapper around `convertGDP()`
#' @export
toolConvertGDP <- function(gdp,
unit_in,
unit_out,
source = "wb_wdi",
use_USA_cf_for_all = FALSE,
with_regions = NULL,
replace_NAs = NULL,
verbose = getOption("GDPuc.verbose", default = FALSE),
return_cfs = FALSE) {
"!# @monitor GDPuc::convertGDP"
convertGDP(gdp,
unit_in,
unit_out,
source,
use_USA_cf_for_all,
with_regions,
replace_NAs,
verbose,
return_cfs)
}

#' @describeIn convertGDP Madrat wrapper around `convertSingle()`
#' @export
toolConvertSingle <- function(x, iso3c, year = NULL, unit_in, unit_out, ...) {
"!# @monitor GDPuc::convertSingle"
convertSingle(x, iso3c, year = NULL, unit_in, unit_out, ...)
}

#' @describeIn convertGDP Madrat wrapper around `convertCPI(...)`
#' @export
toolConvertCPI <- function(...) {
"!# @monitor GDPuc::convertCPI"
convertCPI(...)
}
4 changes: 4 additions & 0 deletions R/imports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by lucode2: do not edit by hand

#' @import madrat
NULL
7 changes: 7 additions & 0 deletions R/madrat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.onAttach <- function(libname, pkgname) {
madrat::madratAttach(pkgname)
}

.onDetach <- function(libpath) {
madrat::madratDetach(libpath)
}
27 changes: 26 additions & 1 deletion man/convertGDP.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/test-06_replace_NAs.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,19 @@ test_that("convertGDP replace_NAs = c('linear', 'no_conversion')", {
expect_identical(dplyr::pull(gdp_conv[19:24, "value"]), rep(0, 6))
})

test_that("convertGDP use_USA_cf_for_all = TRUE", {
gdp <- tidyr::expand_grid("iso3c" = c("AIA", "DEU", "USA", "AFG"),
"year" = c(2010, 2015, 2025),
"SSP" = c("SSP1", "SSP2"), "value" = 100)

gdp_conv <- convertGDP(gdp,
unit_in = "constant 2005 Int$PPP",
unit_out = "constant 2019 US$MER",
use_USA_cf_for_all = TRUE)

expect_true(!any(is.na(gdp_conv)))
expect_identical(dplyr::filter(gdp_conv, .data$iso3c == "AIA") %>% dplyr::select(-"iso3c"),
dplyr::filter(gdp_conv, .data$iso3c == "USA") %>% dplyr::select(-"iso3c"),
dplyr::filter(gdp_conv, .data$iso3c == "DEU") %>% dplyr::select(-"iso3c"),
dplyr::filter(gdp_conv, .data$iso3c == "AFG") %>% dplyr::select(-"iso3c"))
})

0 comments on commit 97b6b7a

Please sign in to comment.