Skip to content

Commit

Permalink
Rename objects (#35)
Browse files Browse the repository at this point in the history
Introduce consistent object names for snomed, ICD-10 and OPSC-4 codes
---------

Co-authored-by: Milan Wiedemann <[email protected]>
  • Loading branch information
atamborska and milanwiedemann authored Jan 9, 2025
1 parent a37d259 commit 9fc4be3
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 77 deletions.
33 changes: 26 additions & 7 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ NULL
#' \describe{
#' \item{start_date}{Start date of code usage count}
#' \item{end_date}{End date of code usage count}
#' \item{snomed_concept_id}{SNOMED Concept ID}
#' \item{snomed_code}{SNOMED Concept ID}
#' \item{usage}{Yearly summary of code usage.
#' Note that counts are rounded to the nearest 10.
#' Counts of 5 or below are displayed as 5.}
Expand All @@ -35,6 +35,9 @@ NULL
#' Yearly summary of 4-character ICD-10 code usage from 1st April 2013 to 31st March 2024.
#' The code usage represents the annual count of all episodes which record the given ICD-10 code in any primary or secondary position.
#' Restricted codes for which annual usage is not published have been removed.
#' Yearly summary of 4-character ICD-10 code usage from 1st April 2013 to 31st March 2024.
#' The code usage represents the annual count of all episodes which record the given ICD-10 code in any primary or secondary position.
#' Restricted codes for which annual usage is not published have been removed.
#' @format A data frame with 135,951 rows and 5 columns:
#' \describe{
#' \item{start_date}{Start date of code usage count}
Expand All @@ -43,6 +46,8 @@ NULL
#' Note that the punctuation from the code has been removed for compatibility with OpenCodelists.}
#' \item{usage}{Annual count of code usage.}
#' \item{description}{Description of the ICD-10 Code}
#' \item{usage}{Annual count of code usage.}
#' \item{description}{Description of the ICD-10 Code}
#' }
#' @source <https://digital.nhs.uk/data-and-information/publications/statistical/hospital-admitted-patient-care-activity>
#' @examples
Expand All @@ -59,25 +64,39 @@ NULL
#' "https://www.opencodelists.org/codelist/opensafely/pregnancy-icd10-aurum/5a7d8d12/download.csv")
#' icd10_usage |>
#' dplyr:: filter(icd10_code %in% codelist$code)
#' @examples
#' # Filter to codes in the ICD-10 Chapter XIX: "Injury, poisoning..."
#' # (codes begin with letters "S" or "T"), with usage > 10,000.
#' # For each of these, select the year with the highest count.
#' icd10_usage |>
#' dplyr:: filter(grepl("^[ST]", icd10_code) & usage > 10000)|>
#' dplyr:: group_by(description) |>
#' dplyr:: slice_max(usage)
#' # Filter to codes present in the CPRD Aurum ICD-10 pregnancy codelist.
#' # This codelist is available in OpenCodelists.org
#' codelist<- read.csv(
#' "https://www.opencodelists.org/codelist/opensafely/pregnancy-icd10-aurum/5a7d8d12/download.csv")
#' icd10_usage |>
#' dplyr:: filter(icd10_code %in% codelist$code)
"icd10_usage"

#' Yearly OPCS Code Usage from Hospital Admitted Patient Care Activity in England
#' Yearly OPCS-4 Code Usage from Hospital Admitted Patient Care Activity in England
#'
#' Yearly summary of 4-character OPCS code usage from 1st April 2013 to 31st March 2024.
#' Yearly summary of 4-character OPCS-4 code usage from 1st April 2013 to 31st March 2024.
#' The code usage represents the total annual count of each procedure, recorded across the primary and the secondary procedure positions.
#' Restricted codes for which annual usage is not published have been removed.
#' @format A data frame with 107,376 rows and 5 columns:
#' \describe{
#' \item{start_date}{Start date of code usage count}
#' \item{end_date}{End date of code usage count}
#' \item{opcs_code}{The 4-character OPCS code.
#' \item{opcs4_code}{The 4-character OPCS-4 code.
#' Note that the punctuation from the code has been removed for compatibility with OpenCodelists.}
#' \item{usage}{Annual count of code usage.}
#' \item{description}{Description of the OPCS Code}
#' \item{description}{Description of the OPCS-4 Code}
#' }
#' @source <https://digital.nhs.uk/data-and-information/publications/statistical/hospital-admitted-patient-care-activity>
#' @examples
#' # Filter to procedures involving "biopsy" after March 2020 (note each year runs April - March).
#' opcs_usage |>
#' opcs4_usage |>
#' dplyr:: filter(grepl("biopsy", description, ignore.case = TRUE) & lubridate:: year(end_date) > 2020)
"opcs_usage"
"opcs4_usage"
6 changes: 3 additions & 3 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ app_server <- function(input, output, session) {
selected_data <- reactive({
if (input$dataset == "snomedct") {
codeusage::snomed_usage |>
select(start_date, end_date, code = snomed_concept_id, description, usage)
select(start_date, end_date, code = snomed_code, description, usage)
} else if (input$dataset == "icd10") {
codeusage::icd10_usage |>
select(start_date, end_date, code = icd10_code, description, usage)
} else if (input$dataset == "opcs4") {
codeusage::opcs_usage |>
select(start_date, end_date, code = opcs_code, description, usage)
codeusage::opcs4_usage |>
select(start_date, end_date, code = opcs4_code, description, usage)
}
})

Expand Down
8 changes: 4 additions & 4 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ app_ui <- function(request) {
"Select dataset",
bs_icon("info-circle")
),
"SNOMED CT (Systematized Nomenclature of Medicine Clinical Terms); ICD (International Classification of Diseases); OPCS Classification of Interventions and Procedures",
"SNOMED CT (Systematized Nomenclature of Medicine Clinical Terms); ICD-10 (International Classification of Diseases); OPCS-4 Classification of Interventions and Procedures",
options = list(
customClass = "left-align-tooltip"
)
Expand All @@ -32,10 +32,10 @@ app_ui <- function(request) {
choices = c(
# Systematized Nomenclature of Medicine Clinical Terms (SNOMED CT)
"SNOMED-CT" = "snomedct",
# International Classification of Diseases (ICD)
# International Classification of Diseases (ICD-10)
"ICD-10" = "icd10",
# OPCS Classification of Interventions and Procedures (OPCS)
"OPCS" = "opcs4"
# OPCS-4 Classification of Interventions and Procedures (OPCS-4)
"OPCS-4" = "opcs4"
)
)
),
Expand Down
4 changes: 2 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ utils::globalVariables(c(
"annual_proportion",
"start_date",
"end_date",
"snomed_concept_id",
"snomed_code",
"description",
"usage",
"icd10_code",
"opcs_code",
"opcs4_code",
"total_usage",
"full_slug"
))
16 changes: 8 additions & 8 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ knitr::opts_chunk$set(

<!-- badges: end -->

The goal of `codeusage` is to make yearly summaries of **SNOMED Code Usage in Primary Care** and **ICD-10 and OPCS Code Usage in Secondary Care** in England, published by NHS Digital, available in R for research.
The goal of `codeusage` is to make yearly summaries of **SNOMED Code Usage in Primary Care** and **ICD-10 and OPCS-4 Code Usage in Secondary Care** in England, published by NHS Digital, available in R for research.
The interactive [Code Usage Explorer](https://milanwiedemann.shinyapps.io/codeusage/) dashboard provides different options to explore these datasets.
The original data is available from NHS Digital at:

- [SNOMED Code Usage in Primary Care](https://digital.nhs.uk/data-and-information/publications/statistical/mi-snomed-code-usage-in-primary-care)
- [ICD-10 and OPCS Code Usage in Secondary Care](https://digital.nhs.uk/data-and-information/publications/statistical/hospital-admitted-patient-care-activity)
- [ICD-10 and OPCS-4 Code Usage in Secondary Care](https://digital.nhs.uk/data-and-information/publications/statistical/hospital-admitted-patient-care-activity)

## Installation

Expand Down Expand Up @@ -63,13 +63,13 @@ This is only a selection of the full dataset published by NHS Digital, for the d
icd10_usage
```

### Dataset: OPCS Code Usage in Secondary Care in England
### Dataset: OPCS-4 Code Usage in Secondary Care in England

This is the total annual count of each instance that each 4-character OPCS code is listed across all primary and secondary procedure positions in the Finished Consultant Episodes (FCE) of the Hospital Episode Statistics in England.
This is the total annual count of each instance that each 4-character OPCS-4 code is listed across all primary and secondary procedure positions in the Finished Consultant Episodes (FCE) of the Hospital Episode Statistics in England.

This is only a selection of the full dataset published by NHS Digital, for the data pre-processing see `/data-raw/opcs_usage.R`.
This is only a selection of the full dataset published by NHS Digital, for the data pre-processing see `/data-raw/opcs4_usage.R`.

```{r opcs-usage}
# Return OPCS code usage data
opcs_usage
```{r opcs-4-usage}
# Return OPCS-4 code usage data
opcs4_usage
```
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<!-- badges: end -->

The goal of `codeusage` is to make yearly summaries of **SNOMED Code
Usage in Primary Care** and **ICD-10 and OPCS Code Usage in Secondary
Usage in Primary Care** and **ICD-10 and OPCS-4 Code Usage in Secondary
Care** in England, published by NHS Digital, available in R for
research. The interactive [Code Usage
Explorer](https://milanwiedemann.shinyapps.io/codeusage/) dashboard
Expand All @@ -19,7 +19,7 @@ The original data is available from NHS Digital at:

- [SNOMED Code Usage in Primary
Care](https://digital.nhs.uk/data-and-information/publications/statistical/mi-snomed-code-usage-in-primary-care)
- [ICD-10 and OPCS Code Usage in Secondary
- [ICD-10 and OPCS-4 Code Usage in Secondary
Care](https://digital.nhs.uk/data-and-information/publications/statistical/hospital-admitted-patient-care-activity)

## Installation
Expand All @@ -46,7 +46,7 @@ for the data pre-processing see `/data-raw/snomed_code_usage.R`.
# Return SNOMED code usage data
snomed_usage
#> # A tibble: 1,523,967 × 7
#> start_date end_date snomed_concept_id description usage active_at_start
#> start_date end_date snomed_code description usage active_at_start
#> <date> <date> <chr> <chr> <int> <lgl>
#> 1 2023-08-01 2024-07-31 279991000000102 Short message… 4.41e8 TRUE
#> 2 2023-08-01 2024-07-31 184103008 Patient telep… 1.91e8 TRUE
Expand Down Expand Up @@ -90,21 +90,21 @@ icd10_usage
#> # ℹ 135,941 more rows
```

### Dataset: OPCS Code Usage in Secondary Care in England
### Dataset: OPCS-4 Code Usage in Secondary Care in England

This is the total annual count of each instance that each 4-character
OPCS code is listed across all primary and secondary procedure positions
OPCS-4 code is listed across all primary and secondary procedure positions
in the Finished Consultant Episodes (FCE) of the Hospital Episode
Statistics in England.

This is only a selection of the full dataset published by NHS Digital,
for the data pre-processing see `/data-raw/opcs_usage.R`.
for the data pre-processing see `/data-raw/opcs4_usage.R`.

``` r
# Return OPCS code usage data
opcs_usage
# Return OPCS-4 code usage data
opcs4_usage
#> # A tibble: 107,376 × 5
#> start_date end_date opcs_code description usage
#> start_date end_date opcs4_code description usage
#> <date> <date> <chr> <chr> <int>
#> 1 2023-04-01 2024-03-31 A011 Hemispherectomy 7
#> 2 2023-04-01 2024-03-31 A012 Total lobectomy of brain 36
Expand Down
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ reference:
contents:
- snomed_usage
- icd10_usage
- opcs_usage
- opcs4_usage
- title: Codelists
contents:
- get_codelist
Expand Down
18 changes: 9 additions & 9 deletions data-raw/opcs_code_usage.R → data-raw/opcs4_code_usage.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ library(httr)

url_start <- "https://files.digital.nhs.uk/"

opcs_code_usage_urls <- list(
opcs4_code_usage_urls <- list(
"fy23to24" = list(
url = paste0(url_start, "92/DB66C9/hosp-epis-stat-admi-proc-2023-24-tab-v2.xlsx"),
sheet = 6,
Expand Down Expand Up @@ -81,7 +81,7 @@ opcs_code_usage_urls <- list(
)

# Function to download and read the xlsx files
read_opcs_usage_xlsx_from_url <- function(url_list, ...) {
read_opcs4_usage_xlsx_from_url <- function(url_list, ...) {
temp_file <- tempfile(fileext = ".xlsx")
GET(
url_list$url,
Expand All @@ -101,7 +101,7 @@ read_opcs_usage_xlsx_from_url <- function(url_list, ...) {
select_all_diag_counts <- function(data, url_list) {
dplyr::select(
data,
opcs_code = 1,
opcs4_code = 1,
description = 2,
usage = url_list$usage_col
) |>
Expand All @@ -111,13 +111,13 @@ select_all_diag_counts <- function(data, url_list) {
}

# Combine both functions
get_opcs_data <- function(url_list, ...) {
df_temp <- read_opcs_usage_xlsx_from_url(url_list, ...)
get_opcs4_data <- function(url_list, ...) {
df_temp <- read_opcs4_usage_xlsx_from_url(url_list, ...)
select_all_diag_counts(df_temp, url_list)
}

opcs_usage <- opcs_code_usage_urls |>
map(get_opcs_data) |>
opcs4_usage <- opcs4_code_usage_urls |>
map(get_opcs4_data) |>
bind_rows(.id = "nhs_fy") |>
separate(nhs_fy, c("start_date", "end_date"), "to") |>
mutate(
Expand All @@ -127,12 +127,12 @@ opcs_usage <- opcs_code_usage_urls |>
end_date = as.Date(
paste0("20", str_extract_all(end_date, "\\d+"), "-03-31")
),
opcs_code = str_replace_all(opcs_code, "\\.", "")
opcs4_code = str_replace_all(opcs4_code, "\\.", "")
) |>
filter(!is.na(usage))

usethis::use_data(
opcs_usage,
opcs4_usage,
compress = "bzip2",
overwrite = TRUE
)
17 changes: 11 additions & 6 deletions data-raw/snomed_code_usage.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ snomed_code_usage_urls <- list(
# Data dictionary from SNOMED_code_usage_metadata.xlsx
# https://files.digital.nhs.uk/31/097702/SNOMED_code_usage_metadata.xlsx

# * SNOMED_Concept_ID (Text string of digits up to 18 characters long)
# * snomed_code (Text string of digits up to 18 characters long)
# SNOMED concepts which have been added to a patient record in a general practice system during the reporting period.
# * Description (Text string)
# The fully specified name associated with the SNOMED_Concept_ID on the final day of the reporting period (31 July).
# The fully specified name associated with the snomed_code on the final day of the reporting period (31 July).
# * Usage (Numeric (integer) or *)
# The number of times that the SNOMED_Concept_ID was added into any patient record within the reporting period, rounded to the nearerst 10.
# The number of times that the snomed_code was added into any patient record within the reporting period, rounded to the nearerst 10.
# Usage of 1 to 4 is displayed as *.
# * Active_at_Start
# Active status of the SNOMED_Concept_ID on the first day of the reporting period.
# Active status of the snomed_code on the first day of the reporting period.
# This is taken from the most recent UK clinical extension, or associated International extention, which was published up to the start of the reporting year (1 August).
# 1 = SNOMED concept was published and was active (active = 1).
# 0 = SNOMED concept was either not yet available or was inactive (active = 0).
# * Active_at_End "Active status of the SNOMED_Concept_ID on the first day of the reporting period.
# * Active_at_End "Active status of the snomed_code on the first day of the reporting period.
# This is taken from the most recent UK clinical extension, or associated International extention, which was published up to the end of the reporting year (31 July).
# 1 = SNOMED concept was published and was active (active = 1).
# 0 = SNOMED concept was either not yet available or was inactive (active = 0).
Expand All @@ -49,7 +49,7 @@ snomed_code_usage_urls <- list(
snomed_usage <- snomed_code_usage_urls %>%
map(read_tsv,
col_types = list(
SNOMED_Concept_ID = "c",
snomed_code = "c",
Description = "c",
Usage = "i",
Active_at_Start = "l",
Expand All @@ -65,6 +65,11 @@ snomed_usage <- snomed_code_usage_urls %>%
usage = replace_na(usage, 5)
)

# Manipulation required due to variable name change
snomed_usage <- snomed_usage |>
rename(snomed_code = snomed_concept_id)|>
mutate(snomed_code = as.character(snomed_code))

usethis::use_data(
snomed_usage,
compress = "bzip2",
Expand Down
Binary file modified data/icd10_usage.rda
Binary file not shown.
Binary file added data/opcs4_usage.rda
Binary file not shown.
Binary file removed data/opcs_usage.rda
Binary file not shown.
Binary file modified data/snomed_usage.rda
Binary file not shown.
16 changes: 8 additions & 8 deletions man/opcs_usage.Rd → man/opcs4_usage.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/snomed_usage.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9fc4be3

Please sign in to comment.