## ----include = FALSE----------------------------------------------------------
# Examples below contact the live API, so they are only evaluated when the
# documentation site is built (pkgdown sets IN_PKGDOWN), not on CRAN or during a
# normal package build.
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = identical(Sys.getenv("IN_PKGDOWN"), "true")
)

## ----setup--------------------------------------------------------------------
# library(nhsbsa)
# library(dplyr)

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

## -----------------------------------------------------------------------------
# nhsbsa_package_search(fq = 'tags:"Prescribing"')$count

## -----------------------------------------------------------------------------
# nhsbsa_package_search(
#   fq = 'tags:"Prescribing" -organization:freedom-of-information-disclosure-log'
# ) |>
#   as_tibble() |>
#   pull(title)

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

## -----------------------------------------------------------------------------
# nhsbsa_package_search(q = "prescribing", rows = 5)

## -----------------------------------------------------------------------------
# resources <- nhsbsa_list_resources("english-prescribing-data-epd")
# nrow(resources)
# resources |>
#   select(name, format, url) |>
#   slice_head(n = 6)

## -----------------------------------------------------------------------------
# nhsbsa_list_resources("english-prescribing-data-epd", pattern = "202401") |>
#   select(name, id, last_modified)

## -----------------------------------------------------------------------------
# nhsbsa_resource_show(resources$id[[1]])

## -----------------------------------------------------------------------------
# bnf <- nhsbsa_list_resources("bnf-code-information-current-year")
# path <- nhsbsa_download_resource(
#   "bnf-code-information-current-year",
#   resource_id = bnf$id[[1]],
#   directory = tempdir()
# )
# basename(path)

## -----------------------------------------------------------------------------
# nhsbsa_datastore_search(
#   resource_id = "EPD_202401",
#   fields = c("PCO_CODE", "BNF_CHEMICAL_SUBSTANCE", "ITEMS"),
#   sort = "ITEMS desc",
#   limit = 5
# )

## -----------------------------------------------------------------------------
# nhsbsa_datastore_search(
#   resource_id = "EPD_202401",
#   fields = c("PCO_CODE", "ITEMS"),
#   limit = 5,
#   offset = 5
# )

## -----------------------------------------------------------------------------
# # Filter to one organisation
# nhsbsa_datastore_search_sql(
#   resource_id = "EPD_202401",
#   sql = "SELECT PCO_CODE, BNF_CHEMICAL_SUBSTANCE, ITEMS
#          FROM `EPD_202401`
#          WHERE PCO_CODE = 'W2U3Z'
#          LIMIT 5"
# )

## -----------------------------------------------------------------------------
# # Aggregate: 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 5"
# )

