| Title: | Mongolian 'NSO' 'PXWeb' Data and Boundaries (Tidy Client) |
|---|---|
| Description: | A 'tidyverse'-friendly client for the National Statistics Office of Mongolia 'PXWeb' API <https://data.1212.mn/> with helpers to discover tables, variables, and fetch statistical data. Also includes utilities to retrieve Mongolia administrative boundaries (ADM0-ADM2) as 'sf' objects from open sources for mapping and spatial analysis. |
| Authors: | Temuulen Enebish [aut, cre, cph] |
| Maintainer: | Temuulen Enebish <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.2 |
| Built: | 2026-05-27 10:47:40 UTC |
| Source: | https://github.com/temuulene/mongolstats |
Convert a query to a PXWeb body
as_px_query(x, lang = .px_lang())as_px_query(x, lang = .px_lang())
x |
An |
lang |
PX language: "en" or "mn" (defaults to current option). |
A list suitable to send as JSON body to PXWeb.
q <- nso_query("DT_NSO_0300_001V2", list(Year = "2023")) body <- as_px_query(q)q <- nso_query("DT_NSO_0300_001V2", list(Year = "2023")) body <- as_px_query(q)
Downloads Mongolia boundaries for ADM0/ADM1/ADM2 from the GeoBoundaries API
and returns an sf object. Results can be cached by the caller as needed.
mn_boundaries(level = c("ADM0", "ADM1", "ADM2"))mn_boundaries(level = c("ADM0", "ADM1", "ADM2"))
level |
One of "ADM0", "ADM1", "ADM2". |
An sf object with polygons for the requested level.
# Get aimag (province) boundaries aimags <- mn_boundaries("ADM1") head(aimags)# Get aimag (province) boundaries aimags <- mn_boundaries("ADM1") head(aimags)
Adds a name_std column to an sf boundary object by transliterating,
lowercasing, and stripping special characters from place names. This
enables reliable joins between NSO data and administrative boundary polygons.
mn_boundaries_normalize(g, name_col = "shapeName")mn_boundaries_normalize(g, name_col = "shapeName")
g |
sf object from |
name_col |
Column with English names (default 'shapeName'). |
An sf object with an additional name_std column.
aimags <- mn_boundaries("ADM1") aimags <- mn_boundaries_normalize(aimags) head(aimags$name_std)aimags <- mn_boundaries("ADM1") aimags <- mn_boundaries_normalize(aimags) head(aimags$name_std)
Returns a lightweight tibble of key columns from the GeoBoundaries data, including normalized names, without the full geometry. Useful for building custom crosswalks between NSO data and boundary identifiers.
mn_boundary_keys(level = "ADM1")mn_boundary_keys(level = "ADM1")
level |
Boundary level. |
A tibble with key columns from GeoBoundaries and normalized names.
keys <- mn_boundary_keys("ADM1") head(keys)keys <- mn_boundary_keys("ADM1") head(keys)
Performs a fuzzy string-distance join between a data frame and boundary polygons. Useful when place-name spellings differ slightly between datasets (e.g., "Ulanbatar" vs "Ulaanbaatar").
mn_fuzzy_join_by_name( data, name_col, level = "ADM1", boundaries = NULL, max_distance = 2, method = c("osa", "lv", "jw", "dl") )mn_fuzzy_join_by_name( data, name_col, level = "ADM1", boundaries = NULL, max_distance = 2, method = c("osa", "lv", "jw", "dl") )
data |
Data frame with a name column. |
name_col |
Column in |
level |
Boundary level. |
boundaries |
Optional pre-fetched boundaries. |
max_distance |
Maximum string distance for a match (default 2). |
method |
Distance method passed to |
sf with best fuzzy matches joined.
# Join even with minor spelling differences pop_data <- data.frame(aimag = c("Ulanbatar", "Darhan"), pop = c(1500000, 100000)) sf_joined <- mn_fuzzy_join_by_name(pop_data, "aimag", level = "ADM1")# Join even with minor spelling differences pop_data <- data.frame(aimag = c("Ulanbatar", "Darhan"), pop = c(1500000, 100000)) sf_joined <- mn_fuzzy_join_by_name(pop_data, "aimag", level = "ADM1")
Performs an exact join between a data frame and boundary polygons using normalized place names. Both sides are normalized via transliteration and lowercasing before joining.
mn_join_by_name(data, name_col, level = "ADM1", boundaries = NULL)mn_join_by_name(data, name_col, level = "ADM1", boundaries = NULL)
data |
Data frame with a name column. |
name_col |
Column in |
level |
Boundary level, passed to |
boundaries |
Optional pre-fetched boundaries. |
An sf object with joined data.
pop_data <- data.frame(aimag = c("Ulaanbaatar", "Darkhan-Uul"), pop = c(1500000, 100000)) sf_joined <- mn_join_by_name(pop_data, "aimag", level = "ADM1")pop_data <- data.frame(aimag = c("Ulaanbaatar", "Darkhan-Uul"), pop = c(1500000, 100000)) sf_joined <- mn_join_by_name(pop_data, "aimag", level = "ADM1")
Clear cached entries
nso_cache_clear()nso_cache_clear()
No return value, called for side effects.
nso_cache_clear()nso_cache_clear()
Disable caching
nso_cache_disable()nso_cache_disable()
No return value, called for side effects.
nso_cache_disable()nso_cache_disable()
Caches table lists and codebooks on disk to speed up repeated calls. Optionally set a time-to-live (TTL) for cache entries.
nso_cache_enable(dir = NULL, ttl = NULL)nso_cache_enable(dir = NULL, ttl = NULL)
dir |
Directory for cache; defaults to user cache dir. |
ttl |
Optional TTL in seconds for cached entries (applies to the
disk cache). If |
Cache directory path (invisibly).
# Enable caching in a temporary directory (for demo purposes) cache_dir <- nso_cache_enable(dir = tempdir()) # Check status nso_cache_status() # Disable when done nso_cache_disable()# Enable caching in a temporary directory (for demo purposes) cache_dir <- nso_cache_enable(dir = tempdir()) # Check status nso_cache_status() # Disable when done nso_cache_disable()
Report current cache configuration and basic stats.
nso_cache_status()nso_cache_status()
A list with enabled, dir, and has_cache.
nso_cache_status()nso_cache_status()
Fetch statistical data for a table (PXWeb)
nso_data( tbl_id, selections, labels = c("none", "en", "mn", "both"), value_name = getOption("mongolstats.value_name", "value"), include_raw = getOption("mongolstats.attach_raw", FALSE) )nso_data( tbl_id, selections, labels = c("none", "en", "mn", "both"), value_name = getOption("mongolstats.value_name", "value"), include_raw = getOption("mongolstats.attach_raw", FALSE) )
tbl_id |
Table identifier (e.g., "DT_NSO_0300_001V2"). |
selections |
Named list mapping variable labels (e.g., Year, Sex) to desired codes or labels. |
labels |
Label handling: "none" (codes only), "en", "mn", or "both". |
value_name |
Name of the numeric value column in the result (default: "value"). |
include_raw |
If TRUE, attach the raw PX payload as attribute |
A tibble with one column per dimension and a numeric value column.
Some NSO tables have unique dimension structures that affect how selections
should be constructed:
Air Quality Monthly Tables (e.g., DT_NSO_2400_015V1 to V6):
These tables do not have a Year dimension. Instead, they use a running
Month dimension with integer codes (e.g., "0" for the most recent month).
Example: selections = list(Month = as.character(0:11)) retrieves the last 12 months.
# Fetch population data pop <- nso_data( tbl_id = "DT_NSO_0300_001V2", selections = list(Year = "2023") ) head(pop)# Fetch population data pop <- nso_data( tbl_id = "DT_NSO_0300_001V2", selections = list(Year = "2023") ) head(pop)
Returns codes and optional labels for a specific dimension.
nso_dim_values(tbl_id, dim, labels = c("code", "en", "mn", "both"))nso_dim_values(tbl_id, dim, labels = c("code", "en", "mn", "both"))
tbl_id |
Table identifier (e.g., "DT_NSO_0300_001V2"). |
dim |
Dimension name or code (case-insensitive; exact match preferred). |
labels |
One of "code", "en", "mn", or "both" to control returned label columns. |
A tibble with at least code; may include label_en and/or label_mn.
values <- nso_dim_values("DT_NSO_0300_001V2", "Year") head(values)values <- nso_dim_values("DT_NSO_0300_001V2", "Year") head(values)
Returns one row per dimension with basic metadata.
nso_dims(tbl_id)nso_dims(tbl_id)
tbl_id |
Table identifier (e.g., "DT_NSO_0300_001V2"). |
A tibble with columns: dim (display name), code (dimension code),
is_time (logical), and n_values (number of values for the dimension).
dims <- nso_dims("DT_NSO_0300_001V2") dimsdims <- nso_dims("DT_NSO_0300_001V2") dims
Executes an nso_query and returns a tidy tibble with one column per
dimension and a numeric value column. Use labels to add _en/_mn
columns for each dimension.
nso_fetch( x, labels = c("code", "en", "mn", "both"), value_name = getOption("mongolstats.value_name", "value"), include_raw = getOption("mongolstats.attach_raw", FALSE) )nso_fetch( x, labels = c("code", "en", "mn", "both"), value_name = getOption("mongolstats.value_name", "value"), include_raw = getOption("mongolstats.attach_raw", FALSE) )
x |
An |
labels |
One of "code", "en", "mn", or "both" (mapped to internal API). |
value_name |
Name of the numeric value column in the result (default: "value"). |
include_raw |
If TRUE, attach the raw PX payload as attribute |
A tibble.
q <- nso_query("DT_NSO_0300_001V2", list(Year = "2023")) data <- nso_fetch(q) head(data)q <- nso_query("DT_NSO_0300_001V2", list(Year = "2023")) data <- nso_fetch(q) head(data)
Retrieves the full catalogue of available statistical tables from the
National Statistics Office PXWeb API. Uses the embedded index by default
for fast startup; call nso_rebuild_px_index() to refresh from the API.
nso_itms() nso_tables()nso_itms() nso_tables()
A tibble with columns: px_path, px_file, tbl_id, tbl_eng_nm,
tbl_nm, strt_prd, end_prd, list_id.
# List all available tables tables <- nso_itms() head(tables)# List all available tables tables <- nso_itms() head(tables)
Filters the table catalogue to only those belonging to a given sector
or sub-sector path, as returned by nso_sectors() or nso_subsectors().
nso_itms_by_sector(list_id)nso_itms_by_sector(list_id)
list_id |
Path string from |
A tibble of tables matching the specified sector path.
sectors <- nso_sectors() tables <- nso_itms_by_sector(sectors$id[1])sectors <- nso_sectors() tables <- nso_itms_by_sector(sectors$id[1])
Returns detailed variable metadata for a single table, including field names, item IDs, and labels in English and Mongolian when available.
nso_itms_detail(tbl_id) nso_variables(tbl_id)nso_itms_detail(tbl_id) nso_variables(tbl_id)
tbl_id |
Table identifier (e.g., "DT_NSO_0300_001V2"). |
A tibble with variable metadata.
vars <- nso_itms_detail("DT_NSO_0300_001V2") varsvars <- nso_itms_detail("DT_NSO_0300_001V2") vars
Performs a case-insensitive keyword search across the table catalogue,
matching query against table names in English and/or Mongolian.
nso_itms_search(query, fields = c("tbl_eng_nm", "tbl_nm"))nso_itms_search(query, fields = c("tbl_eng_nm", "tbl_nm"))
query |
A single keyword string to search for (case-insensitive). |
fields |
Character vector of column names to search within (defaults to English and Mongolian titles). |
A tibble of matching tables.
# Search for population tables nso_itms_search("population")# Search for population tables nso_itms_search("population")
Disable offline mode
nso_offline_disable()nso_offline_disable()
Invisibly, TRUE.
nso_offline_disable()nso_offline_disable()
When enabled, HTTP requests are prevented and functions that require
the network will raise a clear offline error. Cached metadata can still
be used if already available via nso_cache_enable().
nso_offline_enable()nso_offline_enable()
Invisibly, TRUE.
# Enable offline mode nso_offline_enable() # Check the option was set getOption("mongolstats.offline") # Disable to restore normal operation nso_offline_disable()# Enable offline mode nso_offline_enable() # Check the option was set getOption("mongolstats.offline") # Disable to restore normal operation nso_offline_disable()
Convenience wrapper around base::options() for mongolstats.
nso_options(...)nso_options(...)
... |
Named options to set. If empty, returns a named list of current mongolstats options. |
Invisibly, the previous values of the options changed, or a list of current values when called with no arguments.
# Get all current mongolstats options nso_options() # Set an option (save old value for restoration) old <- nso_options(mongolstats.default_labels = "en") # Restore original value options(old)# Get all current mongolstats options nso_options() # Set an option (save old value for restoration) old <- nso_options(mongolstats.default_labels = "en") # Restore original value options(old)
Fetch multiple tables and bind (PXWeb)
nso_package( requests, labels = c("none", "en", "mn", "both"), parallel = getOption("mongolstats.parallel", FALSE), value_name = getOption("mongolstats.value_name", "value") )nso_package( requests, labels = c("none", "en", "mn", "both"), parallel = getOption("mongolstats.parallel", FALSE), value_name = getOption("mongolstats.value_name", "value") )
requests |
A list of records, each with |
labels |
Label handling as in |
parallel |
If TRUE, use future.apply to fetch tables in parallel. |
value_name |
Name of the numeric value column in the result (default: "value"). |
A tibble combining data from all requested tables, with a tbl_id column
identifying the source table.
reqs <- list( list(tbl_id = "DT_NSO_0300_001V2", selections = list(Year = "2023")) ) combined <- nso_package(reqs)reqs <- list( list(tbl_id = "DT_NSO_0300_001V2", selections = list(Year = "2023")) ) combined <- nso_package(reqs)
Utilities to construct NSO period codes and sequences. For monthly data, use YYYYMM; for yearly, use YYYY.
nso_period_seq(start, end, by = c("Y", "M"))nso_period_seq(start, end, by = c("Y", "M"))
start, end
|
Start and end periods as character (YYYY or YYYYMM). |
by |
'Y' for yearly or 'M' for monthly. |
Character vector of period codes.
# Generate yearly sequence nso_period_seq("2020", "2024", by = "Y") # Generate monthly sequence nso_period_seq("202401", "202406", by = "M")# Generate yearly sequence nso_period_seq("2020", "2024", by = "Y") # Generate monthly sequence nso_period_seq("202401", "202406", by = "M")
Builds a lightweight query object that records a table id and selections.
Use nso_fetch() to execute it, or as_px_query() to inspect the
underlying PXWeb body.
nso_query(tbl_id, selections = list())nso_query(tbl_id, selections = list())
tbl_id |
Table identifier, e.g. "DT_NSO_0300_001V2". |
selections |
Named list mapping dimension labels (e.g., Year, Sex) to desired codes or labels. |
An object of class nso_query.
# Create a query object (does not require network) q <- nso_query("DT_NSO_0300_001V2", list(Year = "2023", Sex = "Total")) print(q)# Create a query object (does not require network) q <- nso_query("DT_NSO_0300_001V2", list(Year = "2023", Sex = "Total")) print(q)
Crawls the PXWeb API to rebuild the table index. If path is provided,
the index is written to that file; otherwise only the in-memory index is
refreshed.
nso_rebuild_px_index(path = NULL, write = !is.null(path))nso_rebuild_px_index(path = NULL, write = !is.null(path))
path |
Output path for JSON. If |
write |
Whether to write JSON to |
A tibble containing the rebuilt table index.
# Rebuild in-memory index only (takes time to crawl API) idx <- nso_rebuild_px_index() head(idx)# Rebuild in-memory index only (takes time to crawl API) idx <- nso_rebuild_px_index() head(idx)
Performs a case-insensitive regex search across the table catalogue, optionally filtered to a specific sector. Searches table names in English and/or Mongolian by default.
nso_search(query, sector = NULL, fields = c("tbl_eng_nm", "tbl_nm"))nso_search(query, sector = NULL, fields = c("tbl_eng_nm", "tbl_nm"))
query |
Search string (regex, case-insensitive). |
sector |
Optional sector/subsector |
fields |
Character vector of fields to search within. |
Tibble of matching tables.
nso_search("population")nso_search("population")
Queries the PXWeb API root to return the top-level statistical sectors
(e.g., Population, Economy, Environment). Use the returned id column
with nso_subsectors() to drill into sub-categories.
nso_sectors()nso_sectors()
A tibble with columns id, type, and text.
sectors <- nso_sectors() head(sectors)sectors <- nso_sectors() head(sectors)
Navigates one level deeper in the PXWeb catalogue hierarchy. Pass in a
path id obtained from nso_sectors() or a previous nso_subsectors()
call to list its children (sub-sectors or tables).
nso_subsectors(subid)nso_subsectors(subid)
subid |
Path id from |
A tibble with columns: id, type, text.
sectors <- nso_sectors() nso_subsectors(sectors$id[1])sectors <- nso_sectors() nso_subsectors(sectors$id[1])
Returns a tibble with one row per dimension and a codes list-column,
where each element is a tibble of codes and labels (code, label_en,
label_mn). Useful for manual query assembly and for inspecting available
categories per dimension.
nso_table_meta(tbl_id)nso_table_meta(tbl_id)
tbl_id |
Table identifier (e.g., "DT_NSO_0300_001V2"). |
A tibble with columns: dim (display name), code (dimension code),
is_time (logical), n_values (integer), and codes (list of tibbles).
meta <- nso_table_meta("DT_NSO_0300_001V2") metameta <- nso_table_meta("DT_NSO_0300_001V2") meta
Inspects the table metadata to find the time dimension and returns its available period labels (e.g., years or year-months).
nso_table_periods(tbl_id)nso_table_periods(tbl_id)
tbl_id |
Table identifier. |
Character vector of period labels (e.g., years)
periods <- nso_table_periods("DT_NSO_0300_001V2") head(periods)periods <- nso_table_periods("DT_NSO_0300_001V2") head(periods)
Displays a human-readable summary of the query, including the table identifier and the first few dimension selections.
## S3 method for class 'nso_query' print(x, ...)## S3 method for class 'nso_query' print(x, ...)
x |
An |
... |
Additional arguments passed to print methods (ignored). |
x, invisibly.