| Title: | From/to Classification Converter |
|---|---|
| Description: | Convert text into target classifications (e.g., ISO 3166-1) using a JSON mapping with regular expressions. Provides helpers to return the full mapping and associated metadata. |
| Authors: | Ilya Bolotov [aut, cre] (ORCID: <https://orcid.org/0000-0003-1148-7144>) |
| Maintainer: | Ilya Bolotov <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.1.0 |
| Built: | 2026-06-07 19:44:03 UTC |
| Source: | https://github.com/econcz/rconvertu |
Pure-R implementation of the convertu API.
Converts text into a target classification using a JSON mapping, or
returns mapping/metadata (info / dump modes).
cconv( data = NULL, json_file = NULL, info = FALSE, dump = FALSE, to = NULL, text = character() ) convertu( data = NULL, json_file = NULL, info = FALSE, dump = FALSE, to = NULL, text = character() )cconv( data = NULL, json_file = NULL, info = FALSE, dump = FALSE, to = NULL, text = character() ) convertu( data = NULL, json_file = NULL, info = FALSE, dump = FALSE, to = NULL, text = character() )
data |
list of named lists (optional). A complete classification
mapping provided directly. If supplied without |
json_file |
character(1). Path to the classification JSON file.
If not provided, the default bundled |
info |
logical(1). If |
dump |
logical(1). If |
to |
character(1). Target field name to return from matched records
(e.g., |
text |
character(). One or more input strings to convert. A single string input yields a single string output; a vector yields a character vector of converted results. |
Behavior:
info = TRUE → returns only metadata and sources entries (no conversion).
dump = TRUE → returns the full classification (no metadata/sources).
Otherwise → converts text using regex-based matching and returns the
value from the requested field to.
If info = TRUE or dump = TRUE, returns a list of records.
Otherwise, returns a character vector of converted values:
If length(text) == 1, returns a length-one character scalar.
If no match is found for an input, the original value is returned.
The classification is a top-level list with three kinds of elements:
Many record elements (unnamed or named) with fields:
regex (chr): pattern matching the input text.
name_en (chr): English short name.
name_fr (chr): French short name (optional).
iso3 (chr): alpha-3 code (example field).
iso2 (chr): alpha-2 code (example field).
isoN (chr): numeric code (example field).
One element metadata (named list) mapping field names to their
human-readable descriptions:
metadata = list(
name_en = "English short name",
name_fr = "French short name",
iso3 = "alpha-3 code",
iso2 = "alpha-2 code",
isoN = "numeric"
)
One element sources (character vector) with references:
sources = c(
"https://www.iso.org/iso-3166-country-codes.html",
"https://en.wikipedia.org/wiki/List_of_alternative_country_names"
)
# Single conversion cconv(to = "iso3", text = "Czech Republic") # Multiple conversions cconv(to = "iso3", text = c("Czech Republic", "Slovakia")) # Show bundled metadata cconv(info = TRUE) # Dump classification mapping only cconv(dump = TRUE)# Single conversion cconv(to = "iso3", text = "Czech Republic") # Multiple conversions cconv(to = "iso3", text = c("Czech Republic", "Slovakia")) # Show bundled metadata cconv(info = TRUE) # Dump classification mapping only cconv(dump = TRUE)
Ensures the provided JSON-based classification data follows the expected structure and contains valid fields.
check_classification(x)check_classification(x)
x |
list. The classification object loaded via
|
Logical, TRUE if valid, otherwise an error is raised.
path <- system.file("extdata", "classification.json", package = "rconvertu") cls <- jsonlite::fromJSON(path, simplifyVector = FALSE) check_classification(cls)path <- system.file("extdata", "classification.json", package = "rconvertu") cls <- jsonlite::fromJSON(path, simplifyVector = FALSE) check_classification(cls)