Code
library(tidyverse)
library(opr)
library(arrow)
library(gt)
library(here)
library(glue)Nasir Abdulrasheed
Load packages and set up variables for BNF codes and data date range (May 2016 to April 2026).
Before running the notebook for data fetching, kindly check the dates and BNF codes to ensure you are not wasting BigQuery resources.
# dev set of 4 drugs
SERTRALINE_HCL <- "0403030Q0%"
ESCITALOPRAM <- "0403030X0%"
LORATADINE <- "0304010D0%"
FEXOFENADINE_HCL <- "0304010E0%"
DEV_CODES <- c(SERTRALINE_HCL, ESCITALOPRAM, FEXOFENADINE_HCL, LORATADINE)
# test drug set
FLUPENTIXOL <- "0402020G0%"
PALIPERIDONE <- "0402020AB%"
ZUCLOPENTHIXOL <- "0402020Z0%"
NORETHISTERONE <- "0604012P0%"
CELECOXIB <- "1001010AH%"
TEST_CODES <- c(
FLUPENTIXOL,
PALIPERIDONE,
ZUCLOPENTHIXOL,
NORETHISTERONE,
CELECOXIB
)
DATE_START <- "2016-05-01"
DATE_END <- "2026-04-01"Table 1 and Table 4 show the list of chemicals and sections whose data will be extracted for this study.
Connect to BigQuery. See here.
Query the normalised_prescribing table to fetch the data for the BNF codes. The function below takes the connection object and either display the query or collect the data, based on the display_query argument:
fetch_presc_data <- function(code, display_query = TRUE) {
df <- get_normalised_prescribing(
con = CONN,
bnf_codes = code,
start_date = DATE_START,
end_date = DATE_END
) |>
select(-net_cost, -actual_cost)
if (display_query) {
show_query(df)
} else {
collect(df)
}
}
# fetches given products details from the bnf table
fetch_bnf_codes <- function(bnf_codes, display_query = TRUE) {
df <- tbl(CONN, "bnf") |>
filter(presentation_code %in% bnf_codes) |>
select(
-ends_with("_code"),
-presentation,
chemical_code,
presentation_code
)
if (display_query) {
show_query(df)
} else {
collect(df)
}
}This emits the query for fetching the prescribing data for the development drugs as an example:
The data for the four drugs/chemicals in Table 1 will be used in developing the pipeline in the first instance, while other drugs will used for testing after implementation is complete.
These drugs are used to test the output of the pipeline after full implementation.
df_presc_test <- fetch_presc_data(TEST_CODES, display_query = FALSE)
df_bnf_test <- unique(df_presc_test$bnf_code) |>
fetch_bnf_codes(display_query = FALSE)
df_test_drugs <- df_presc_test |>
left_join(df_bnf_test, c("bnf_code" = "presentation_code"))
save_file <- glue("df_test_drugs_{DATE_START}_{DATE_END}.parquet")
write_parquet(
df_test_drugs,
here("data-raw", "nasir", save_file)
)
head(df_test_drugs, 5) |> gt()Fetch all NHS regions for likely sensitivity analysis later on: