Skip to content

Commit

Permalink
Update conversion factors
Browse files Browse the repository at this point in the history
Update wdi conversion factors and data
Change USMER base year
Improve order of tests
Add the CPI as possible deflator
Use magclass::as_tibble() instead of custom function
Add example to convertGDP (fixes pik-piam#21)
Add two shortcut functions for using the CPI as deflator and convert a single value (fixed pik-piam#20)
Add options to convert to constant € and use only US deflator
Add with_USA option to replace_NAs
  • Loading branch information
johanneskoch94 committed Apr 16, 2024
1 parent 7a48808 commit ec57bc2
Show file tree
Hide file tree
Showing 29 changed files with 625 additions and 180 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: GDPuc
Title: Easily Convert GDP Data
Version: 0.11.1
Version: 1.0.0
Date: 2023-07-13
Authors@R:
person("Johannes", "Koch", , "jokoch@pik-potsdam.de", role = c("aut", "cre"))
Expand Down Expand Up @@ -42,4 +42,4 @@ VignetteBuilder:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(convertCPI)
export(convertGDP)
export(convertSingle)
export(print_source_info)
importFrom(magrittr,"%>%")
importFrom(rlang,"!!")
Expand Down
17 changes: 17 additions & 0 deletions R/adapt_source.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
adapt_source_USA <- function(gdp, source, replace_NAs) {
source_USA <- source %>%
dplyr::filter(.data$iso3c == "USA") %>%
dplyr::select("year", "USA_deflator" = "GDP deflator")

source_adapted <- source %>%
dplyr::left_join(source_USA, by = dplyr::join_by("year")) %>%
dplyr::mutate("GDP deflator" = .data$USA_deflator)

if (!is.null(replace_NAs) && replace_NAs[1] == "with_USA") {
source_USA <- source %>% dplyr::filter(.data$iso3c == "USA")
source_adapted <- purrr::map(unique(gdp$iso3c), ~dplyr::mutate(source_USA, "iso3c" = .x)) %>%
purrr::list_rbind()
}
source_adapted
}

#
adapt_source <- function(gdp, source, with_regions, replace_NAs) {
rlang::check_installed(c("zoo"), reason = "in order for 'replace_NAs' to work.")
Expand Down
38 changes: 27 additions & 11 deletions R/check_user_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
#
# check_user_input performs some checks on the function arguments.
# @return TRUE or an Error
check_user_input <- function(gdp, unit_in, unit_out, source, with_regions, replace_NAs, verbose, return_cfs) {
check_user_input <- function(gdp,
unit_in,
unit_out,
source,
use_USA_deflator_for_all,
with_regions,
replace_NAs,
verbose,
return_cfs) {

# Check the gdp argument
check_gdp(gdp)
# Check the unit_in and unit_out arguments
check_unit_in_out(unit_in, unit_out)
# Check the source argument
source <- check_source(source)
# Check the with_regions argument. Depends on unit_in, unit_out, and the source tibble
check_use_USA_deflator_for_all(use_USA_deflator_for_all, unit_in, unit_out)
check_with_regions(unit_in, unit_out, source, with_regions)
# Check the replaceNAs argument. Depends on the with_regions argument.
check_replace_NAs(with_regions, replace_NAs)
# Check the verbose argument
check_verbose(verbose)
# Check the return_cfs argument
check_return_cfs(return_cfs)

TRUE
Expand Down Expand Up @@ -59,6 +61,7 @@ check_unit_in_out <- function(unit_in, unit_out) {
"current Int\\$PPP",
"constant .... LCU",
"constant .... US\\$MER",
"constant .... \u20ac",
"constant .... Int\\$PPP"
)
if (!is.character(unit_in) || !any(sapply(valid_units, grepl, unit_in))) {
Expand Down Expand Up @@ -95,9 +98,22 @@ check_source <- function(source) {
if (!all(required_cols_in_source %in% colnames(source))) {
abort("Invalid 'source' argument. Required columns are: {paste(required_cols_in_source, collapse = '; ')}")
}
if (nrow(dplyr::distinct(source)) != nrow(dplyr::distinct(source, .data$iso3c , .data$year))) {
abort("Invalid 'source' argument. Duplicate iso3c - year pairs found.")
}
source
}

# Check input parameter 'verbose'
check_use_USA_deflator_for_all <- function(use_USA_deflator_for_all, unit_in, unit_out) {
if (!is.logical(use_USA_deflator_for_all)) {
abort("Invalid 'use_USA_deflator_for_all' argument. Has to be either TRUE or FALSE.")
}
if (use_USA_deflator_for_all && any(grepl("current", c(unit_in, unit_out)))) {
abort("Setting 'use_USA_deflator_for_all' to TRUE should only be applied between conversion of constant units.")
}
}

# Check input parameter 'with_regions'
check_with_regions <- function(unit_in, unit_out, source, with_regions) {
if (!is.null(with_regions)) {
Expand Down Expand Up @@ -129,9 +145,9 @@ check_replace_NAs <- function(with_regions, replace_NAs) {
"convertGDP(replace_NAs = '\"linear_regional_average\" has been replaced by c(\"linear\", \"regional_average\")')"
)
}
if (!all(replace_NAs %in% c(NA, 0, 1, "no_conversion", "linear", "regional_average"))) {
abort("Invalid 'replace_NAs' argument. Has to be either NULL, NA, 0, 1, no_conversion, linear, regional_average or \\
a combination of the above.")
if (!all(replace_NAs %in% c(NA, 0, 1, "no_conversion", "linear", "regional_average", "with_USA"))) {
abort("Invalid 'replace_NAs' argument. Has to be either NULL, NA, 0, 1, no_conversion, linear, \\
regional_average, with_USA or a combination of the above.")
}
if (length(replace_NAs) > 1 && replace_NAs[1] != "linear") {
abort("Invalid 'replace_NAs' argument. The only accepted combinations of arguments start with 'linear', e.g. \\
Expand Down
Loading

0 comments on commit ec57bc2

Please sign in to comment.