R/get_diseases.R

Defines functions get_diseases

Documented in get_diseases

#' @title Get Diseases and ICD-9 Numbers Supported
#'
#' @description Gets Diseases and ICD-9 numbers from the disgenet API to produce a supported lsit of dieases for the
#' Gadget \code{genetic_disease_explore()}. This takes a few minutes to run and is only provided to demonstrate how
#' data used in this package is generated.
#'
#' @param write
#' Bool, if T it writes a csv to your current wd.
#' @param to_r
#' Bool, if T it writes a data.frame to R with the name specified by ob_name
#' @param ob_name
#' A String, specifying an object name to save results as if ob_name = T
#'
#' @export
#'
#' @import magrittr
#' @import glue
#' @import dplyr
#'
#' @return A data.frame of diseases and associated ICD-9 numbers
#' @examples
#' \dontrun{
#' get_diseases()
#' }

get_diseases <- function(write = T, to_r = F, ob_name = "diseases") {

## Pre-allocate memory
j <- 1
res <- list()
res_ind <- list()
## For loop to check all possible ICD-9 numbers for a matching disease.
## It saves it as a possible query for later.
for(i in 11:999) {

  data4 <- try(readr::read_tsv(glue("https://www.disgenet.org/api/gda/disease/icd9cm/{stringr::str_pad(glue::glue('{i}'), width = 3, side = 'left', pad = '0')}?source=CURATED&min_score=0&max_score=1&format=tsv")),
               silent = T)

  if(class(data4) != "try-error") {

    res[[j]] <- unique(data4$disease_name)
    res_ind[[j]] <- i
    j <- j + 1

  }

print(i)

}

## Clean the results and save it for later reference ----
cleaned_res <- unlist(res)
cleaned_res <- tibble::as_tibble(cbind(cleaned_res, icd9 = unlist(res_ind)))
cleaned_res <- cleaned_res %>%
  dplyr::arrange(cleaned_res)

if (to_r) {
  assign(ob_name, cleaned_res, envir = globalenv())
}

if (write) {
  write.csv(cleaned_res, "diseases.csv")
}

}
lharris421/gdexpl documentation built on Dec. 23, 2019, 6:38 p.m.