| Title: | Open Data SDMX API Client |
|---|---|
| Description: | The data and meta data from various statistical agencies such as OECD and Statistics Netherlands can be browsed and downloaded. The client uses the open data SDMX API of Statistics Netherlands. |
| Authors: | Edwin de Jonge [aut, cre] (ORCID: <https://orcid.org/0000-0002-6580-4718>), Roelof Waaijman [ctb] |
| Maintainer: | Edwin de Jonge <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.9000 |
| Built: | 2026-06-05 07:55:37 UTC |
| Source: | https://github.com/edwindj/sdmxdata |
Retrieve data from an sdmx api and return it as a data.frame
## S3 method for class 'sdmx_v2_1_data_request' as.data.frame(x, row.names = NULL, optional = FALSE, ...)## S3 method for class 'sdmx_v2_1_data_request' as.data.frame(x, row.names = NULL, optional = FALSE, ...)
x |
An |
row.names |
Not used |
optional |
Not used |
... |
reserved for future use |
a data.frame
if (interactive()){ dfs <- list_dataflows() flowRef <- paste0(dfs[2, c("agencyID", "id", "version")], sep=",") # json d <- sdmx_v2_1_data_request(flowRef = flowRef) |> as.data.frame() }if (interactive()){ dfs <- list_dataflows() flowRef <- paste0(dfs[2, c("agencyID", "id", "version")], sep=",") # json d <- sdmx_v2_1_data_request(flowRef = flowRef) |> as.data.frame() }
Retrieve data from an sdmx api and return it as a data.frame
## S3 method for class 'sdmx_v2_1_data_request' as.data.table(x, keep.rownames = FALSE, ...)## S3 method for class 'sdmx_v2_1_data_request' as.data.table(x, keep.rownames = FALSE, ...)
x |
An httr2 request object |
keep.rownames |
logical, not used |
... |
reserved for future use |
data.table
if (interactive()){ dfs <- list_dataflows() flowRef <- paste0(dfs[2, c("agencyID", "id", "version")], sep=",") # json d <- sdmx_v2_1_data_request(flowRef = flowRef) |> as.data.frame() }if (interactive()){ dfs <- list_dataflows() flowRef <- paste0(dfs[2, c("agencyID", "id", "version")], sep=",") # json d <- sdmx_v2_1_data_request(flowRef = flowRef) |> as.data.frame() }
This function creates a data request from a URL that contains the necessary parameters for an SDMX data request.
Note that this is a raw request. Most users are better off using get_observations_from_url() instead.
data_request_from_url(url, ..., verbose = FALSE)data_request_from_url(url, ..., verbose = FALSE)
url |
A URL that contains the parameters for the data request, including flowRef. |
... |
Additional parameters to be passed to the data request. |
verbose |
If |
An object of class sdmx_v2_1_data_request() that can be used to retrieve data.
# create a data request object from a URL url <- "https://sdmx.oecd.org/public/rest/data/OECD.TAD.ARP,DSD_FISH_PROD@DF_FISH_AQUA,1.0/.A.._T.T?startPeriod=2010&dimensionAtObservation=AllDimensions" # it does not execute the request, it just creates the request object req <- data_request_from_url(url, verbose = TRUE) # this is the raw csv output of the SDMX data request # most users are better of using `get_observations_from_url()` d <- req |> as.data.frame() head(d)# create a data request object from a URL url <- "https://sdmx.oecd.org/public/rest/data/OECD.TAD.ARP,DSD_FISH_PROD@DF_FISH_AQUA,1.0/.A.._T.T?startPeriod=2010&dimensionAtObservation=AllDimensions" # it does not execute the request, it just creates the request object req <- data_request_from_url(url, verbose = TRUE) # this is the raw csv output of the SDMX data request # most users are better of using `get_observations_from_url()` d <- req |> as.data.frame() head(d)
Download the response of a request to a file
download_to(x, destfile, ...)download_to(x, destfile, ...)
x |
An httr2 request object |
destfile |
A character string with the path to the file to save the response |
... |
saved for future use |
Download the response of a request to a file, but allows for different format specifications e.g. csv, json, xml
A data.frame with several endpoints for sdmx data
endpointsendpoints
A data.frame with columns:
id - the id of the endpoint
name - the name of agency of the endpoint
url - the url of the endpoint
language - the default language of the endpoint
data("endpoints") endpoints[, 1:3] OECD <- get_endpoint("OECD") OECD$verbose <- TRUE list_oecd <- OECD |> list_dataflows(cache=TRUE)data("endpoints") endpoints[, 1:3] OECD <- get_endpoint("OECD") OECD$verbose <- TRUE list_oecd <- OECD |> list_dataflows(cache=TRUE)
Retrieve data from an SDMX data API. The function retrieves data from the SDMX data API and returns it as a data.frame.
get_data( endpoint = NULL, agencyID, id, version = "latest", ref = NULL, startPeriod = NULL, endPeriod = NULL, filter_on = list(), ..., dim_contents = c("label", "both", "id"), obs_value_numeric = TRUE, raw = FALSE, language = NULL, cache = TRUE, pivot = NULL, verbose = getOption("sdmxdata.verbose", FALSE) )get_data( endpoint = NULL, agencyID, id, version = "latest", ref = NULL, startPeriod = NULL, endPeriod = NULL, filter_on = list(), ..., dim_contents = c("label", "both", "id"), obs_value_numeric = TRUE, raw = FALSE, language = NULL, cache = TRUE, pivot = NULL, verbose = getOption("sdmxdata.verbose", FALSE) )
endpoint |
An endpoint, url or |
agencyID |
The agency ID |
id |
The id of the dataflow |
version |
The version of the dataflow, default "latest" |
ref |
The dataflow reference can be used in stead of |
startPeriod |
The start period for which the data should be returned, works only for dataflows with an explicit time dimension. |
endPeriod |
The end period for which the data should be returned, works only for dataflows with an explicit time dimension. |
filter_on |
A named list of filters to apply to the data, if not specified or |
... |
Additional parameters to pass to the request |
dim_contents |
The contents of the dimension columns, either "label", "id" or "both" |
obs_value_numeric |
Should the OBS_VALUE column be coerced to numeric? Default is |
raw |
If |
language |
The language of the metadata |
cache |
if |
pivot |
|
verbose |
If |
get_data is similar to get_observations(), but its differs in two aspects:
it returns only data columns, no attribute columns
it can pivot the observations, i.e. it can return the data in a wide format, which is useful feature when the data has multiple measures or when the data is to presented as a time series.
a data.frame
Other retrieve data:
get_observations(),
get_observations_from_url()
Get information and structure about a dataflow. The dataflow can be identified
using agencyID, id and version or by using the ref argument.
get_dataflow_structure( endpoint = NULL, agencyID = getOption("sdmxdata.agencyID", NULL), id, version = "latest", ref, language = getOption("sdmxdata.language"), cache = TRUE, verbose = getOption("sdmxdata.verbose", FALSE) )get_dataflow_structure( endpoint = NULL, agencyID = getOption("sdmxdata.agencyID", NULL), id, version = "latest", ref, language = getOption("sdmxdata.language"), cache = TRUE, verbose = getOption("sdmxdata.verbose", FALSE) )
endpoint |
An endpoint or an object that can be coerced to an SDMXEndpoint |
agencyID |
The agency ID of the dataflow |
id |
The id of the dataflow |
version |
The version of the dataflow, defaults to "latest" |
ref |
A string with the flow reference, in the form of "agencyID:id(version)". Overrules the agencyID, id and version arguments. |
language |
The language of the metadata |
cache |
logical, if |
verbose |
print some information on the console |
For most use cases the agencyID and id are to be preferred, as the ref argument
includes a specific version number of the dataflow. Not specifying a version number
will default to the latest version. If it is desirable to pin the reference of
the dataflow to a specific version, the ref argument can be used. The ref
argument can also be found in list_dataflows().
a list with the dataflow information
Connect to an SDMX endpoint
get_endpoint(id, language = NULL, singleton = TRUE, verbose = FALSE)get_endpoint(id, language = NULL, singleton = TRUE, verbose = FALSE)
id |
character, id of one of the available |
language |
character, the language to use for the text used in the response. |
singleton |
logical, if |
verbose |
logical, if |
a SDMXEndpoint object
Other SDMX endpoints:
SDMXEndpoint,
sdmx_endpoint()
data("endpoints") endpoints[, 1:3] OECD <- get_endpoint("OECD") OECD$verbose <- TRUE list_oecd <- OECD |> list_dataflows(cache=TRUE)data("endpoints") endpoints[, 1:3] OECD <- get_endpoint("OECD") OECD$verbose <- TRUE list_oecd <- OECD |> list_dataflows(cache=TRUE)
Get observations from an SDMX provider. It retrieves the
data from the SDMX provider and returns it as a data.frame.
get_observations( endpoint = NULL, agencyID, id, version = "latest", ref = NULL, startPeriod = NULL, endPeriod = NULL, filter_on = list(), use_factor = TRUE, ..., language = NULL, as.data.table = FALSE, dim_contents = c("label", "both", "id"), attributes_contents = c("label", "id", "both"), obs_value_numeric = TRUE, raw = FALSE, drop_first_columns = !raw, cache = TRUE, verbose = getOption("sdmxdata.verbose", FALSE) )get_observations( endpoint = NULL, agencyID, id, version = "latest", ref = NULL, startPeriod = NULL, endPeriod = NULL, filter_on = list(), use_factor = TRUE, ..., language = NULL, as.data.table = FALSE, dim_contents = c("label", "both", "id"), attributes_contents = c("label", "id", "both"), obs_value_numeric = TRUE, raw = FALSE, drop_first_columns = !raw, cache = TRUE, verbose = getOption("sdmxdata.verbose", FALSE) )
endpoint |
An endpoint, url or |
agencyID |
The agency ID |
id |
The id of the dataflow |
version |
The version of the dataflow, default "latest" |
ref |
The dataflow reference can be used in stead of |
startPeriod |
The start period for which the data should be returned, works only for dataflows with an explicit time dimension. |
endPeriod |
The end period for which the data should be returned, works only for dataflows with an explicit time dimension. |
filter_on |
A named list of filters to apply to the data, if not specified or |
use_factor |
if |
... |
Additional parameters to pass to the request |
language |
The language of the metadata |
as.data.table |
If |
dim_contents |
The contents of the dimension columns, either "label", "id" or "both" |
attributes_contents |
The contents of the attribute columns, either "label", "id" or "both" |
obs_value_numeric |
Should the OBS_VALUE column be coerced to numeric? Default is |
raw |
If |
drop_first_columns |
Should the first columns be dropped? Default is |
cache |
if |
verbose |
If |
By default the dimension and attribute columns are recoded to
factors with labels. This can be changed with the dim_contents and
attributes_contents arguments.
data.frame() or data.table::data.table() depending on as.data.table
Other retrieve data:
get_data(),
get_observations_from_url()
dfs <- list_dataflows() ref <- dfs$ref[4] dfs <- list_dataflows() ref <- dfs$ref[4] obs <- get_observations( ref = ref, filter_on = list( "Perioden" = c("2009JJ00") ), verbose = TRUE ) obsdfs <- list_dataflows() ref <- dfs$ref[4] dfs <- list_dataflows() ref <- dfs$ref[4] obs <- get_observations( ref = ref, filter_on = list( "Perioden" = c("2009JJ00") ), verbose = TRUE ) obs
Retrieve observations using the url from the data-explorer. The url given
will be parsed and translated into a get_observations() call, to get you started.
The call will be printed to the console or can be retrieved by settings return_query = TRUE.
get_observations_from_url(url, return_query = FALSE, verbose = FALSE)get_observations_from_url(url, return_query = FALSE, verbose = FALSE)
url |
character, the REST 2.1 url retrieved from an external source |
return_query |
logical, if |
verbose |
logical, if |
when return_query = FALSE a data.frame with the observations, i.e.
same output as get_observations(). Otherwise a list with the query object
and the parameters
Other retrieve data:
get_data(),
get_observations()
# retrieve an external url pointing to sdmx data url <- "https://sdmx.oecd.org/public/rest/data/OECD.TAD.ARP,DSD_FISH_PROD@DF_FISH_AQUA,1.0/.A.._T.T?startPeriod=2010&dimensionAtObservation=AllDimensions" obs <- get_observations_from_url(url) head(obs) query <- get_observations_from_url(url, return_query = TRUE) query# retrieve an external url pointing to sdmx data url <- "https://sdmx.oecd.org/public/rest/data/OECD.TAD.ARP,DSD_FISH_PROD@DF_FISH_AQUA,1.0/.A.._T.T?startPeriod=2010&dimensionAtObservation=AllDimensions" obs <- get_observations_from_url(url) head(obs) query <- get_observations_from_url(url, return_query = TRUE) query
list the agencies of an endpoint
list_agencies( endpoint = NULL, language = NULL, raw = FALSE, cache = TRUE, verbose = getOption("sdmxdata.verbose", FALSE) )list_agencies( endpoint = NULL, language = NULL, raw = FALSE, cache = TRUE, verbose = getOption("sdmxdata.verbose", FALSE) )
endpoint |
an endpoint, url or an |
language |
the language to return the data in. |
raw |
if |
cache |
if |
verbose |
if |
Get sdmx categoryschemes from a given endpoint.
list_categoryschemes( endpoint = NULL, agencyID = NULL, ..., language = "nl", cache = TRUE, raw = FALSE, verbose = getOption("sdmxdata.verbose", FALSE) )list_categoryschemes( endpoint = NULL, agencyID = NULL, ..., language = "nl", cache = TRUE, raw = FALSE, verbose = getOption("sdmxdata.verbose", FALSE) )
endpoint |
an endpoint, url or |
agencyID |
A character string from a given agencyID. |
... |
saved for future use. |
language |
The language to use for the text used in the response. |
cache |
if |
raw |
If |
verbose |
if |
a data.frame with available dataflows
dfs <- list_dataflows(verbose = TRUE) head(dfs)dfs <- list_dataflows(verbose = TRUE) head(dfs)
List the sdmx dataflows from a given endpoint.
list_dataflows( endpoint = NULL, agencyID = NULL, ..., language = NULL, cache = TRUE, raw = FALSE, verbose = getOption("sdmxdata.verbose", FALSE) )list_dataflows( endpoint = NULL, agencyID = NULL, ..., language = NULL, cache = TRUE, raw = FALSE, verbose = getOption("sdmxdata.verbose", FALSE) )
endpoint |
A character string or endpoint for a given endpoint. |
agencyID |
A character string from a given agencyID. |
... |
saved for future use. |
language |
The language to use for the text used in the response. |
cache |
if |
raw |
If |
verbose |
if |
a data.frame with available dataflows
dfs <- list_dataflows(verbose = TRUE) head(dfs)dfs <- list_dataflows(verbose = TRUE) head(dfs)
Create an endpoint from an url or httr2 request object
sdmx_endpoint(x, ...) ## S3 method for class 'character' sdmx_endpoint( x, id = gsub("\\W+", "_", x), name = x, language = NULL, cache_dir = file.path(tempdir(), "sdmxdata", id), verbose = getOption("sdmxdata.verbose", FALSE), ... ) ## S3 method for class 'httr2_request' sdmx_endpoint(x, ...)sdmx_endpoint(x, ...) ## S3 method for class 'character' sdmx_endpoint( x, id = gsub("\\W+", "_", x), name = x, language = NULL, cache_dir = file.path(tempdir(), "sdmxdata", id), verbose = getOption("sdmxdata.verbose", FALSE), ... ) ## S3 method for class 'httr2_request' sdmx_endpoint(x, ...)
x |
An endpoint or an httr2 request object |
... |
saved for future use |
id |
character, the id of the endpoint |
name |
character, the name of the endpoint |
language |
character, the language to use for the text used in the response |
cache_dir |
character, the directory to cache the metadata in |
verbose |
logical, if |
a SDMXEndpoint object
Other SDMX endpoints:
SDMXEndpoint,
get_endpoint()
sdmx_v2_1_data_request is a wrapper around the smdx rest api v2.1 and is used to retrieve
data from the api.
sdmx_v2_1_data_request( endpoint = NULL, resource = c("data", "metadata"), flowRef = NULL, key = NULL, providerRef = NULL, ..., startPeriod = NULL, endPeriod = NULL, updatedAfter = NULL, firstNObservations = NULL, lastNObservations = NULL, dimensionAtObservation = NULL, detail = c("full", "dataonly", "serieskeysonly", "nodata"), includeHistory = NULL, labels = c("both", "id") )sdmx_v2_1_data_request( endpoint = NULL, resource = c("data", "metadata"), flowRef = NULL, key = NULL, providerRef = NULL, ..., startPeriod = NULL, endPeriod = NULL, updatedAfter = NULL, firstNObservations = NULL, lastNObservations = NULL, dimensionAtObservation = NULL, detail = c("full", "dataonly", "serieskeysonly", "nodata"), includeHistory = NULL, labels = c("both", "id") )
endpoint |
An endpoint or an object that can be coerced to an endpoint |
resource |
The resource to request. Either "data" or "metadata" |
flowRef |
The flow reference to request, see details |
key |
The key to request, see details |
providerRef |
The provider reference to request, see details |
... |
Additional parameters to pass to the request |
startPeriod |
The start period for which the data should be returned |
endPeriod |
The end period for which the data should be returned |
updatedAfter |
Only return data that has been updated after this date |
firstNObservations |
Only return the first n observations |
lastNObservations |
Only return the last n observations |
dimensionAtObservation |
The dimensions of the observations should be returned |
detail |
The detail of the data to return. Either "full", "dataonly", "serieskeysonly" or "nodata" |
includeHistory |
Include the different versions of the data |
labels |
Include the labels in the data, only valid when format is "csv" |
a modified httr2::request() object
Wrapper for the SDMX REST v2.1 interface for structured information
sdmx_v2_1_structure_request( endpoint = NULL, resource = "dataflow", agencyID = NULL, resourceID = NULL, version = NULL, itemID = NULL, format = c("json", "xml"), language = NULL, ..., detail = c("full", "allstubs", "referencestubs", "allcompletestubs", "referencecompletestubs", "referencepartial"), references = c("none", "parents", "parentsandsiblings", "children", "descendants", "all") )sdmx_v2_1_structure_request( endpoint = NULL, resource = "dataflow", agencyID = NULL, resourceID = NULL, version = NULL, itemID = NULL, format = c("json", "xml"), language = NULL, ..., detail = c("full", "allstubs", "referencestubs", "allcompletestubs", "referencecompletestubs", "referencepartial"), references = c("none", "parents", "parentsandsiblings", "children", "descendants", "all") )
endpoint |
An endpoint or an object that can be coerced to an endpoint |
resource |
The resource to request. One of "datastructure", "metadatastructure", "categoryscheme", "conceptscheme", "codelist", "hierarchicalcodelist", "organisationscheme", "agencyscheme", "dataproviderscheme", "dataconsumerscheme", "organisationunitscheme", "dataflow", "metadataflow", "reportingtaxonomy", "provisionagreement", "structureset", "process", "categorisation", "contentconstraint", "attachmentconstraint", "actualconstraint", "allowedconstraint", "structure", "transformationscheme", "rulesetscheme", "userdefinedoperatorscheme", "customtypescheme", "namepersonalisationscheme", "vtlmappingscheme" |
agencyID |
The agency ID to request |
resourceID |
The resource ID to request |
version |
The version to request |
itemID |
The item ID to request |
format |
The format to request. Either "json" or "xml" |
language |
The language to use for the text used in the response |
... |
saved for future use |
detail |
The detail of the data to return. Either "full", "allstubs", "referencestubs", "allcompletestubs", "referencecompletestubs", "referencepartial" |
references |
The references to return. Either "none", "parents", "parentsandsiblings", "children", "descendants", "all" |
a modified httr2::request() object
Create a:
a known endpoint from endpoints use get_endpoint().
unlisted SDMX 2.1 REST endpoint use sdmx_endpoint().
a new SDMX endpoint use SDMXEndpoint$new().
Base class for SDMX endpoints
reqhttr2::request() object, can be adjusted to add headers or proxy settings
versioncharacter, the SDMX version of the endpoint
idcharacter, the id of the endpoint
namecharacter, the name of the endpoint
urlcharacter, the url of the endpoint
verboselogical, if TRUE print information the connection
cache_dircharacter, the directory to cache the metadata in
languagecharacter, the language to use for the text used in the response
new()
create a new SDMXEndpoint object, see also sdmx_endpoint()
SDMXEndpoint$new(
url,
id = gsub("\\W+", "_", url),
name = url,
language = NULL,
cache_dir = file.path(tempdir(), "sdmxdata", id),
verbose = getOption("sdmxdata.verbose", FALSE)
)urlcharacter, the url of the endpoint
idcharacter, the id of the endpoint
namecharacter, the name of the endpoint
languagecharacter, the language to use for the text used in the response
cache_dircharacter, the directory to cache the metadata in
verboselogical, if TRUE print information on the connection
clone()
The objects of this class are cloneable with this method.
SDMXEndpoint$clone(deep = FALSE)
deepWhether to make a deep clone.
Other SDMX endpoints:
get_endpoint(),
sdmx_endpoint()