R/nob.R

Defines functions nob parse_nob_data nob_dimension nob_metadata nob_data

Documented in nob_data nob_dimension nob_metadata

#' Fetch Norges Bank (NoB) data
#'
#' Retrieve time series data from the Norges Bank SDMX Web Service.
#'
#' @param flow (`character(1)`)\cr
#'   The dataflow to query. See [nob_metadata()] for available dataflows.
#' @param key (`NULL` | `character(1)`)\cr
#'   The series key to query using dot-separated dimension values
#'   (e.g., `"B.USD.NOK.SP"`). Use `+` for multiple values in one dimension
#'   (e.g., `"B.USD+EUR.NOK.SP"`). If `NULL`, all data for the flow is returned.
#'   Default `NULL`.
#' @param start_period (`NULL` | `character(1)` | `integer(1)`)\cr
#'   Start date of the data (e.g., `"2024-01-01"` or `2024`). If `NULL`, no start date restriction
#'   is applied. Default `NULL`.
#' @param end_period (`NULL` | `character(1)` | `integer(1)`)\cr
#'   End date of the data, in the same format as start_period. If `NULL`, no end date restriction is
#'   applied. Default `NULL`.
#' @param first_n (`NULL` | `numeric(1)`)\cr
#'   Number of observations to retrieve from the start of the series. If `NULL`, no restriction is
#'   applied. Default `NULL`.
#' @param last_n (`NULL` | `numeric(1)`)\cr
#'   Number of observations to retrieve from the end of the series. If `NULL`, no restriction is
#'   applied. Default `NULL`.
#' @returns A [data.table::data.table()] with the requested data.
#' @source <https://www.norges-bank.no/en/topics/Statistics/open-data/>
#' @family data
#' @export
#' @examplesIf curl::has_internet()
#' \donttest{
#' # fetch USD/NOK exchange rate
#' nob_data("EXR", "B.USD.NOK.SP", last_n = 5L)
#'
#' # fetch multiple exchange rates
#' nob_data("EXR", "B.USD+EUR+GBP.NOK.SP", start_period = "2024-01-01")
#'
#' # fetch policy rate
#' nob_data("IR", last_n = 5L)
#' }
nob_data = function(
  flow,
  key = NULL,
  start_period = NULL,
  end_period = NULL,
  first_n = NULL,
  last_n = NULL
) {
  assert_string(flow, min.chars = 1L)
  assert_string(key, min.chars = 1L, null.ok = TRUE)
  assert_period(start_period)
  assert_period(end_period)
  first_n = assert_count(first_n, null.ok = TRUE, positive = TRUE, coerce = TRUE)
  last_n = assert_count(last_n, null.ok = TRUE, positive = TRUE, coerce = TRUE)

  resource = sdmx_data_resource(flow, key)
  xml = nob(
    resource,
    startPeriod = start_period,
    endPeriod = end_period,
    firstNObservations = first_n,
    lastNObservations = last_n,
    accept = "application/vnd.sdmx.genericdata+xml;version=2.1"
  )
  parse_nob_data(xml)
}

#' Fetch Norges Bank (NoB) metadata
#'
#' Retrieve metadata from the Norges Bank SDMX Web Service.
#'
#' @param type (`character(1)`)\cr
#'   The type of metadata to query.
#'   One of: `"datastructure"`, `"dataflow"`, `"codelist"`, or `"concept"`.
#' @param id (`NULL` | `character(1)`)\cr
#'   The id to query. Default `NULL`.
#' @param lang (`character(1)`)\cr
#'   Language for names, either `"en"` or `"no"`. Default `"en"`.
#' @returns A [data.table::data.table()] with the requested metadata.
#' @source <https://www.norges-bank.no/en/topics/Statistics/open-data/>
#' @family metadata
#' @export
#' @examplesIf curl::has_internet()
#' \donttest{
#' nob_metadata("dataflow")
#' nob_metadata("datastructure")
#' nob_metadata("codelist", "CL_CURRENCY")
#' }
nob_metadata = function(type, id = NULL, lang = "en") {
  assert_choice(type, c("datastructure", "dataflow", "codelist", "concept"))
  assert_string(id, min.chars = 1L, null.ok = TRUE)
  assert_choice(lang, c("en", "no"))

  meta = sdmx_metadata_type(type)
  resource = if (is.null(id)) {
    meta$resource
  } else {
    paste(meta$resource, "NB", toupper(id), sep = "/")
  }
  xml = nob(resource)
  entries = xml2::xml_find_all(xml, meta$xpath)
  sdmx_metadata(entries, lang)
}

#' Fetch Norges Bank (NoB) dimensions
#'
#' Retrieve the dimension structure for a given dataflow from the Norges Bank SDMX Web Service.
#'
#' @param id (`character(1)`)\cr
#'   The id of the data structure definition to query (e.g., `"NB_EXR"`).
#' @returns A [data.table::data.table()] with columns:
#'   \item{id}{The dimension id (e.g., `"FREQ"`, `"BASE_CUR"`)}
#'   \item{position}{The position of the dimension in the series key}
#'   \item{codelist}{The id of the associated codelist (e.g., `"CL_FREQ"`)}
#' @source <https://www.norges-bank.no/en/topics/Statistics/open-data/>
#' @family metadata
#' @export
#' @examplesIf curl::has_internet()
#' \donttest{
#' nob_dimension("DSD_EXR")
#' }
nob_dimension = function(id) {
  assert_string(id, min.chars = 1L)
  resource = paste("datastructure", "NB", toupper(id), sep = "/")
  xml = nob(resource)
  sdmx_dimension(xml)
}

parse_nob_data = function(xml) {
  series = xml2::xml_find_all(xml, ".//generic:Series")
  res = map(series, function(x) {
    series_key = x |>
      xml2::xml_find_first("./generic:SeriesKey") |>
      xml2::xml_children()
    nms = series_key |>
      xml2::xml_attr("id") |>
      tolower()
    series_key = series_key |>
      xml2::xml_attr("value") |>
      setNames(nms) |>
      as.list()

    attrs = x |>
      xml2::xml_find_first("./generic:Attributes") |>
      xml2::xml_children()
    nms = attrs |>
      xml2::xml_attr("id") |>
      tolower()
    attrs = attrs |>
      xml2::xml_attr("value") |>
      setNames(nms) |>
      as.list()

    data = c(series_key, attrs)
    data = data[names(data) %nin% c("collection", "calculated", "decimals", "unit_mult")]
    data$key = paste(series_key, collapse = ".")
    data$freq = sdmx_freq(data$freq)

    entries = xml2::xml_find_all(x, "./generic:Obs[generic:ObsValue]")
    data$date = entries |>
      xml2::xml_find_all(".//generic:ObsDimension") |>
      xml2::xml_attr("value") |>
      parse_date(data$freq)

    data$value = entries |>
      xml2::xml_find_all(".//generic:ObsValue") |>
      xml2::xml_attr("value") |>
      as.numeric()

    as.data.table(data)
  })
  res = res |>
    rbindlist(fill = TRUE) |>
    setcolorder(col_order, skip_absent = TRUE)
  res[]
}

nob = function(resource, ..., accept = NULL) {
  sdmx_request("https://data.norges-bank.no/api", resource, \(resp) sdmx_error_body(resp), ..., accept = accept)
}

Try the bbk package in your browser

Any scripts or data that you put into this service are public.

bbk documentation built on Aug. 28, 2026, 5:07 p.m.