Type: Package
Title: R Interface to PXWEB APIs
Version: 0.17.0
Date: 2024-01-28
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.
License: BSD_2_clause + file LICENSE
URL: https://github.com/rOpenGov/pxweb/, https://ropengov.github.io/pxweb/, https://github.com/rOpenGov/pxweb
BugReports: https://github.com/rOpenGov/pxweb/issues
Depends: R (≥ 3.5.0)
Imports: checkmate, curl, httr (≥ 1.1), jsonlite
Suggests: digest, httptest, knitr, remotes, rmarkdown, testthat (≥ 0.11), xml2
VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 7.2.3
X-schema.org-isPartOf: http://ropengov.org/
X-schema.org-keywords: ropengov
NeedsCompilation: no
Packaged: 2024-01-28 12:57:36 UTC; manma760
Author: Mans Magnusson ORCID iD [aut, cre], Markus Kainu [aut], Janne Huovari [aut], Leo Lahti ORCID iD [aut], 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 <mons.magnusson@gmail.com>
Repository: CRAN
Date/Publication: 2024-01-29 08:50:02 UTC

Interface to PX-WEB APIs from R

Description

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.

References

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/


Add default values to pxe

Description

Add default values to pxe

Usage

add_pxe_defaults(pxe)

Arguments

pxe

a pxweb_explorer object


Defunct functions

Description

These function has as from version 0.10.0 become defunct. Call the functions to get information on new functions to use.

Usage

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)

Arguments

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.

Format

An object of class list of length 1.


Assert that x is a correct pxweb_database_list object.

Description

Assert that x is a correct pxweb_database_list object.

Usage

as_pxweb_levels(x)

Arguments

x

an object to check.


Assert that the url structure is correct

Description

Assert that the url slot in the pxweb object is correct.

Usage

assert_pxweb(x)

Arguments

x

a object to assert


Assert a pxweb_api_catalogue object

Description

Assert a pxweb_api_catalogue object

Usage

assert_pxweb_api_catalogue(x)

Arguments

x

an object to assert is a pxweb_api_catalogue_entry.


Assert that the rda_file_path is correct

Description

Assert that the rda_file_path slot in the pxweb object is correct.

Usage

assert_pxweb_calls(x)

Arguments

x

a object to assert


Assert that the config slot is correct

Description

Assert that the config slot in the pxweb object is correct.

Usage

assert_pxweb_config(x)

Arguments

x

a object to assert


Assert that x is a correct pxweb_data object.

Description

Assert that x is a correct pxweb_data object.

Usage

assert_pxweb_data(x)

Arguments

x

an object to check.


Assert that x is a correct pxweb_data_comments object.

Description

Assert that x is a correct pxweb_data_comments object.

Usage

assert_pxweb_data_comments(x)

Arguments

x

an object to check.


Assert that x is a correct pxweb_data_jsonstat object. Assert a json-stat version 1.0 or later object

Description

Assert that x is a correct pxweb_data_jsonstat object. Assert a json-stat version 1.0 or later object

Usage

assert_pxweb_data_jsonstat(x)

Arguments

x

an object to check.


Assert that x is a correct pxweb_database_list object.

Description

Assert that x is a correct pxweb_database_list object.

Usage

assert_pxweb_database_list(x)

Arguments

x

an object to check.


Assert a pxweb_input_allowed object

Description

Assert a pxweb_input_allowed object

Usage

assert_pxweb_input_allowed(x)

## S3 method for class 'pxweb_input_allowed'
print(x, ...)

Arguments

x

an object to assert.


Assert that x is a correct pxweb_levels object.

Description

Assert that x is a correct pxweb_levels object.

Usage

assert_pxweb_levels(x)

Arguments

x

an object to check.


Assert that x is a correct pxweb_metadata object.

Description

Assert that x is a correct pxweb_metadata object.

Usage

assert_pxweb_metadata(x)

Arguments

x

an object to check.


Assert a pxweb_query object

Description

Assert a pxweb_query object

Usage

assert_pxweb_query(x, check_response_format = TRUE)

Arguments

x

an object to assert conferms to the structure of an pxweb_query object.


Assert that the rda_file_path is correct

Description

Assert that the rda_file_path slot in the pxweb object is correct.

Usage

assert_pxweb_rda_file_path(x)

Arguments

x

a object to assert


Assert that the url structure is correct

Description

Assert that the url slot in the pxweb object is correct.

Usage

assert_pxweb_url(x)

Arguments

x

a object to assert


Assert that a given pxweb query can be split

Description

Assert that a given pxweb query can be split

Usage

assert_query_can_be_split_to_batches(pxq, pxmd, mxv)

Arguments

pxq

a [pxweb_query] object

pxmd

a [pxweb_metadata] object

mxv

maximum batch size


Build or get the tmp_rda_file_path from an url or pxweb_api_s3 object

Description

Build or get the tmp_rda_file_path from an url or pxweb_api_s3 object

Usage

build_pxweb_rda_file_path(x)

## S3 method for class 'character'
build_pxweb_rda_file_path(x)

## S3 method for class 'url'
build_pxweb_rda_file_path(x)

## S3 method for class 'pxweb'
build_pxweb_rda_file_path(x)

## S3 method for class 'list'
build_pxweb_rda_file_path(x)

Arguments

x

object to create tmp_file_path for.

Details

The hostname is used to


Build the url to a PXWEB api

Description

Build the url to a PXWEB api

Usage

build_pxweb_url(x)

## S3 method for class 'list'
build_pxweb_url(x)

## S3 method for class 'character'
build_pxweb_url(x)

## S3 method for class 'pxweb'
build_pxweb_url(x)

## S3 method for class 'pxweb_api_catalogue_entry'
build_pxweb_url(x)

Build the url to get the config from a PXWEB api

Description

Build the url to get the config from a PXWEB api

Usage

## S3 method for class 'url'
build_pxweb_url(x)

build_pxweb_config_url(x)

## S3 method for class 'list'
build_pxweb_config_url(x)

## S3 method for class 'pxweb'
build_pxweb_config_url(x)

## S3 method for class 'url'
build_pxweb_config_url(x)

Generate batch permutations

Description

Generate batch permutations

Usage

generate_permutations(x)

Arguments

x

a vector with elements to permute

Details

Generates permutations of dim. If more than 6 dim (highly unlikely) a sample of 1000 combinations is drawn. Otherwise all possible permutations are returned.


http_was_redirected

Description

http_was_redirected

Usage

http_was_redirected(r)

Arguments

r

an httr response object, e.g. from a call to httr::GET()

Value

list with slots was_redirected, redirected_from and redirected_to

References

Function in large parts taken from https://petermeissner.de/blog/2018/11/07/using-httr-to-detect-redirects/.

Examples

## Not run: 
r <- httr::GET("http://httpbin.org/redirect/2")
pxweb:::http_was_redirected(r)

## End(Not run)

Check if a response is a pxweb config response

Description

Check if a response is a pxweb config response

Usage

is_pxweb_config_response(x)

Arguments

x

a response object


Parse a character string or throws error if it fails

Description

Parse a character string or throws error if it fails

Usage

parse_url_or_fail(x)

Arguments

x

a character element to parse


Generate permutations of dimensions to find optimal no of batches

Description

Generate permutations of dimensions to find optimal no of batches

Usage

permutations(n, r, v = 1:n, set = TRUE, repeats.allowed = FALSE)

Arguments

n

See gtools::permutations.

r

See gtools::permutations.

v

See gtools::permutations.

set

See gtools::permutations.

repeats.allowed

See gtools::permutations.

Details

Taken from gtools to minimize dependencies. See permutations of the gtools packages for details


Print a catalogue entry

Description

Print a catalogue entry

Usage

## S3 method for class 'pxweb_api_catalogue_entry'
print(x, ...)

Arguments

x

an object used to select a method.

...

further arguments passed to or from other methods.


Convert a pxweb data objects values to valuetext

Description

Convert a pxweb data objects values to valuetext

Usage

pxd_values_to_valuetexts(x, variable_code, variable_vector)

Arguments

x

a pxweb_data object

variable_code

the variable name of the variable to convert. NOTE! Need to be the variable code.

variable_vector

the vector with the variables to convert.


Defines allowed input for a position in a pxweb_explorer or character object.

Description

Defines allowed input for a position in a pxweb_explorer or character object.

Usage

pxe_allowed_input(x)

pxe_allowed_input_df()

## S3 method for class 'pxweb_explorer'
pxe_allowed_input(x)

## S3 method for class 'character'
pxe_allowed_input(x)

## S3 method for class 'pxweb_explorer'
pxe_allowed_input(x)

Arguments

x

a object to get allowed input for.


Move in the pxweb_explorer position

Description

Move in the pxweb_explorer position

Usage

pxe_back_position(pxe)

pxe_add_position(pxe, new_pos)

Arguments

pxe

a pxweb_explorer object.

new_pos

add a new position.

Details

pxe_back_position moves back one position and pxe_add_position moves forward, based on user choice.


Get the url to a table

Description

Get the url to a table

Usage

pxe_data_url(x)

Arguments

x

a pxweb_explorer object


Handle a user input for a pxweb_explorer object.

Description

Handle a user input for a pxweb_explorer object.

Usage

pxe_handle_input(user_input, pxe)

## S3 method for class 'numeric'
pxe_handle_input(user_input, pxe)

## S3 method for class 'character'
pxe_handle_input(user_input, pxe)

Arguments

user_input

an (allowed) user input to handle.

pxe

a pxweb_explorer object to get user input for.

See Also

pxe_allowed_input()


Get (allowed) inputs for a pxweb_input_allowed object.

Description

Get (allowed) inputs for a pxweb_input_allowed object.

Usage

pxe_input(allowed_input, title = NULL, test_input = NULL)

pxe_parse_input(user_input, allowed_input)

Arguments

allowed_input

a pxweb_input_allowed.

title

Print (using cat()) before ask for the allowed choices.

test_input

supplying a test input (for testing only)

Details

It handles input and checks if the input is allowed.


Ask to download and download data

Description

Ask to download and download data

Usage

pxe_interactive_get_data(pxe, test_input = NULL)

Arguments

pxe

a pxweb_explorer object with full query

test_input

a test input for testing the function. Since two question, supply a vector of length two.


Get and set pxe_metadata_coices

Description

Get and set pxe_metadata_coices

Usage

pxe_metadata_choices(x)

pxe_metadata_choices(x) <- value

Arguments

x

a pxweb_explorer object

value

an object to set as pxe_metadata_choice


Get the meta data variable names from a pxweb_explorer object.

Description

Get the meta data variable names from a pxweb_explorer object.

Usage

pxe_metadata_variable_names(x)

Arguments

x

a pxweb_explorer object


How many choices has the current position?

Description

How many choices has the current position?

Usage

pxe_position_choice_size(x)

pxe_position_print_size(x)

Arguments

x

a pxweb_explorer object to check.


Is the current position an api_catalogue position?

Description

Is the current position an api_catalogue position?

Usage

pxe_position_is_api_catalogue(x)

Arguments

x

a pxweb_explorer object to check.


Is the current position a full query (i.e. choices for all metadata variables)?

Description

Is the current position a full query (i.e. choices for all metadata variables)?

Usage

pxe_position_is_full_query(x)

Arguments

x

a pxweb_explorer object to check.


Is the current position a metadata object?

Description

Is the current position a metadata object?

Usage

pxe_position_is_metadata(x)

Arguments

x

a pxweb_explorer object to check.


Are multiple choices allowed?

Description

Are multiple choices allowed?

Usage

pxe_position_multiple_choice_allowed(x)

Arguments

x

a pxweb_explorer object to check.


Get the table title for the current position

Description

Get the table title for the current position

Usage

pxe_position_title(x)

Arguments

x

a pxweb_explorer object.


Can the variable at the current position be eliminated?

Description

Can the variable at the current position be eliminated?

Usage

pxe_position_variable_can_be_eliminated(x)

Arguments

x

a pxweb_explorer object to check.


Print code to download query

Description

Print code to download query

Usage

pxe_print_download_code(pxe, as)

Arguments

pxe

a pxweb_query object.

as

json or r.


Return the pxweb object at the current position

Description

Return the pxweb object at the current position

Usage

pxe_pxobj_at_position(x)

pxe_pxobj_at_position(x) <- value

Arguments

x

a pxweb_explorer object.


S3 constructor for pxweb api object.

Description

The pxwebapi object contain all information to do calls to the pxweb api and keep count of the number of calls. The object is constructed The object will also be cached in R temp folder to minimize calls to api. All urls should be passed through the constructor to set up the pxweb api config.

Garantuees: The base_url has been pinged The sub_path has been checked The config has been captured from the API The url has been checked to be a pxweb api (through config)

Usage

pxweb(url)

is.pxweb(x)

## S3 method for class 'pxweb'
print(x, ...)

Arguments

url

an url to a pxweb api including language and version of the api. See examples.

x

an an object to test if it is a pxweb object.

...

further arguments supplied to print().

Value

A pxweb object.

Examples

## Not run: 
pxapi_1 <-
  pxweb("https://api.scb.se/OV0104/v1/doris/sv/ssd/START/ME/ME0104/ME0104C/ME0104T24")
pxapi_2 <-
  pxweb(url = "https://api.scb.se/OV0104/v1/doris/sv")

## End(Not run)


Add the subpath slot to a pxweb path slot

Description

Add the subpath slot to a pxweb path slot

Usage

pxweb_add_api_subpath(obj)

Arguments

obj

an object to add subpath to

Details

Queries the path from pos 1 and up until a config is returned.


Add an api call to a pxweb_api_s3 object

Description

The pxweb_add_call function add a call the the api in the call stack, then compute

Promise: The stored rda api object will always have the latest calls This is not thread safe so only one session at a time should call the api.

Usage

pxweb_add_call(obj, time_stamp = Sys.time())

Arguments

obj

a pxweb_api_s3 object


Add the config slot to a pxweb object

Description

Add the config slot to a pxweb object

Usage

pxweb_add_config(obj)

Arguments

obj

an object to add config to

Details

Checks if there exist a config object in the object. Otherwise it query the api to get it and add that call to the call stack.


Add mandatory variables to query

Description

Add mandatory variables to query

Usage

pxweb_add_mandatory_variables(pxq, pxmd)

Arguments

pxq

a pxweb_query object.

pxmd

a pxweb_metadata object.

Details

Complement queries that lack explicit requests for variables with requests for every value of these variables.


Add and remove metadata to query

Description

Add and remove metadata to query

Usage

pxweb_add_metadata_to_query(pxq, pxmd)

pxweb_remove_metadata_from_query(pxq, pxmd)

Arguments

pxq

a pxweb_query object.

pxmd

a pxweb_metadata object.

Details

Add metadata values to variables with a query with filter "all".


Do a GET call to PXWEB API for advanced users

Description

Do a GET call to PXWEB API for advanced users

Usage

pxweb_advanced_get(
  url,
  query = NULL,
  verbose = TRUE,
  log_http_calls = FALSE,
  pxmdo = NULL,
  ...
)

Arguments

url

a pxweb object or url that can be coherced to a pxweb object.

query

a json string, json file or list object that can be coherced to a pxweb_query object.

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 pxweb_metadata object to use for query.

...

Further arguments sent to httr::POST (for queries) or httr::GET (for query = NULL). If used with query, also supply a pxweb_metadata object. Otherwise the same parameters are sent to both httr::POST and httr::GET.

Details

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.


Get the PXWEB API catalogue

Description

Get the PXWEB API catalogue

Usage

pxweb_api_catalogue()

pxweb_api_catalogue_from_json(json)

pxweb_api_catalogue_from_github(branch = "master")

pxweb_api_catalogue_path()

Details

A list with implemented API:s.

Examples

pxweb_api_catalogue()


Constructor for pxweb_api_catalogue_entry.

Description

Constructor for pxweb_api_catalogue_entry.

Usage

pxweb_api_catalogue_entry(x)

## S3 method for class 'list'
pxweb_api_catalogue_entry(x)

assert_pxweb_api_catalogue_entry(x)

Arguments

x

an object to assert is a pxweb_api_catalogue_entry.


Get the api name, rootpath, subpath, path or dbpath

Description

Get the api name, rootpath, subpath, path or dbpath

Usage

pxweb_api_name(x)

## S3 method for class 'url'
pxweb_api_name(x)

## S3 method for class 'pxweb'
pxweb_api_name(x)

## S3 method for class 'pxweb_api_catalogue_entry'
pxweb_api_name(x)

## S3 method for class 'pxweb_explorer'
pxweb_api_name(x)

pxweb_api_rootpath(x)

## S3 method for class 'url'
pxweb_api_rootpath(x)

## S3 method for class 'pxweb'
pxweb_api_rootpath(x)

## S3 method for class 'pxweb_explorer'
pxweb_api_rootpath(x)

pxweb_api_subpath(x, init_slash = TRUE, as_vector = FALSE)

## S3 method for class 'pxweb'
pxweb_api_subpath(x, init_slash = TRUE, as_vector = FALSE)

## S3 method for class 'pxweb_explorer'
pxweb_api_subpath(x, init_slash = TRUE, as_vector = FALSE)

pxweb_api_path(x, init_slash = TRUE, as_vector = FALSE)

## S3 method for class 'url'
pxweb_api_path(x, init_slash = TRUE, as_vector = FALSE)

## S3 method for class 'pxweb'
pxweb_api_path(x, init_slash = TRUE, as_vector = FALSE)

## S3 method for class 'pxweb_explorer'
pxweb_api_path(x, init_slash = TRUE, as_vector = FALSE)

pxweb_api_dbpath(x, init_slash = TRUE, as_vector = FALSE)

## S3 method for class 'pxweb'
pxweb_api_dbpath(x, init_slash = TRUE, as_vector = FALSE)

## S3 method for class 'pxweb_explorer'
pxweb_api_path(x, init_slash = TRUE, as_vector = FALSE)

assert_path(x)

pxe_position_path(
  x,
  init_slash = TRUE,
  as_vector = FALSE,
  include_rootpath = FALSE
)

pxe_metadata_path(x, as_vector = FALSE)

Arguments

x

object to get the name or path for

init_slash

should subpath and path start with a /. Default is TRUE.

as_vector

should subpath and path be a vector split by /. Default is FALSE.

include_rootpath

Should the rootpath be included? Default is FALSE

Details

The PXWEB API contain the following path: API-NAME/API-VERSION/LANGUAGE/DATABASE-ID/<LEVELS>/TABLE-ID

The full url is made up by the rootpath, subpath, and path. The rootpath is made up of the protocol and the API-NAME / hostname and protocol (if any).

The subpath contain the API-VERSION and LANGUAGE but can contain other parts as well. The subpath is the shortest the config can be called for. It can be seen as the base for the API.

The dbpath, the data base path, contain DATABASE-ID/<LEVELS>/TABLE-ID.

The path, is the standar path of an url, i.e. subpath + dbpath.

No path ends with slash, but subpath and dbpath may begin with slash, see the parameters


Coerce a pxweb_data object to a data.frame

Description

Coerce a pxweb_data object to a data.frame

Usage

pxweb_as_data_frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE,
  column.name.type = "text",
  variable.value.type = "text"
)

## S3 method for class 'pxweb_data'
pxweb_as_data_frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE,
  column.name.type = "text",
  variable.value.type = "text"
)

## S3 method for class 'pxweb_data_comments'
pxweb_as_data_frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE
)

## S3 method for class 'pxweb_data_comment'
pxweb_as_data_frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE
)

## S3 method for class 'pxweb_levels'
pxweb_as_data_frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE
)

## S3 method for class 'pxweb_data'
as.data.frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE,
  column.name.type = "text",
  variable.value.type = "text"
)

## S3 method for class 'pxweb_data_comments'
as.data.frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE
)

## S3 method for class 'pxweb_levels'
as.data.frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE
)

## S3 method for class 'pxweb_metadata'
as.data.frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE
)

pxweb_as_matrix(
  x,
  row.names = NULL,
  column.name.type = "text",
  variable.value.type = "text"
)

## S3 method for class 'pxweb_data'
pxweb_as_matrix(
  x,
  row.names = NULL,
  column.name.type = "text",
  variable.value.type = "text"
)

## S3 method for class 'pxweb_data'
as.matrix(
  x,
  ...,
  row.names = NULL,
  column.name.type = "text",
  variable.value.type = "text"
)

pxweb_pxd_slot_idx_pos(x)

Arguments

x

an object to convert to data.frame.

row.names

See as.data.frame.

optional

See as.data.frame.

...

See as.data.frame.

stringsAsFactors

See as.data.frame.

column.name.type

character: should code or text be used as column names?

variable.value.type

character: should code or text be used as values in columns?

See Also

as.data.frame.


Convert object to json

Description

Convert object to json

Usage

pxweb_as_json(x)

## S3 method for class 'pxweb_query'
pxweb_as_json(x)

Arguments

x

an object to convert.


Combine pxweb objects

Description

Combine pxweb objects

Usage

pxweb_c(x)

Arguments

x

a list with pxweb objects.


Cite a PXWEB data object

Description

Cite a PXWEB data object

Usage

pxweb_cite(x, style = "citation")

Arguments

x

a pxweb_data object to cite.

style

see bibentry.

Details

Functionality to automatic cite PXWEB data objects.


Clear cache of all (or one) pxweb object

Description

Clear cache of all (or one) pxweb object

Usage

pxweb_clear_cache(x = NULL)

Arguments

x

a pxweb object to clear cache for. Default is NULL (clear everything).

Details

Clean the cache and sleep to restore api timing limit.


Construct a pxweb_data object.

Description

An object that contain the data for a given PXWEB table.

Usage

pxweb_data(x)

Arguments

x

a list returned from a PXWEB API to convert to a pxweb_data object.

Value

a pxweb_data object


Combine pxweb objects

Description

Combine pxweb objects

Usage

pxweb_data_c(x)

Arguments

x

a list with pxweb objects.


Get query filter

Description

Get query filter

Usage

pxweb_data_colnames(pxd, type = "text")

Arguments

pxd

a pxweb_data object.

Value

character vector with column names.


Construct a pxweb_data_comment object

Description

Construct a pxweb_data_comment object

Usage

pxweb_data_column_comment(x, column_idx)

pxweb_data_value_comment(x, comment_idx)

pxweb_data_obs_comment(x, obs_idx)

Arguments

x

an pxweb_data to extract a pxweb_data_comment object from.

column_idx

the index of the column to extract.

comment_idx

the index of the comment to extract.

obs_idx

the index of the comment to extract.


Construct a pxweb_data_comments object.

Description

An object that contain the comments for a given PXWEB table.

Usage

pxweb_data_comments(x)

## S3 method for class 'pxweb_data'
pxweb_data_comments(x)

Arguments

x

a pxweb_data object.

Value

a pxweb_data_comments object

Examples

## Not run: 
url <- "https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/BefolkningNy"
json_query <-
  file.path(system.file(package = "pxweb"), "extdata", "examples", "json_query_example.json")
pxd <- pxweb_get(url = url, query = json_query)
pxdcs <- pxweb_data_comments(x = pxd)
pxdc_df <- as.data.frame(pxdcs, stringsAsFactors = TRUE)

## End(Not run)

Compute the dimension of the query pxweb_data object

Description

Compute the dimension of the query pxweb_data object

Usage

pxweb_data_dim(pxd)

Arguments

pxd

a pxweb_data object.


Construct a pxweb_data_jsonstat object.

Description

An object that contain the data for a given PXWEB table.

Usage

pxweb_data_jsonstat(x)

Arguments

x

a list returned from a PXWEB API to convert to a pxweb_data_jsonstat object.

Value

a pxweb_data_jsonstat object.


Construct a pxweb_database_list object.

Description

An object that contain the list of databases for a given PXWEB api.

Usage

pxweb_database_list(x)

Arguments

x

a list returned from a PXWEB API to convert to a pxweb_database_list object.

Value

a pxweb_database_list object


Create a pxweb_explorer object.

Description

position the current position in the api, as a character vector from the root. Note position is not alway a correct url. Metadata and other choices are part of position

root is the bottom path (as position) that the user can go. If length 0, user can essentially go to hostname.

paste(root_path + position, collapse = "/") is used to construct the path to the position in case of url.

print out a bar for separation purposes

Usage

pxweb_explorer(x = NULL)

## S3 method for class ''NULL''
pxweb_explorer(x)

## S3 method for class 'character'
pxweb_explorer(x)

## S3 method for class 'pxweb'
pxweb_explorer(x)

## S3 method for class 'pxweb_api_catalogue_entry'
pxweb_explorer(x)

assert_pxweb_explorer(x)

## S3 method for class 'pxweb_explorer'
print(x, ...)

print_bar()

pxe_print_choices(x)

Arguments

x

a pxweb object, a PXWEB url, NULL or an api in the api catalogue.

Examples

## The functions below are internal generic functions
## x <- pxweb_explorer()
## url <- "api.scb.se"
## x <- pxweb_explorer(x = url)
## url <- "https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/"
## x <- pxweb_explorer(x = url)
## url <- "https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/BefolkningNy"
## x <- pxweb_explorer(x = url)


Fix url characters

Description

Fix url characters

Usage

pxweb_fix_url(x)

Arguments

x

a string to fix


Do a GET call to PXWEB API

Description

Do a GET call to PXWEB API

Usage

pxweb_get(url, query = NULL, verbose = TRUE)

Arguments

url

a pxweb object or url that can be coherced to a pxweb object.

query

a json string, json file or list object that can be coherced to a pxweb_query object.

verbose

should large queries print out progress.

Examples

## 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)

## End(Not run)


Build api test data.frame

Description

Build api test data.frame

Usage

pxweb_get_api_test_data_frame(x)

Arguments

x

a pxweb object


Do a GET call to PXWEB API and return a data.frame

Description

Do a GET call to PXWEB API and return a data.frame

Usage

pxweb_get_data(
  url,
  query,
  verbose = TRUE,
  column.name.type = "text",
  variable.value.type = "text"
)

Arguments

url

a pxweb object or url that can be coherced to a pxweb object.

query

a json string, json file or list object that can be coherced to a pxweb_query object.

verbose

should large queries print out progress.

column.name.type

character: should code or text be used as column names?

variable.value.type

character: should code or text be used as values in columns?

Details

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 Also

See pxweb_get for mor general usage and pxweb_query for details on PXWEB queries.

Examples

## 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)


Setup a structure to log all API calls

Description

Setup a structure to log all API calls

Usage

pxweb_http_log_on(fn = "log_pxweb_api_http_calls.txt")

pxweb_http_log_off()

pxweb_log_paths_path()

pxweb_http_log_is_on()

pxweb_http_log_response(r)

pxweb_response_to_log_as_json(r)

Arguments

fn

The file name of the log file.

Details

Set up internal structure to handle http PXWEB API calls. http calls are stored in a log file. To access the path to this log file, the path is stored in the pxweb folder in the tempdir(). The path is given by pxweb_log_paths_path(). If the file exists, the http calls should be logged (appended) to the log file where the object contains the path.


Find and download data interactively from a PXWEB API

Description

Wrapper function (for pxweb_get) to simply find and download data to the current R session.

Usage

pxweb_interactive(x = NULL)

interactive_pxweb(x = NULL)

Arguments

x

The name or alias of the pxweb api to connect to, a pxweb object or an url.

Value

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)

See Also

pxweb_get

Examples

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/")


Get input from user

Description

Get input from user

Usage

pxweb_interactive_input(pxe, test_input = NULL)

Arguments

pxe

a pxweb_explorer object to get user input for.

test_input

supplying a test input (for testing only).


Construct a pxweb_levels object.

Description

An object that contain the levels for a given PXWEB api position.

Usage

pxweb_levels(x)

Arguments

x

a list returned from a PXWEB API to convert to a pxweb_levels object.

Value

a pxweb_levels object


Construct a pxweb_metadata object.

Description

An object that contain the metadata for a given PXWEB table.

Usage

pxweb_metadata(x)

Arguments

x

a list returned from a PXWEB API to convert to a pxweb_metadata object.

Value

a pxweb_metadata object


Add values to NULL value variables in PXWEB metadata objects

Description

Add values to NULL value variables in PXWEB metadata objects

Usage

pxweb_metadata_add_null_values(x, px)

Arguments

x

an object to check if is a pxweb_metadata object to which we should add values.

px

a pxweb object

Details

Some metadata objects may have NULL values. In these cases the values are downloaded and added to the metadata object.


Compue the dimension of a metadata object

Description

Compue the dimension of a metadata object

Usage

pxweb_metadata_dim(pxmd)

Arguments

pxmd

a pxweb_metadata object.


Get boolean vector

Description

Get boolean vector

Usage

pxweb_metadata_elimination(pxmd)

Arguments

pxmd

a pxweb_metadata object.

Value

pxweb_metadata eliminations as a named boolean vector.


Get boolean vector

Description

Get boolean vector

Usage

pxweb_metadata_time(pxmd)

Arguments

pxmd

a pxweb_metadata object.

Value

pxweb_metadata eliminations as a named boolean vector.


Parse the response from a PXWEB API (advanced)

Description

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.

Usage

pxweb_parse_response(x)

is_pxweb_response(x)

Arguments

x

a httr response object from a PXWEB call.


Create a PXWEB query

Description

Creates a pxweb query object from either a list with named values, a json query file or json query string. See examples below.

Usage

pxweb_query(x)

## S3 method for class 'character'
pxweb_query(x)

## S3 method for class 'json'
pxweb_query(x)

## S3 method for class 'pxweb_query'
pxweb_query(x)

## S3 method for class 'list'
pxweb_query(x)

## S3 method for class 'response'
pxweb_query(x)

## S3 method for class 'pxweb_explorer'
pxweb_query(x)

Arguments

x

an object to cast as a pxweb_query object.

See Also

pxweb_query_as_json, pxweb_query_as_rcode

Examples

dims <- list(
  Alue = c("*"),
  "Asuntokunnan koko" = c("*"),
  Talotyyppi = c("S"),
  Vuosi = c("*")
)
pxq1 <- pxweb_query(dims)

json_query <- file.path(
  system.file(package = "pxweb"),
  "extdata", "examples", "json_query_example.json"
)
pxq2 <- pxweb_query(json_query)


Convert a pxweb_query object to a json string

Description

Convert a pxweb_query object to a json string

Usage

pxweb_query_as_json(pxq, ...)

Arguments

pxq

a pxweb_query object.

...

further argument to jsonlite::toJSON().

See Also

pxweb_query, pxweb_query_as_rcode

Examples

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)


Print a pxweb_query object as R code

Description

Print a pxweb_query object as R code

Usage

pxweb_query_as_rcode(pxq)

Arguments

pxq

a pxweb_query object.

See Also

pxweb_query_as_json, pxweb_query


Compue the dimension of the query

Description

Compue the dimension of the query

Usage

pxweb_query_dim(pxq)

Arguments

pxq

a pxweb_query object.


Get vector indicating splittable variables

Description

Get vector indicating splittable variables

Usage

pxweb_query_dim_splittable(pxq, pxmd)

Arguments

pxq

a pxweb_query object.

Details

Splitable variables are variables that can be split. Content variables cannot be split, nor variables with filter == "top".

Currently, we can only be sure that time variables and eliminated variables can be split. Hopefully the next API makes this more clear.

Value

a named logical vector.


Get query values

Description

Get query values

Usage

pxweb_query_filter(pxq)

Arguments

pxq

a pxweb_query object.

Value

query variable selection filters as a named character vector.


Get query filter

Description

Get query filter

Usage

pxweb_query_values(pxq)

Arguments

pxq

a pxweb_query object.

Value

query variable selection values as a named list of character vectors.


Split query in optimal sub-queries

Description

Split query in optimal sub-queries

Usage

pxweb_split_query(pxq, px, pxmd)

Arguments

pxq

a pxweb_query object.

px

a pxweb object.

pxmd

a pxweb_metadata object.

Details

Computes (brute-force) the optimal split of a query to match the api maximum value limit. It also take into account that time variables and content variables should not be split. Also variables with filter "top" should not be split, since the top filter does not supply the individual levels, just a number. This can probably be improved further.

Value

a list with pxweb_query objects.


Setup temorary directory for the pxweb

Description

Setup temorary directory for the pxweb

Usage

pxweb_tempdir(to = "apis")

Arguments

to

to what part of the temp folder (apis or logs)


Test a full or a part of a PXWEB api.

Description

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.

Usage

pxweb_test_api(
  url,
  test_type = "first",
  n = 1,
  verbose = TRUE,
  time_limit = Inf
)

Arguments

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 first observation of each table. A random sample of size n. Download all full tables. touch the api by only downloading the first table metadata. This is minimal testing of the API.

n

sample size if test_type is sample.

verbose

The function will print information.

time_limit

Time limit in second the API is allowed to be tested.

Value

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

Examples

## 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)

Create all paths from a list of pxweb_api_catalogue entries

Description

Create all paths from a list of pxweb_api_catalogue entries

Usage

pxweb_test_create_api_paths(apis)

Arguments

apis

a list of pxweb_api_catalogue_entry elements


Test time limit object

Description

Test time limit object

Usage

pxweb_test_time_limit(time_limit)

is_test_time_limit_reached(x)

Arguments

time_limit

the number of seconds the API will be tested.

Details

Used to limit testing of API:s.


Validate a pxweb_query with a pxweb_metadata object

Description

Validate a pxweb_query with a pxweb_metadata object

Usage

pxweb_validate_query_with_metadata(pxq, pxmd)

Arguments

pxq

a pxweb_query object.

pxmd

a pxweb_metadata object.

Details

Validate a query with a metadata object to asses that the query can be used to query the table.

Examples

## 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)


Save and load pxweb objects from R temp folder

Description

Save and load pxweb objects from R temp folder

Usage

save_pxweb(obj)

load_pxweb(obj)

load_pxweb_calls(obj)

load_pxweb_config(obj)

load_pxweb_api_subpath(obj)

Arguments

obj

a pxweb object


Split variables into chunks

Description

Split variables into chunks

Usage

split_dimensions_left_right(x, bool, max_size)

Details

Splitable variables are variables that can be split. Content variables cannot be split, not variables with filter == "top"

Value

a pxweb_split_dimensions


Pad a string to a fixed size

Description

Pad a string to a fixed size

Usage

str_pad(txt, n = 5, pad = " ", type = "left")

Arguments

txt

a character vector to pad

n

final char width

pad

pad symbol

type

pad from 'left' or 'right'.


Taken from trimws for reasons of compatibility with previous R versios.

Description

Taken from trimws for reasons of compatibility with previous R versios.

Usage

str_trim(x, which = c("both", "left", "right"))

Arguments

x

a string to trim.

which

how to trim the string.

See Also

trimws