| Title: | R Interface to PXWEB APIs |
|---|---|
| Description: | Generic interface for the PX-Web/PC-Axis API. The PX-Web/PC-Axis API is used by organizations such as Statistics Sweden and Statistics Finland to disseminate data. The R package can interact with all PX-Web/PC-Axis APIs to fetch information about the data hierarchy, extract metadata and extract and parse statistics to R data.frame format. PX-Web is a solution to disseminate PC-Axis data files in dynamic tables on the web. Since 2013 PX-Web contains an API to disseminate PC-Axis files. |
| Authors: | Mans Magnusson [aut, cre] (ORCID: <https://orcid.org/0000-0002-0296-2719>), Markus Kainu [aut], Janne Huovari [aut], Leo Lahti [aut] (ORCID: <https://orcid.org/0000-0001-5537-637X>), Love Hansson [ctb], Eydun Nielsen [ctb], Bo Werth [ctb], Thomas Runarsson [ctb], Torbjörn Lindquist [ctb], Palmar Thorsteinsson [ctb], Pyry Kantanen [ctb], Sebastian Ankargren [ctb] |
| Maintainer: | Mans Magnusson <[email protected]> |
| License: | BSD_2_clause + file LICENSE |
| Version: | 0.19.2 |
| Built: | 2026-07-02 13:20:10 UTC |
| Source: | https://github.com/rOpenGov/pxweb |
PXWEB is a common web API used by many European Statistical agencies to disseminate official statistics. The pxweb package facilitates connections and usage of these APIs.
Maintainer: Mans Magnusson [email protected] (ORCID)
Authors:
Markus Kainu
Janne Huovari
Leo Lahti (ORCID)
Other contributors:
Love Hansson [contributor]
Eydun Nielsen [contributor]
Bo Werth [contributor]
Thomas Runarsson [contributor]
Torbjörn Lindquist [contributor]
Palmar Thorsteinsson [contributor]
Pyry Kantanen [contributor]
Sebastian Ankargren [contributor]
The decription of the PXWEB API here: https://www.scb.se/en/About-us/about-the-website-and-terms-of-use/open-data-api/ The official home page of PXWEB can be found here: https://www.scb.se/en/services/statistical-programs-for-px-files/px-web/
Useful links:
Report bugs at https://github.com/rOpenGov/pxweb/issues
These function has as from version 0.10.0 become defunct. Call the functions to get information on new functions to use.
api_catalogue() update_pxweb_apis() api_parameters(url = NULL) base_url(api, version = NULL, language = NULL) get_pxweb_data(url, dims, clean = FALSE, encoding = NULL) get_pxweb_dims(node, verbose = TRUE) get_pxweb_levels(baseURL, descriptions = FALSE, quiet = FALSE, ...) get_pxweb_metadata( path = NULL, node = NULL, topnodes = NULL, quiet = TRUE, baseURL = NULL, ... ) pxweb_api checkForLevels(url)api_catalogue() update_pxweb_apis() api_parameters(url = NULL) base_url(api, version = NULL, language = NULL) get_pxweb_data(url, dims, clean = FALSE, encoding = NULL) get_pxweb_dims(node, verbose = TRUE) get_pxweb_levels(baseURL, descriptions = FALSE, quiet = FALSE, ...) get_pxweb_metadata( path = NULL, node = NULL, topnodes = NULL, quiet = TRUE, baseURL = NULL, ... ) pxweb_api checkForLevels(url)
url |
Defunct argument. |
api |
Defunct argument. |
version |
Defunct argument. |
language |
Defunct argument. |
dims |
Defunct argument. |
clean |
Defunct argument. |
encoding |
Defunct argument. |
node |
Defunct argument. |
verbose |
Defunct argument. |
baseURL |
Defunct argument. |
descriptions |
Defunct argument. |
quiet |
Defunct argument. |
... |
Defunct argument. |
path |
Defunct argument. |
topnodes |
Defunct argument. |
An object of class list of length 1.
Assert that a given pxweb query can be split
assert_query_can_be_split_to_batches(pxq, pxmd, mxv)assert_query_can_be_split_to_batches(pxq, pxmd, mxv)
pxq |
a [pxweb_query] object |
pxmd |
a [pxweb_metadata] object |
mxv |
maximum batch size |
Do a GET call to PXWEB API for advanced users
pxweb_advanced_get( url, query = NULL, verbose = TRUE, log_http_calls = FALSE, pxmdo = NULL, output_format = "json-stat2", ... )pxweb_advanced_get( url, query = NULL, verbose = TRUE, log_http_calls = FALSE, pxmdo = NULL, output_format = "json-stat2", ... )
url |
a |
query |
a json string, json file or list object that can be coherced to a |
verbose |
should large queries print out progress. |
log_http_calls |
Should the http calls to the API be logged (for debugging reasons). If TRUE, all calls and responses are logged and written to "log_pxweb_api_http_calls.txt" in the working directory. |
pxmdo |
A |
output_format |
output format for PXWEB API v2 data requests. Defaults
to |
... |
Further arguments sent to |
This function is intended for more advanced users that want to supply specific arguments in
httr calls or what to debug httr calls.
pxweb_get() is a wrapper for standard use.
Checks whether each version and language combination in a PXWEB API catalogue entry exposes a valid config endpoint.
pxweb_audit_api_catalogue_config( apis = pxweb_api_catalogue(), timeout = 10, verbose = TRUE )pxweb_audit_api_catalogue_config( apis = pxweb_api_catalogue(), timeout = 10, verbose = TRUE )
apis |
a list of pxweb_api_catalogue_entry elements |
timeout |
Time limit in seconds for each config endpoint request. |
verbose |
Print progress while auditing. |
A data.frame with one row per catalogue URL.
Cite a PXWEB data object
pxweb_cite(x, style = "citation")pxweb_cite(x, style = "citation")
x |
a |
style |
see |
Functionality to automatic cite PXWEB data objects.
Extracts aggregation and value set codelists from a PXWEB API v2 metadata
object. Use the returned id values with pxweb_aggregation
or pxweb_valueset.
pxweb_codelists(x, variable = NULL, type = NULL)pxweb_codelists(x, variable = NULL, type = NULL)
x |
a |
variable |
optional variable code or label to filter on. |
type |
optional codelist type to filter on, for example
|
A data.frame with columns variable_code,
variable_text, id, label, type, and href.
## Not run: meta <- pxweb_get("https://statistikdatabasen.scb.se/api/v2/tables/TAB5974/metadata?lang=sv") pxweb_codelists(meta, variable = "Alder") ## End(Not run)## Not run: meta <- pxweb_get("https://statistikdatabasen.scb.se/api/v2/tables/TAB5974/metadata?lang=sv") pxweb_codelists(meta, variable = "Alder") ## End(Not run)
Do a GET call to PXWEB API
pxweb_get(url, query = NULL, verbose = TRUE, output_format = "json-stat2")pxweb_get(url, query = NULL, verbose = TRUE, output_format = "json-stat2")
url |
a |
query |
a json string, json file or list object that can be coherced to a |
verbose |
should large queries print out progress. |
output_format |
output format for PXWEB API v2 data requests. Defaults
to |
If query = NULL, returns metadata about the API path: a
pxweb_levels object for API branches or a pxweb_metadata
object for data tables. If query is supplied, returns the parsed data
response, typically a pxweb_data, pxweb_data_jsonstat,
pxweb_data_jsonstat2, or pxweb_data_v2 object depending on the
API version and response format. File-based response formats, such as
"px" and "sdmx", are written to a temporary file and the file
path is returned. Returns NULL if the URL host cannot be reached.
## Not run: url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy" px_meta_data <- pxweb_get(url) url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101" px_levels <- pxweb_get(url) url <- "https://api.scb.se/OV0104/v1/doris/sv" px_levels <- pxweb_get(url) url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy" query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_query_example.json" ) px_data <- pxweb_get(url = url, query = query) # Convert to data.frame as.data.frame(px_data, column.name.type = "text", variable.value.type = "text") # Get raw data as.matrix(px_data, column.name.type = "code", variable.value.type = "code") # Get data comments pxweb_data_comments(px_data) # Get jsonstat data jstat <- query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json-stat_query_example.json" ) jstat_data <- pxweb_get(url = url, query = query) # Get very large datasets (multiple downloads needed) big_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_big_query_example.json" ) px_data <- pxweb_get(url = url, query = big_query) # Get json-stat2 data from StatFin using a downloaded json query jstat2_url <- "https://pxdata.stat.fi/PxWeb/api/v1/en/StatFin/vaenn/139e.px" jstat2_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "statfin_vaenn_139e_query.json" ) jstat2_data <- pxweb_get(url = jstat2_url, query = jstat2_query) ## End(Not run)## Not run: url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy" px_meta_data <- pxweb_get(url) url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101" px_levels <- pxweb_get(url) url <- "https://api.scb.se/OV0104/v1/doris/sv" px_levels <- pxweb_get(url) url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy" query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_query_example.json" ) px_data <- pxweb_get(url = url, query = query) # Convert to data.frame as.data.frame(px_data, column.name.type = "text", variable.value.type = "text") # Get raw data as.matrix(px_data, column.name.type = "code", variable.value.type = "code") # Get data comments pxweb_data_comments(px_data) # Get jsonstat data jstat <- query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json-stat_query_example.json" ) jstat_data <- pxweb_get(url = url, query = query) # Get very large datasets (multiple downloads needed) big_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_big_query_example.json" ) px_data <- pxweb_get(url = url, query = big_query) # Get json-stat2 data from StatFin using a downloaded json query jstat2_url <- "https://pxdata.stat.fi/PxWeb/api/v1/en/StatFin/vaenn/139e.px" jstat2_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "statfin_vaenn_139e_query.json" ) jstat2_data <- pxweb_get(url = jstat2_url, query = jstat2_query) ## End(Not run)
Do a GET call to PXWEB API and return a data.frame
pxweb_get_data( url, query, verbose = TRUE, column.name.type = "text", variable.value.type = "text", output_format = "json-stat2" )pxweb_get_data( url, query, verbose = TRUE, column.name.type = "text", variable.value.type = "text", output_format = "json-stat2" )
url |
a |
query |
a json string, json file or list object that can be coherced to a |
verbose |
should large queries print out progress. |
column.name.type |
character: should |
variable.value.type |
character: should |
output_format |
output format for PXWEB API v2 data requests. Defaults
to |
The functions use will do a pxweb_query to a PXWEB url and return a data.frame.
This is a wrapper for the pxweb_get function.
See pxweb_get for mor general usage and pxweb_query for details on PXWEB queries.
## Not run: url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy" query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_query_example.json" ) df <- pxweb_get_data(url = url, query = query) ## End(Not run)## Not run: url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy" query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_query_example.json" ) df <- pxweb_get_data(url = url, query = query) ## End(Not run)
Wrapper function (for pxweb_get) to simply find and download data to the current R session.
interactive_pxweb() is a deprecated alias for pxweb_interactive().
pxweb_interactive(x = NULL) interactive_pxweb(x = NULL)pxweb_interactive(x = NULL) interactive_pxweb(x = NULL)
x |
The name or alias of the pxweb api to connect to, a |
Invisibly returns a list with at least:
urlThe URL to the selected data table.
queryA pxweb_query object that can be passed to pxweb_get.
If data is downloaded during the interactive session, the list also includes
data, containing the downloaded data object returned by pxweb_get.
This return value is not the same as calling pxweb_get() directly.
pxweb_api_catalogue() # List apis ## The examples below can only be run in interactive mode ## x <- pxweb_interactive() ## x <- pxweb_interactive(x = "api.scb.se") ## x <- pxweb_interactive(x = "https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/") ## x <- pxweb_interactive(x = "https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/")pxweb_api_catalogue() # List apis ## The examples below can only be run in interactive mode ## x <- pxweb_interactive() ## x <- pxweb_interactive(x = "api.scb.se") ## x <- pxweb_interactive(x = "https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/") ## x <- pxweb_interactive(x = "https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/")
The function parses the response from a call made to a PXWEB API
using the httr R package. In this way it is possible to parse the
content of calls made outside the pxweb R package.
pxweb_parse_response(x) is_pxweb_response(x)pxweb_parse_response(x) is_pxweb_response(x)
x |
a |
pxweb_query object to a json stringConvert a pxweb_query object to a json string
pxweb_query_as_json(pxq, ...)pxweb_query_as_json(pxq, ...)
pxq |
a |
... |
further argument to |
pxweb_query, pxweb_query_as_rcode
json_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_query_example.json" ) pxq <- pxweb_query(json_query) json <- pxweb_query_as_json(pxq, pretty = TRUE)json_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_query_example.json" ) pxq <- pxweb_query(json_query) json <- pxweb_query_as_json(pxq, pretty = TRUE)
pxweb_query object as R codePrint a pxweb_query object as R code
pxweb_query_as_rcode(pxq)pxweb_query_as_rcode(pxq)
pxq |
a |
pxweb_query_as_json, pxweb_query
Helper functions for expressing PXWEB API v2 query selections without relying on magic strings in user code.
pxweb_all() pxweb_latest(code = "9999") pxweb_aggregation( id, value_codes = "*", output_values = c("aggregated", "single") ) pxweb_valueset( id, value_codes = "*", output_values = c("aggregated", "single") ) pxweb_top(n) pxweb_bottom(n)pxweb_all() pxweb_latest(code = "9999") pxweb_aggregation( id, value_codes = "*", output_values = c("aggregated", "single") ) pxweb_valueset( id, value_codes = "*", output_values = c("aggregated", "single") ) pxweb_top(n) pxweb_bottom(n)
code |
placeholder value used before the latest available value is
resolved against table metadata. The default |
id |
codelist id, for example an aggregation id returned by a PXWEB API v2 metadata response. |
value_codes |
value codes to request from the selected codelist. |
output_values |
whether the API should return aggregated values or single values for the codelist. |
n |
number of values to request. |
A pxweb_query_selection object.
query <- list( Region = pxweb_all(), Alder = pxweb_aggregation("agg_Ålder5år_1"), Tid = pxweb_latest() )query <- list( Region = pxweb_all(), Alder = pxweb_aggregation("agg_Ålder5år_1"), Tid = pxweb_latest() )
Search for tables in a PXWEB API. PXWEB API v2 search uses the standard
/tables?query= endpoint. PXWEB API v1 search is supported for API
database roots that expose search, such as Statistics Finland.
pxweb_search( query, api_url, lang = NULL, page_number = 1, page_size = NULL, past_days = NULL, include_discontinued = FALSE, ... )pxweb_search( query, api_url, lang = NULL, page_number = 1, page_size = NULL, past_days = NULL, include_discontinued = FALSE, ... )
query |
a search string. |
api_url |
a PXWEB API root URL. For v2, use an API root such as
|
lang |
language code for PXWEB API v2. For v1, the language is part of
|
page_number |
page number for PXWEB API v2 searches. |
page_size |
page size for PXWEB API v2 searches. |
past_days |
if supplied, restrict PXWEB API v2 searches to tables
updated in the last |
include_discontinued |
include discontinued PXWEB API v2 tables. |
... |
further arguments passed to |
a data.frame with search hits and table URLs.
## Not run: pxweb_search( "population", api_url = "https://statistikdatabasen.scb.se/api/v2", lang = "en" ) pxweb_search( "population", api_url = "https://pxdata.stat.fi/PxWeb/api/v1/en/StatFin" ) ## End(Not run)## Not run: pxweb_search( "population", api_url = "https://statistikdatabasen.scb.se/api/v2", lang = "en" ) pxweb_search( "population", api_url = "https://pxdata.stat.fi/PxWeb/api/v1/en/StatFin" ) ## End(Not run)
Creates a query selection for use inside a named list passed to
pxweb_query or pxweb_get. This is useful when a
PXWEB table requires an explicit API filter such as an aggregation or value
set filter.
pxweb_selection(filter = "item", values)pxweb_selection(filter = "item", values)
filter |
a PXWEB selection filter, for example |
values |
selected value codes for the filter. |
A pxweb_query_selection_filter object.
query <- list( alue_23_20260101 = pxweb_selection("agg:_Regions 2026.agg", "MK01"), timeperiod_y = "2025" ) pxq <- pxweb_query(query)query <- list( alue_23_20260101 = pxweb_selection("agg:_Regions 2026.agg", "MK01"), timeperiod_y = "2025" ) pxq <- pxweb_query(query)
The function can be used to test a whole pxweb api by using the api base url. By using a branch in a tree the api is tested below this branch.
pxweb_test_api( url, test_type = "first", n = 1, verbose = TRUE, time_limit = Inf )pxweb_test_api( url, test_type = "first", n = 1, verbose = TRUE, time_limit = Inf )
url |
The base url to the pxweb api (or a branch of the metadata tree) |
test_type |
What type of test should be done.
The |
n |
sample size if |
verbose |
The function will print information. |
time_limit |
Time limit in second the API is allowed to be tested. |
Function returns a data.frame with information on each node
Two variables are added:
checked : The node has been checked
error : Whether there were errors encountered with the call
download_error : Whether there were errors encountered during download
## Not run: url <- "https://bank.stat.gl/api/v1/en/Greenland/BE/BE01" res <- pxweb_test_api(url) res <- pxweb_test_api(url, test_type = "touch") ## End(Not run)## Not run: url <- "https://bank.stat.gl/api/v1/en/Greenland/BE/BE01" res <- pxweb_test_api(url) res <- pxweb_test_api(url, test_type = "touch") ## End(Not run)
pxweb_query with a pxweb_metadata objectValidate a pxweb_query with a pxweb_metadata object
pxweb_validate_query_with_metadata(pxq, pxmd)pxweb_validate_query_with_metadata(pxq, pxmd)
pxq |
a |
pxmd |
a |
Validate a query with a metadata object to asses that the query can be used to query the table.
## Not run: url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy" json_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_query_example.json" ) pxq <- pxweb_query(json_query) pxweb_validate_query_with_metadata(pxq, pxweb_get(url)) ## End(Not run)## Not run: url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy" json_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "json_query_example.json" ) pxq <- pxweb_query(json_query) pxweb_validate_query_with_metadata(pxq, pxweb_get(url)) ## End(Not run)