## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = FALSE)
library(widr)

## ----download-sig-------------------------------------------------------------
# download_wid(
#   indicators             = "all",
#   areas                  = "all",
#   years                  = "all",
#   perc                   = "all",
#   ages                   = "992",   # default: adults 20+
#   pop                    = "j",     # default: equal-split
#   metadata               = FALSE,
#   include_extrapolations = TRUE,
#   verbose                = FALSE,
#   cache                  = TRUE)

## ----decode-encode------------------------------------------------------------
# wid_decode("sptinc992j")
# #> $series_type  "s"     $concept  "ptinc"     $age  "992"     $pop  "j"
# 
# wid_encode("s", "ptinc", "992", "j")       #> [1] "sptinc992j"
# wid_encode("m", "nninc")                   #> [1] "mnninc"
# wid_encode(wid_decode("sptinc992j"))       # round-trip: identical to input
# 
# # Validate (throws on failure) or check silently
# wid_validate(series_type = "s", concept = "ptinc", age = 992, pop = "j")
# wid_is_valid(series_type = "s", concept = "ptinc")   #> [1] TRUE
# wid_is_valid(series_type = "Z")                       #> [1] FALSE

## ----series-types-------------------------------------------------------------
# wid_series_types
# wid_search("share", tables = "series_types")

## ----concepts-----------------------------------------------------------------
# nrow(wid_concepts)
# head(wid_concepts)
# wid_search("wealth")
# wid_search("^ptinc$", tables = "concepts")
# wid_search("wealth", type = "s")
# wid_search("income", tables = "all")

## ----ages---------------------------------------------------------------------
# wid_ages
# wid_validate(age = 992)

## ----pop-types----------------------------------------------------------------
# wid_pop_types

## ----is-valid-----------------------------------------------------------------
# wid_is_valid(series_type = "s", concept = "ptinc")   # TRUE
# wid_is_valid(series_type = "b", pop = "f")            # FALSE

## ----countries----------------------------------------------------------------
# head(wid_countries)
# wid_search("^US", tables = "countries")
# wid_validate(areas = c("US", "FR", "US-CA"))

## ----percentiles--------------------------------------------------------------
# head(wid_percentiles)
# wid_search("top 1", tables = "percentiles")
# wid_validate(perc = "p99p100")
# wid_validate(perc = "p90p10")    # error: invalid order

## ----nni----------------------------------------------------------------------
# download_wid(indicators = "anninc992i", areas = "FR", years = 1900:2023)

## ----dist-income--------------------------------------------------------------
# # Top 1% pretax income share
# download_wid(
#   indicators = "sptinc992j",
#   areas      = c("US", "FR", "CN", "ZA"),
#   perc       = "p99p100",
#   years      = 1990:2023
# )
# 
# # Gini from full distribution
# dist <- download_wid("sptinc992j", areas = "US", perc = "all", years = 2022)
# wid_gini(dist)
# wid_top_share(dist, top = 0.01)
# 
# # Percentile ratio from threshold series
# thresh <- download_wid("tptinc992j", areas = "US", perc = "all", years = 2022)
# wid_percentile_ratio(thresh)
# 
# # Plot Lorenz curve
# wid_plot_lorenz(dist)

## ----fiscal-------------------------------------------------------------------
# download_wid(
#   indicators = "sfiinc992j",
#   areas      = "US",
#   perc       = "p99p100",
#   years      = 1913:2023
# )

## ----wealth-national----------------------------------------------------------
# download_wid(indicators = "mnweal999i", areas = "FR", years = 1970:2023)

## ----wealth-dist--------------------------------------------------------------
# download_wid(
#   indicators = "shweal992j",
#   areas      = "GB",
#   perc       = c("p90p100", "p99p100"),
#   years      = 1995:2023)
# 
# wid_plot_lorenz(download_wid("shweal992j", areas = "US", perc = "all", years = 2019))

## ----price--------------------------------------------------------------------
# download_wid(indicators = "inyixx999i", areas = "US", years = 1950:2023)

## ----fx-----------------------------------------------------------------------
# # Retrieve PPP exchange rate directly
# download_wid(indicators = "xlcusp999i", areas = "FR", years = 2023)
# 
# # Convert a monetary series to 2022 USD PPP in one step
# download_wid("aptinc992j", areas = c("US", "FR", "CN", "IN"), perc = "p0p50") |>
#   wid_convert(target = "ppp", base_year = "2022")

## ----pop----------------------------------------------------------------------
# download_wid(indicators = "npopul999i", areas = "US", years = 1800:2023)

## ----carbon-agg---------------------------------------------------------------
# download_wid(indicators = "entghg999i", areas = "US", years = 1990:2023)
# download_wid(indicators = "enfghg999i", areas = "US", years = 1990:2023)

## ----carbon-dist--------------------------------------------------------------
# # WID canonical example: top 10% average per capita footprint, Switzerland 2014
# download_wid(
#   indicators = "lpfghg999i",
#   areas      = "CH",
#   perc       = "p90p100",
#   years      = 2014)
# 
# # Bottom 50% share of emissions
# download_wid(
#   indicators = "spfghg999i",
#   areas      = c("US", "FR", "IN"),
#   perc       = "p0p50",
#   years      = 2019)

