Skip to content
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ playground.R
figure/
docs
inst/doc
data-raw/wdi_downloads/
5 changes: 1 addition & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 (>= 3.5)
Imports:
cli (>= 2.4.0),
crayon,
Expand All @@ -26,17 +26,14 @@ Imports:
tidyselect,
withr
Suggests:
countrycode,
covr,
knitr,
magclass,
madrat (>= 3.6.4),
purrr,
rmarkdown,
stringr,
testthat (>= 3.0.0),
usethis,
WDI,
zoo
VignetteBuilder:
knitr
Expand Down
51 changes: 20 additions & 31 deletions R/convertGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' conversion factors (PPPs).
#'
#' @details
#' # Using a custom source
#' When providing a custom source to the function, a certain format is required.
#' The source object must be a data frame or tibble with at least the following columns:
#' \itemize{
Expand All @@ -17,43 +18,31 @@
#' The base year of the deflator can be any year, and can be country-specific.
#' \item a numeric column named "MER (LCU per US$)" with MER values,
#' \item a numeric column named "PPP conversion factor, GDP (LCU per international $)"
#' wit PPP exchange rate values.
#' with PPP exchange rate values.
#' }
#'
#' @param gdp A tibble, data frame or magpie object, the latter of which
#' requires the [magclass](https://github.com/pik-piam/magclass)
#' package to be installed. The data-frame needs to have at least 2 columns, in some cases 3:
#' @param gdp A tibble, data frame or magpie (see [magclass](https://github.com/pik-piam/magclass)) object.
#' The data-frame needs to have at least 2 columns, in some cases 3:
#' \itemize{
#' \item a character column with iso3c
#' ([wikipedia](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3)) country codes,
#' \item a numeric column with years (only required when converting from or to current currencies),
#' \item a numeric column with years (only required when converting from or to "current" currencies),
#' \item a numeric column named "value" with GDP values.
#' }
#' @param unit_in A string with the incoming GDP unit, one of:
#' \itemize{
#' \item "current LCU"
#' \item "current Int$PPP"
#' \item "current US$MER"
#' \item "constant YYYY LCU"
#' \item "constant YYYY Int$PPP"
#' \item "constant YYYY US$MER"
#' \item "constant YYYY €" or "constant YYYY EUR"
#' \item "constant YYYY xxx_CU"
#' }
#' where YYYY should be replaced with a year e.g. "2010" or "2017".
#' @param unit_out A string with the outgoing GDP unit, one of:
#' \itemize{
#' \item "current LCU"
#' \item "current Int$PPP"
#' \item "current US$MER"
#' \item "constant YYYY LCU"
#' \item "constant YYYY Int$PPP"
#' \item "constant YYYY US$MER"
#' \item "constant YYYY €" or "constant YYYY EUR"
#' \item "constant YYYY xxx_CU"
#' }
#' where YYYY should be replaced with a year e.g. "2010" or "2017", and xxx with a valid iso3c country code,
#' e.g. "JPN_CU" to pick the currency unit of Japan.
#' @param unit_in,unit_out A string with the units to convert from and to. The following units are available:
#'
#' * "current LCU"
#' * "current Int$PPP"
#' * "current US$MER"
#' * "current xxx_CU"
#' * "constant yyyy LCU"
#' * "constant yyyy Int$PPP"
#' * "constant yyyy US$MER"
#' * "constant yyyy €" or "constant yyyy EUR"
#' * "constant yyyy xxx_CU"
#'
#' Here "LCU" stands for Local Currency Unit, "yyyy" is a placeholder for a year, e.g. "2010" or "2015", and xxx is a
#' placeholder for a valid iso3c country code with for instance "JPN_CU" designating the currency unit of Japan
#'
#' @param source A string referring to a package internal data frame containing the conversion factors, or
#' a data-frame that exists in the calling environment.
Expand Down Expand Up @@ -124,7 +113,7 @@ convertGDP <- function(gdp,
# 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 2025-11-19"
"last changes 2026-07-15"

# Save all function arguments as list
arg <- as.list(environment())
Expand Down
6 changes: 3 additions & 3 deletions R/print_source_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ print_source_info <- function(source) {
if (missing(source) || source == "wb_wdi") {
cli_source_info(name = "wb_wdi",
origin = "The World Bank's World Development Indicator Database",
date = "Downloaded on the 30th of April 2024",
date = "Downloaded on the 15th of July 2026",
html = "https://databank.worldbank.org/source/world-development-indicators",
note = "Uses the GDP deflator.")
}
if (missing(source) || source == "wb_wdi_linked") {
cli_source_info(name = "wb_wdi_linked",
origin = "The World Bank's World Development Indicator Database",
date = "Downloaded on the 30th of April 2024",
date = "Downloaded on the 15th of July 2026",
html = "https://databank.worldbank.org/source/world-development-indicators",
note = "Uses the linked GDP deflator.")
}
if (missing(source) || source == "wb_wdi_cpi") {
cli_source_info(name = "wb_wdi_cpi",
origin = "The World Bank's World Development Indicator Database",
date = "Downloaded on the 30th of April 2024",
date = "Downloaded on the 15th of July 2026",
html = "https://databank.worldbank.org/source/world-development-indicators",
note = "Uses the CPI as deflator.")
}
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
14 changes: 9 additions & 5 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ The `unit_in` and `unit_out` arguments specify the incoming and outgoing GDP uni
- current LCU
- current US$MER
- current Int$PPP
- constant YYYY LCU
- constant YYYY US$MER
- constant YYYY Int$PPP

Here "YYYY" is a placeholder for a year, e.g. "2010" or "2015", and "LCU" stands for Local Currency Unit.
- current xxx_CU
- constant yyyy LCU
- constant yyyy US$MER
- constant yyyy Int$PPP
- constant yyyy € or constant yyyy EUR
- constant yyyy xxx_CU

Here "LCU" stands for Local Currency Unit, "YYYY" is a placeholder for a year, e.g. "2010" or "2015", and xxx is a
placeholder for a valid iso3c country code with for instance "JPN_CU" designating the currency unit of Japan.

For a quick conversion of a single value use `convertSingle`.

Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ GDP units. All common GDP units are supported, i.e.:
- current LCU
- current US\$MER
- current Int\$PPP
- constant YYYY LCU
- constant YYYY US\$MER
- constant YYYY Int\$PPP

Here “YYYY” is a placeholder for a year, e.g. “2010” or “2015”, and
“LCU” stands for Local Currency Unit.
- current xxx_CU
- constant yyyy LCU
- constant yyyy US\$MER
- constant yyyy Int\$PPP
- constant yyyy € or constant yyyy EUR
- constant yyyy xxx_CU

Here “LCU” stands for Local Currency Unit, “YYYY” is a placeholder for a
year, e.g. “2010” or “2015”, and xxx is a placeholder for a valid iso3c
country code with for instance “JPN_CU” designating the currency unit of
Japan.

For a quick conversion of a single value use `convertSingle`.

Expand Down
151 changes: 34 additions & 117 deletions data-raw/create_internal_sources.R
Original file line number Diff line number Diff line change
@@ -1,133 +1,50 @@
# Download data from WDI and save as internal package data
# !! Don't forget to update the "date" section in print_source_info
library(magrittr)
rlang::check_installed(c("countrycode", "WDI", "stringr", "usethis"), reason = "in order to access the WDI database.")

my_vars <- c(
"GDP (constant LCU)",
"GDP (current LCU)",
"GDP: linked series (current LCU)",
"GDP (constant 2015 US$)",
"GDP (current US$)",
"GDP, PPP (constant 2017 international $)",
"PPP conversion factor, GDP (LCU per international $)",
"GDP, PPP (current international $)",
"Population, total",
"GDP deflator (base year varies by country)",
"GDP deflator: linked series (base year varies by country)",
"DEC alternative conversion factor (LCU per US$)",
"Consumer price index (2010 = 100)"
)

my_info <- WDI::WDIsearch(
paste0("^",
paste0(stringr::str_replace_all(my_vars, c("\\$" = "\\\\$",
"\\(" = "\\\\(",
"\\)" = "\\\\)",
"\\%" = "\\\\%")),
collapse = "$|^"),
"$"),
field = "name",
short = FALSE
) %>%
tibble::as_tibble()

# Get codes of only countries (excluding regional aggregates)
wb_country_codes <- WDI::WDIcache()$country %>%
dplyr::pull(iso3c) %>%
countrycode::countrycode("wb", "country.name") %>%
`[`(!is.na(.)) %>%
countrycode::countrycode("country.name", "wb")


# Download data, remove aggregates and do some pivoting and renaming
my_data <- purrr::map2(my_info$indicator,
my_info$name,
~ WDI::WDI(indicator = .x, extra = TRUE) %>%
tibble::as_tibble() %>%
dplyr::filter(iso3c %in% wb_country_codes) %>%
#dplyr::filter(!is.na(region) & region != "Aggregates") %>%
dplyr::arrange(iso3c, year) %>%
dplyr::select(iso3c, year, tidyselect::contains(.x)) %>%
tidyr::pivot_longer(cols = tidyselect::contains(.x), names_to = "id") %>%
dplyr::mutate(name = .y) %>%
dplyr::select(iso3c, year, id, name, value)) %>%
purrr::list_rbind()
rlang::check_installed(c("usethis"), reason = "to create the R/sysdata.rda file.")

# my_vars <- c(
# "GDP (constant LCU)",
# "GDP (current LCU)",
# "GDP: linked series (current LCU)",
# "GDP (constant 2015 US$)",
# "GDP (current US$)",
# "GDP, PPP (constant 2021 international $)",
# "PPP conversion factor, GDP (LCU per international $)",
# "GDP, PPP (current international $)",
# "Population, total",
# "GDP deflator (base year varies by country)",
# "GDP deflator: linked series (base year varies by country)",
# "DEC alternative conversion factor (LCU per US$)",
# "Consumer price index (2010 = 100)"
# )

my_data <- readr::read_csv("~/Downloads/wdi_GDPuc_15Jul2026.csv", na = "..", show_col_types = FALSE) %>%
tidyr::pivot_longer(tidyselect::starts_with(c("19", "20")),
names_to = "year",
names_transform = ~as.integer(substr(.x, 1, 4))) %>%
dplyr::select("iso3c" = "Country Code", "year", "name" = "Series Name", "value") %>%
dplyr::arrange(.data$iso3c, .data$name, .data$year)

wb_wdi <- my_data %>%
dplyr::select(-id) %>%
tidyr::pivot_wider(names_from = name) %>%
dplyr::mutate(`GDP deflator: linked series` = `GDP deflator: linked series (base year varies by country)` / 100,
`GDP deflator` = `GDP deflator (base year varies by country)` / 100,
`CPI` = `Consumer price index (2010 = 100)` / 100,
`MER (LCU per US$)` = `DEC alternative conversion factor (LCU per US$)`)

wb_wdi_linked <- wb_wdi %>%
dplyr::select(iso3c,
year,
`GDP deflator` = `GDP deflator: linked series`,
`PPP conversion factor, GDP (LCU per international $)`,
`MER (LCU per US$)`)
dplyr::select("iso3c",
"year",
"GDP deflator" = "GDP deflator: linked series",
"PPP conversion factor, GDP (LCU per international $)",
"MER (LCU per US$)")

wb_wdi_cpi <- wb_wdi %>%
dplyr::select(iso3c,
year,
`GDP deflator` = `CPI`,
`PPP conversion factor, GDP (LCU per international $)`,
`MER (LCU per US$)`)
dplyr::select("iso3c",
"year",
"GDP deflator" = "CPI",
"PPP conversion factor, GDP (LCU per international $)",
"MER (LCU per US$)")

# For now, IMF is removed due to copyright issues
#usethis::use_data(imf_weo, wb_wdi, wb_wdi_linked, internal = TRUE, overwrite = TRUE)
usethis::use_data(wb_wdi, wb_wdi_linked, wb_wdi_cpi, internal = TRUE, overwrite = TRUE)




#####################################################################
#####################################################################
#####################################################################
#
##### IMF DATA
#
# Path to the IMF WEO report from April 2021, downloaded as .xls file
# imf_2021_file <- "../../R_projects/gdp_pop_trends/data/source/IMF/WEOApr2021all.xls"
#
# imf_weo <- readr::read_tsv(imf_2021_file) %>%
# dplyr::rename("iso3c" = ISO) %>%
# dplyr::filter(`WEO Subject Code` %in% c(
# "NGDP_R",
# "NGDP",
# "NGDPD",
# "PPPGDP",
# "NGDPRPPPPC",
# "NGDP_D",
# "PPPEX",
# "LP"
# )) %>%
# dplyr::select(iso3c, `WEO Subject Code`, tidyselect::starts_with(c("1", "2"))) %>%
# dplyr::mutate(dplyr::across(.cols = tidyselect::starts_with(c("1", "2")),
# ~ stringr::str_remove_all(.x, ",") %>%
# as.double())) %>%
# tidyr::pivot_longer(tidyselect::starts_with(c("1", "2")),
# names_to = "year") %>%
# dplyr::mutate(year = as.double(year)) %>%
# tidyr::pivot_wider(names_from = `WEO Subject Code`) %>%
# dplyr::rename(
# "GDP (constant LCU)" = NGDP_R,
# "GDP (current LCU)" = NGDP,
# "GDP (current US$)" = NGDPD,
# "GDP, PPP (current international $)" = PPPGDP,
# "GDPpc, PPP (constant 2017 international $)" = NGDPRPPPPC,
# "GDP deflator" = NGDP_D,
# "PPP conversion factor, GDP (LCU per international $)" = PPPEX,
# "Population, total" = LP
# ) %>%
# dplyr::mutate(
# `GDP (constant LCU)` = `GDP (constant LCU)` * 1e+9,
# `GDP (current LCU)` = `GDP (current LCU)` * 1e+9,
# `GDP (current US$)` = `GDP (current US$)` * 1e+9,
# `GDP deflator` = `GDP deflator` / 100,
# `GDP, PPP (current international $)` = `GDP, PPP (current international $)` * 1e+9,
# `MER (LCU per US$)` = `GDP (current LCU)` / `GDP (current US$)`,
# `Population, total` = `Population, total` * 1e+6
# )
Loading
Loading