Skip to content

Commit

Permalink
Improved readme
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoZepeda committed Jul 16, 2024
1 parent b501204 commit 30feb5b
Show file tree
Hide file tree
Showing 44 changed files with 538 additions and 801 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: WHOicd
Title: Retrieve data from the World Health Organization (WHO) International Classification of Diseases (ICD) API
Title: Retrieve data from the World Health Organization (WHO) International Classification of Diseases (ICD) API version 2
Version: 0.2.0
Authors@R:
person("Rodrigo", "Zepeda-Tello", , "rzepeda17@gmail.com", role = c("aut", "cre"),
Expand Down
4 changes: 1 addition & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# Generated by roxygen2: do not edit by hand

export(get_token)
export(icd10_block_title)
export(icd10_blocks)
export(icd10_chapter_title)
export(icd10_chapters)
export(icd10_code_search_release)
export(icd10_code_title)
export(icd10_codes)
export(icd10_release_info)
export(icd10_releases)
export(icd10_search)
export(icd10_title)
import(httr2)
140 changes: 51 additions & 89 deletions R/icd10.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,55 +89,67 @@ icd10_release_info <- function(token, release = 2019, language = "en", auto_upda
}

#' @title ICD-10 titles
#' @name titles
#'
#' @description Obtain the title of an ICD-10 chapter, block or code
#'
#' @inheritParams make_request
#' @inheritParams get_token
#' @inheritParams icd10_release_info
#' @param chapter (`character`) ICD-10 chapter as a roman numeral
#' @param block ICD-10 section codes (hyphenated section codes)
#' @param code (`character`) ICD-10 code
#' @param validate_code ( `logical`) Apply additional functions to validate that
#' the ICD-10 code is written as the API needs it.
#' @param search_val (`character`) ICD-10 chapter, block or code for searching.
#'
#' @returns A character with the title of the chapter, block or code
NULL

#' @rdname titles
#' @examples
#' #Assuming that the CLIENT ID and CLIENT SECRET are set up. Substitute accordingly
#' if (exists("CLIENT_ID") & exists("CLIENT_SECRET")) {
#' #Generated token
#' token <- get_token(CLIENT_ID, CLIENT_SECRET)
#'
#' # Get title and url for release
#' icd10_chapter_title(token, "XII")
#' icd10_title(token, "XII")
#'
#' #Do multiple at once
#' icd10_title(token, c("XII","IX","XII","II","D50","D50.1"))
#' }
#'
#' @export
icd10_chapter_title <- function(token, chapter, release = 2019, language = "en",
auto_update = TRUE) {
icd10_title <- function(token, search_val, release = 2019, language = "en",
auto_update = TRUE, as_data_frame = TRUE) {

# Request the release list to entity
site_name <- request_WHO(
url = paste0("https://id.who.int/icd/release/10/", release, "/", toupper(chapter)),
token = token,
language = language,
auto_update = auto_update,
warning_message_404 = paste(
"Request not found. Possibly any of release, chapter/block/code or language is not",
"available or incorrectly specified."
),
post_process_function = function(site_name) {
site_name <- unlist(site_name["title"])
site_name <- as.character(site_name["title.@value"])
return(site_name)
}
)

return(site_name)
to_search <- unique(search_val)
searched <- c()
for (val in to_search){

# Request the release list to entity
site_name <- request_WHO(
url = paste0("https://id.who.int/icd/release/10/", release, "/", toupper(val)),
token = token,
language = language,
auto_update = auto_update,
warning_message_404 = paste(
"Request not found. Possibly any of release, chapter/block/code or language is not",
"available or incorrectly specified."
),
post_process_function = function(site_name) {
site_name <- unlist(site_name["title"])
site_name <- as.character(site_name["title.@value"])
return(site_name)
}
)

searched <- c(searched, site_name)
}

if (as_data_frame){
value_searched <- cbind(to_search, data.frame(title = searched))
value_searched <- data.frame(searched = search_val) |>
merge(value_searched, by.x = "searched", by.y = "to_search", all.x = TRUE, sort = FALSE)

} else {
value_searched <- searched
}

return(value_searched)
}

#' Chapters of ICD-10
Expand Down Expand Up @@ -172,40 +184,16 @@ icd10_chapters <- function(token, release = 2019, language = "en", codes_only =
}


#' @inheritParams make_request
#' @inheritParams get_token
#' @inheritParams icd10_release_info
#'
#' @examples
#' #Assuming that the CLIENT ID and CLIENT SECRET are set up. Substitute accordingly
#' if (exists("CLIENT_ID") & exists("CLIENT_SECRET")) {
#' #Generated token
#' token <- get_token(CLIENT_ID, CLIENT_SECRET)
#'
#' # Get title and url for release
#' icd10_block_title(token, "L50-L54")
#' }
#'
#' @rdname titles
#' @export
icd10_block_title <- function(token, block, release = 2019, language = "en") {
icd10_chapter_title(
token = token,
chapter = block,
release = release,
language = language,
)
}

#' Block of ICD-10 Chapter
#'
#' @description Lists the available sections inside a chapter for an ICD-10
#' release.
#'
#' @inheritParams make_request
#' @inheritParams get_token
#' @inheritParams icd10_chapter_title
#' @inheritParams icd10_title
#' @inheritParams icd10_name_children
#' @param chapter Roman numeral indicating the chapter of interest to obtain the blocks from.
#'
#' @returns Data frame with all the blocks inside a chapter and their
#' description
Expand All @@ -231,51 +219,23 @@ icd10_blocks <- function(token, chapter, release = 2019, language = "en", codes_
)
}

#' @inheritParams icd10_block_title
#' @inheritParams make_request
#' @inheritParams get_token
#' @inheritParams icd10_release_info
#'
#' @rdname titles
#'
#' @examples
#' #Assuming that the CLIENT ID and CLIENT SECRET are set up. Substitute accordingly
#' if (exists("CLIENT_ID") & exists("CLIENT_SECRET")) {
#' #Generated token
#' token <- get_token(CLIENT_ID, CLIENT_SECRET)
#'
#' # Get title and url for release
#' icd10_code_title(token, "E14.1")
#' }
#'
#' @export
icd10_code_title <- function(token, code, release = 2019, language = "en", validate_code = TRUE) {
if (validate_code) {
code <- icd10_validate_code(code)
}

# Obtain the code
icd10_chapter_title(
token = token,
chapter = code,
release = release,
language = language
)
}

#' Codes of ICD-10 block
#'
#' @description Lists the available codes inside a block for an ICD-10
#' release.
#'
#' @inheritParams make_request
#' @inheritParams get_token
#' @inheritParams icd10_block_title
#' @inheritParams icd10_title
#' @inheritParams icd10_name_children
#'
#' @returns Data frame with all the codes inside a block and their
#' description
#'
#' @param block Character with range of values that conform an ICD-10 code (such as `"I70-I79"`).
#' Note that not all possible ranges are blocks. See [icd10_blocks()] to list all blocks for a
#' chapter.
#'
#' @examples
#' #Assuming that the CLIENT ID and CLIENT SECRET are set up. Substitute accordingly
#' if (exists("CLIENT_ID") & exists("CLIENT_SECRET")) {
Expand All @@ -302,7 +262,9 @@ icd10_codes <- function(token, block, release = 2019, language = "en", codes_onl
#' Searches for a specific ICD-10 code across different releases and returns
#' the editions of those releases in which the code appears.
#' @inheritParams make_request
#' @inheritParams icd10_code_title
#' @inheritParams icd10_title
#' @param code ICD-10 code (can be 3 or 4 digit) to search.
#' @param validate_code Check that value seems a valid ICD-10 code before querying the API.
#'
#' @returns A vector of characters with the ICD-10 releases where you can find
#' the ICD-10 code
Expand Down
14 changes: 8 additions & 6 deletions R/icd10_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ icd10_name_children <- function(token, site, release = 2019, language = "en", co
# Loop through all chapters/blocks/codes and create a data.frame
titles <- rep(NA_character_, length(children))
for (k in 1:length(children)) {
titles[k] <- icd10_chapter_title(
titles[k] <- icd10_title(
token = token,
chapter = children[k],
search_val = children[k],
release = release,
language = language
language = language,
as_data_frame = FALSE
)
}

Expand Down Expand Up @@ -187,11 +188,12 @@ icd10_parents <- function(token, site, release = 2019, language = "en", codes_on

# Obtain name of the parent
if (!codes_only) {
name_parent <- icd10_chapter_title(
name_parent <- icd10_title(
token = token,
chapter = current_parent,
search_val = current_parent,
release = release,
language = language
language = language,
as_data_frame = FALSE
)
} else {
name_parent <- ""
Expand Down
7 changes: 4 additions & 3 deletions R/icd10_vectorized.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
for (val in search_values){

#Get title of current disease
title_val <- icd10_chapter_title(
title_val <- icd10_title(
token = token,
chapter = val,
search_val = val,
release = release,
language = language,
auto_update = auto_update)
auto_update = auto_update,
as_data_frame = FALSE)
titles <- c(titles, title_val)

#Get parents
Expand Down
Loading

0 comments on commit 30feb5b

Please sign in to comment.