| 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.17.1 |
| Built: | 2026-06-01 09:26:37 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, ... )pxweb_advanced_get( url, query = NULL, verbose = TRUE, log_http_calls = FALSE, pxmdo = NULL, ... )
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 |
... |
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.
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.
Do a GET call to PXWEB API
pxweb_get(url, query = NULL, verbose = TRUE)pxweb_get(url, query = NULL, verbose = TRUE)
url |
a |
query |
a json string, json file or list object that can be coherced to a |
verbose |
should large queries print out progress. |
## 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 downloaded json query jstat2_url <- "https://pxdata.stat.fi:443/PxWeb/api/v1/fi/StatFin/eot/statfin_eot_pxt_132a.px" jstat2_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "sq-api_table_statfin_eot_pxt_132a.px.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 downloaded json query jstat2_url <- "https://pxdata.stat.fi:443/PxWeb/api/v1/fi/StatFin/eot/statfin_eot_pxt_132a.px" jstat2_query <- file.path( system.file(package = "pxweb"), "extdata", "examples", "sq-api_table_statfin_eot_pxt_132a.px.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" )pxweb_get_data( url, query, verbose = TRUE, column.name.type = "text", variable.value.type = "text" )
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 |
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.
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 |
The function returns a list with three slots:
url: The URL to the data
query: The query to access the data
data: The downloaded data (if chosen to download data)
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
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)