| Title: | Download Data from the Office for National Statistics |
| Version: | 0.1.0 |
| Description: | Provides functions to download and tidy statistical data published by the Office for National Statistics https://www.ons.gov.uk. Covers GDP, inflation (CPI, CPIH, RPI), unemployment, employment, wages, trade, retail sales, house prices, productivity, population, and public sector finances. Most series are fetched from the ONS website using its CSV time series endpoint. House price data is sourced from HM Land Registry https://www.gov.uk/government/organisations/land-registry. Data is cached locally between sessions. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| Language: | en-GB |
| RoxygenNote: | 7.3.3 |
| Imports: | cli (≥ 3.6.0), httr2 (≥ 1.0.0), tools |
| Suggests: | testthat (≥ 3.0.0) |
| Config/testthat/edition: | 3 |
| URL: | https://github.com/charlescoverdale/ons |
| BugReports: | https://github.com/charlescoverdale/ons/issues |
| Depends: | R (≥ 4.1.0) |
| NeedsCompilation: | no |
| Packaged: | 2026-03-08 18:34:01 UTC; charlescoverdale |
| Author: | Charles Coverdale [aut, cre] |
| Maintainer: | Charles Coverdale <charlesfcoverdale@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-03-12 09:00:02 UTC |
ons: Download Data from the Office for National Statistics
Description
Provides functions to download and tidy statistical data published by the Office for National Statistics https://www.ons.gov.uk. Covers GDP, inflation (CPI, CPIH, RPI), unemployment, employment, wages, trade, retail sales, house prices, productivity, population, and public sector finances. Most series are fetched from the ONS website using its CSV time series endpoint. House price data is sourced from HM Land Registry https://www.gov.uk/government/organisations/land-registry. Data is cached locally between sessions.
Author(s)
Maintainer: Charles Coverdale charlesfcoverdale@gmail.com
See Also
Useful links:
Report bugs at https://github.com/charlescoverdale/ons/issues
Clear the local download cache
Description
Removes cached data files stored by this package. By default all cached
files are deleted. Use max_age_days to remove only files older than a
given number of days.
Usage
clear_cache(max_age_days = NULL)
Arguments
max_age_days |
Numeric. If supplied, only files older than this many
days are removed. If |
Value
Invisibly returns NULL. Called for its side effect of deleting
cached files.
Examples
clear_cache()
Download consumer price inflation
Description
Downloads CPI, CPIH, or RPI data from the ONS. Returns either the annual rate of change or the index level.
Usage
ons_cpi(
measure = c("cpi", "cpih", "rpi"),
type = c("rate", "index"),
from = NULL,
to = NULL,
cache = TRUE
)
Arguments
measure |
Character. One of |
type |
Character. One of |
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the month.
- value
Numeric. Inflation rate (percent) or index level.
Source
Examples
ons_cpi(from = "2020-01-01")
Download employment rate
Description
Downloads the UK employment rate (aged 16 to 64, seasonally adjusted) from the ONS Labour Market Statistics. Available from 1971.
Usage
ons_employment(
group = c("total", "male", "female"),
from = NULL,
to = NULL,
cache = TRUE
)
Arguments
group |
Character. One of |
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the period.
- value
Numeric. Employment rate (percent).
Source
Examples
ons_employment(from = "2020-01-01")
Download GDP data
Description
Downloads quarterly Gross Domestic Product data from the ONS. Available from 1948 (levels) or 1955 (growth rates).
Usage
ons_gdp(
measure = c("growth", "yoy", "level", "nominal"),
from = NULL,
to = NULL,
cache = TRUE
)
Arguments
measure |
Character. One of |
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the quarter.
- value
Numeric. GDP value (percent for growth measures, GBP millions for level measures).
Source
Examples
ons_gdp(from = "2020-01-01")
Download any ONS time series by CDID code
Description
Fetches a time series from the ONS website using its CDID (four-character series identifier). The function uses the ONS Search API to discover the download path, then retrieves the CSV.
Usage
ons_get(cdid, from = NULL, to = NULL, cache = TRUE)
Arguments
cdid |
Character vector. One or more ONS CDID codes (e.g. |
from |
Date or character (YYYY-MM-DD). Start date filter. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date filter. Defaults to
|
cache |
Logical. Use cached data if available (default |
Details
CDID codes can be found by browsing the ONS time series explorer at
https://www.ons.gov.uk/timeseriestool or by using ons_search().
Value
A data frame with columns:
- date
Date. Observation date (first day of the period).
- cdid
Character. The CDID code.
- value
Numeric. Observation value.
Source
Examples
ons_get("IHYQ", from = "2020-01-01")
Download UK house prices
Description
Downloads UK average house prices from the HM Land Registry UK House Price Index. Available from 1968. Data is jointly produced by the ONS and HM Land Registry.
Usage
ons_house_prices(from = NULL, to = NULL, cache = TRUE)
Arguments
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Details
The function automatically finds the latest available monthly file from HM Land Registry, working backwards from the current month.
Value
A data frame with columns:
- date
Date. First day of the month.
- average_price_gbp
Numeric. Average house price (GBP).
- annual_change_pct
Numeric. Annual percentage change.
Source
https://www.gov.uk/government/collections/uk-house-price-index-reports
Examples
ons_house_prices(from = "2020-01-01")
Download economic inactivity
Description
Downloads the number of economically inactive people (aged 16 to 64, seasonally adjusted) from the ONS Labour Market Statistics. Available from 1971.
Usage
ons_inactivity(from = NULL, to = NULL, cache = TRUE)
Arguments
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the period.
- value
Numeric. Economically inactive people (thousands).
Source
Examples
ons_inactivity(from = "2020-01-01")
Download monthly GDP estimate
Description
Downloads the monthly estimate of GDP from the ONS. This is an experimental index-based estimate, available from 1997.
Usage
ons_monthly_gdp(from = NULL, to = NULL, cache = TRUE)
Arguments
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the month.
- value
Numeric. Monthly GDP index (2019 = 100).
Source
Examples
ons_monthly_gdp(from = "2020-01-01")
Download population estimates
Description
Downloads mid-year population estimates for the United Kingdom from the ONS. Returns annual estimates.
Usage
ons_population(from = NULL, to = NULL, cache = TRUE)
Arguments
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. Mid-year date (represented as 1 January).
- value
Numeric. Population estimate (thousands).
Source
Examples
ons_population(from = "2000-01-01")
Download labour productivity
Description
Downloads quarterly labour productivity data from the ONS. Available as output per hour worked or output per worker.
Usage
ons_productivity(
measure = c("per_hour", "per_worker"),
from = NULL,
to = NULL,
cache = TRUE
)
Arguments
measure |
Character. One of |
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the quarter.
- value
Numeric. Productivity index.
Source
Examples
ons_productivity(from = "2020-01-01")
Download public sector finances
Description
Downloads public sector net borrowing (excluding public sector banks) from the ONS. Available from 1993.
Usage
ons_public_finances(from = NULL, to = NULL, cache = TRUE)
Arguments
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the month.
- value
Numeric. Public sector net borrowing (GBP millions).
Source
Examples
ons_public_finances(from = "2020-01-01")
Download retail sales index
Description
Downloads the Retail Sales Index from the ONS. Returns volume (quantity) or value (amount spent) indices.
Usage
ons_retail_sales(
type = c("volume", "value"),
from = NULL,
to = NULL,
cache = TRUE
)
Arguments
type |
Character. One of |
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the month.
- value
Numeric. Retail sales index.
Source
Examples
ons_retail_sales(from = "2020-01-01")
Search for ONS time series
Description
Searches the ONS catalogue for time series matching a keyword or phrase.
Returns CDID codes, titles, and dataset information that can be used with
ons_get().
Usage
ons_search(query, limit = 10L)
Arguments
query |
Character. Search term (e.g. |
limit |
Integer. Maximum number of results to return (default 10, maximum 50). |
Value
A data frame with columns:
- cdid
Character. The four-character CDID code.
- dataset_id
Character. The ONS dataset identifier.
- title
Character. Description of the series.
- release_date
Character. Date of the most recent release.
Source
Examples
ons_search("GDP growth")
Download UK trade data
Description
Downloads UK trade in goods and services data from the ONS. Available from 1948.
Usage
ons_trade(
measure = c("balance", "exports", "imports"),
from = NULL,
to = NULL,
cache = TRUE
)
Arguments
measure |
Character. One of |
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the month.
- value
Numeric. Trade value (GBP millions).
Source
Examples
ons_trade(from = "2020-01-01")
Download unemployment rate
Description
Downloads the UK unemployment rate (aged 16 and over, seasonally adjusted) from the ONS Labour Market Statistics. Available from 1971.
Usage
ons_unemployment(from = NULL, to = NULL, cache = TRUE)
Arguments
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the period.
- value
Numeric. Unemployment rate (percent).
Source
Examples
ons_unemployment(from = "2020-01-01")
Download average weekly earnings
Description
Downloads average weekly earnings (AWE) for the whole economy from the ONS Labour Market Statistics. Available from 2000.
Usage
ons_wages(
measure = c("level", "growth", "real"),
from = NULL,
to = NULL,
cache = TRUE
)
Arguments
measure |
Character. One of |
from |
Date or character (YYYY-MM-DD). Start date. Defaults to
|
to |
Date or character (YYYY-MM-DD). End date. Defaults to |
cache |
Logical. Use cached data if available (default |
Value
A data frame with columns:
- date
Date. First day of the month.
- value
Numeric. Earnings level (GBP), growth rate (percent), or real index.
Source
Examples
ons_wages(from = "2020-01-01")