Package 'nhsbsa'

Title: Client for the NHS Business Services Authority Open Data Portal
Description: A low-level client for the National Health Service Business Services Authority (NHSBSA) Open Data Portal <https://opendata.nhsbsa.net>, a 'CKAN' data catalogue. Provides thin wrappers around the portal's API actions for listing datasets, retrieving metadata, querying the datastore and downloading resource files. Results are returned as plain data (tibbles and lists) for the caller to interpret.
Authors: Alasdair Warwick [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-0800-2890>)
Maintainer: Alasdair Warwick <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2026-10-07 11:47:31 UTC
Source: https://github.com/rmgpanw/nhsbsa

Help Index


Query a resource with SQL

Description

Wraps the CKAN datastore_search_sql action, which runs a read-only SQL query against the datastore. The sql string is sent to the API verbatim; paging is the caller's responsibility (via LIMIT/OFFSET in the query).

Usage

nhsbsa_datastore_search_sql(resource_id, sql, .return_raw = FALSE)

Arguments

resource_id

Character scalar. The resource the query targets. The NHSBSA datastore requires this alongside sql, and identifies a resource by its name (the name column of nhsbsa_list_resources()), e.g. "EPD_201401". Reference the same name in the query's FROM clause.

sql

Character scalar. A single read-only SQL SELECT statement, e.g. 'SELECT * FROM `EPD_201401` LIMIT 10'.

.return_raw

Logical. If TRUE, return the full parsed CKAN response envelope instead of the processed result. Defaults to FALSE.

Value

A tibble with one row per record returned by the query. With .return_raw = TRUE, the parsed response envelope as a list.

See Also

nhsbsa_datastore_search() for a parameterised search.

Examples

# Select specific columns
nhsbsa_datastore_search_sql(
  resource_id = "EPD_202401",
  sql = "SELECT YEAR_MONTH, PCO_CODE, ITEMS FROM `EPD_202401` LIMIT 10"
)

# Filter by value with a WHERE clause (the reliable way to filter)
nhsbsa_datastore_search_sql(
  resource_id = "EPD_202401",
  sql = "SELECT PCO_CODE, BNF_CHEMICAL_SUBSTANCE, ITEMS
         FROM `EPD_202401`
         WHERE PCO_CODE = 'W2U3Z'
         LIMIT 10"
)

# Aggregate server-side: total items prescribed per organisation
nhsbsa_datastore_search_sql(
  resource_id = "EPD_202401",
  sql = "SELECT PCO_CODE, SUM(ITEMS) AS items
         FROM `EPD_202401`
         GROUP BY PCO_CODE
         ORDER BY items DESC
         LIMIT 10"
)

Download a resource file

Description

Resolves a single resource within a dataset and streams its file to disk. This is the file-download counterpart to the datastore row-query functions (nhsbsa_datastore_search() and friends): it fetches the whole resource file (e.g. a CSV) rather than running a query.

Usage

nhsbsa_download_resource(
  dataset_id,
  directory,
  resource_id = NULL,
  pattern = NULL,
  overwrite = FALSE,
  quiet = FALSE
)

Arguments

dataset_id

Character scalar. The dataset identifier, as returned by nhsbsa_package_list().

directory

Character scalar. The directory to download into. Required; there is no default, and the directory must already exist. Use tempdir() for a throwaway location.

resource_id

Character scalar. The identifier of the resource to download. Takes precedence over pattern.

pattern

Character scalar. A regular expression matched (case-insensitively) against resource names to select a single resource.

overwrite

Logical. Overwrite the file if it already exists in directory? Defaults to FALSE, in which case the existing file is left untouched and its path returned.

quiet

Logical. Suppress informational messages? Defaults to FALSE.

Details

Exactly one resource must be identified. Supply either resource_id or a pattern that matches a single resource name; if neither is given and the dataset has more than one resource, an error is raised.

The file is saved into directory under its own name (the file name from the resource's download URL, e.g. bnf_code_current_202503_version_88.csv).

Value

The path to the downloaded file, invisibly.

See Also

nhsbsa_list_resources() to discover resources.

Examples

resources <- nhsbsa_list_resources("bnf-code-information-current-year")

# Identify a resource by a pattern matching a single resource name
path <- nhsbsa_download_resource(
  "bnf-code-information-current-year",
  pattern = resources$name[[1]],
  directory = tempdir()
)
path

# ...or by its exact id. An existing file is not re-downloaded unless
# `overwrite = TRUE`, so this call short-circuits and returns the path.
nhsbsa_download_resource(
  "bnf-code-information-current-year",
  resource_id = resources$id[[1]],
  directory = tempdir()
)

List dataset groups

Description

Wraps the CKAN group_list action to list the groups (thematic collections) that datasets can belong to.

Usage

nhsbsa_group_list(all_fields = NULL, .return_raw = FALSE)

Arguments

all_fields

Logical. If TRUE, return a richer record for each organisation rather than just its name. Defaults to the API default (FALSE) when left NULL.

.return_raw

Logical. If TRUE, return the full parsed CKAN response envelope instead of the processed result. Defaults to FALSE.

Value

A character vector of group names. If all_fields = TRUE, a list of group records. With .return_raw = TRUE, the parsed response envelope as a list.

See Also

nhsbsa_organization_list(), nhsbsa_tag_list().

Examples

nhsbsa_group_list()

List a dataset's resources as a tibble

Description

A convenience wrapper around nhsbsa_package_show() that returns a dataset's resources as a tibble, optionally filtered by a pattern matched against the resource name. This is the most direct way to discover the resources (and their download URLs) available for a dataset.

Usage

nhsbsa_list_resources(dataset_id, pattern = NULL)

Arguments

dataset_id

Character scalar. The dataset identifier, as returned by nhsbsa_package_list().

pattern

Character scalar. An optional regular expression; only resources whose name matches (case-insensitively) are returned.

Details

If you have already fetched the dataset metadata with nhsbsa_package_show(), calling tibble::as_tibble() on it gives the same table without a second request (but without the pattern filter).

Value

A tibble with one row per resource and columns name, id, format, created, last_modified, url and size.

See Also

nhsbsa_download_resource() to download a resource file; nhsbsa_package_show() (with tibble::as_tibble()) for the same table from already-fetched metadata.

Examples

# All resources for a dataset
nhsbsa_list_resources("english-prescribing-data-epd")

# Only resources whose name matches a pattern
nhsbsa_list_resources("english-prescribing-data-epd", pattern = "202401")

List publishing organisations

Description

Wraps the CKAN organization_list action to list the organisations that publish datasets on the portal.

Usage

nhsbsa_organization_list(all_fields = NULL, .return_raw = FALSE)

Arguments

all_fields

Logical. If TRUE, return a richer record for each organisation rather than just its name. Defaults to the API default (FALSE) when left NULL.

.return_raw

Logical. If TRUE, return the full parsed CKAN response envelope instead of the processed result. Defaults to FALSE.

Value

A character vector of organisation names. If all_fields = TRUE, a list of organisation records. With .return_raw = TRUE, the parsed response envelope as a list.

See Also

nhsbsa_group_list(), nhsbsa_tag_list().

Examples

# Organisation names
nhsbsa_organization_list()

# Richer records (title, description, dataset count, ...) for each organisation
orgs <- nhsbsa_organization_list(all_fields = TRUE)
orgs[[1]]$title

List available datasets

Description

Wraps the CKAN package_list action to return the identifiers of every dataset published on the NHSBSA Open Data Portal.

Usage

nhsbsa_package_list(.return_raw = FALSE)

Arguments

.return_raw

Logical. If TRUE, return the full parsed CKAN response envelope instead of the processed result. Defaults to FALSE.

Value

A character vector of dataset identifiers. With .return_raw = TRUE, the parsed response envelope as a list.

See Also

nhsbsa_package_show() for a dataset's metadata, nhsbsa_package_search() to search datasets.

Examples

datasets <- nhsbsa_package_list()
length(datasets)
head(datasets)

Show a dataset's metadata

Description

Wraps the CKAN package_show action to return the full metadata for a single dataset, including its list of resources (downloadable files and datastore tables).

Usage

nhsbsa_package_show(id, .return_raw = FALSE)

Arguments

id

Character scalar. The dataset identifier or name, as returned by nhsbsa_package_list().

.return_raw

Logical. If TRUE, return the full parsed CKAN response envelope instead of the processed result. Defaults to FALSE.

Value

A list of dataset metadata, with class nhsbsa_package and a print() method; tibble::as_tibble() turns it into a table of its resources. With .return_raw = TRUE, the parsed response envelope as a plain list.

See Also

nhsbsa_list_resources() for a tidy table of a dataset's resources.

Examples

metadata <- nhsbsa_package_show("english-prescribing-data-epd")
metadata
metadata$title

# The dataset's resources as a tibble
tibble::as_tibble(metadata)

Show a resource's metadata

Description

Wraps the CKAN resource_show action to return the metadata for a single resource (a file or datastore table).

Usage

nhsbsa_resource_show(id, .return_raw = FALSE)

Arguments

id

Character scalar. The resource identifier.

.return_raw

Logical. If TRUE, return the full parsed CKAN response envelope instead of the processed result. Defaults to FALSE.

Value

A list of resource metadata, with class nhsbsa_resource and a print() method. With .return_raw = TRUE, the parsed response envelope as a plain list.

Examples

resources <- nhsbsa_list_resources("english-prescribing-data-epd")
meta <- nhsbsa_resource_show(resources$id[[1]])
meta$name
meta$datastore_active

List tags

Description

Wraps the CKAN tag_list action to list the tags applied to datasets. These are the same tags shown on the portal website; you can filter datasets by a tag with nhsbsa_package_search() (see vignette("nhsbsa")).

Usage

nhsbsa_tag_list(query = NULL, vocabulary_id = NULL, .return_raw = FALSE)

Arguments

query

Character scalar. Restrict the results to tags containing this string.

vocabulary_id

Character scalar. Restrict the results to tags in a particular CKAN tag vocabulary.

.return_raw

Logical. If TRUE, return the full parsed CKAN response envelope instead of the processed result. Defaults to FALSE.

Value

A character vector of tags. With .return_raw = TRUE, the parsed response envelope as a list.

See Also

nhsbsa_package_search() to find datasets by tag.

Examples

# All tags
tags <- nhsbsa_tag_list()
length(tags)

# Only tags containing a given string
nhsbsa_tag_list(query = "prescribing")

Methods for nhsbsa objects

Description

Some nhsbsa functions return classed lists with a tailored print() method and a tibble::as_tibble() method for turning them into a table:

Usage

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

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

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

## S3 method for class 'nhsbsa_package'
as_tibble(x, ...)

## S3 method for class 'nhsbsa_package_search'
as_tibble(x, ...)

Arguments

x

An object returned by the relevant nhsbsa function.

...

Ignored, for S3 method consistency.

Details

These objects are still plain lists, so $, [[ and utils::str() work as usual. Pass .return_raw = TRUE to the originating function, or use unclass(), to get the underlying list without a class.

Value

The print() methods return x invisibly. The as_tibble() methods return a tibble.

Examples

pkg <- nhsbsa_package_show("english-prescribing-data-epd")
pkg
tibble::as_tibble(pkg)

hits <- nhsbsa_package_search(q = "prescribing", rows = 5)
hits
tibble::as_tibble(hits)