---
title: "Working with World Health Organization ICD codes and national editions, and different languages"
author: Jack O. Wasey
output: rmarkdown::pdf_document
---

`icd` 4.0+ offers some international support.

- World Health Organization (WHO) codes for 2016 in English, the latest release.
- World Health Organization (WHO) codes for 2008 in French, the latest available online data.
- ICD-10-FR in French, with modifications as used in France.
- Translations of the US ICD-10-CM codes in French and Dutch, which are used in Belgium.

All of this should be considered beta quality, and I would much appreciate testing and bug reports. I would especially be interested if someone has experience internationalizing an R package like this.

Function and variable names are likely to change in the future. The current versions are more consistent with naming of data files.

```{r setup, include=FALSE}
# not building automatically with CRAN, devtools, because of dependency on downloaded data. Create like this:
# rmarkdown::render("vignettes/country-lang-vers-prebuilt.Rmd")
#  then compact or R CMD build --compact-vignettes

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(icd)
```

# WHO

Surprisingly, there are potential copyright restrictions on redistributing the
WHO ICD-9 and ICD-10 definitions, which form the basis of ICD code systems
around the world. These definitions are themselves also used as the
internationally standard way to codify morbidity and mortality for public
health.

`icd` asks the user whether they wish to download the data from the WHO web
site, whereupon it is made available as functions, named similarly to existing
data, e.g., `get_icd10who2016()`, which is the latest ICD-10 release from the
WHO at the time of writing. For French codes, `get_icd10fr2016()`, with the
localized synonym `get_cim10fr2016()` having internationalized column names,
but this would duplicate the data making package storage and loading bigger,
which is increasingly a problem as more data is included.

Unfortunately, the availability and URL of the WHO resources sometimes change.
If you see errors, check that the WHO resource is available at that time, and,
if you think the URL has been changed, then please file an
[issue](https://github.com/jackwasey/icd/issues).

```{r getstarted, eval = FALSE}
# if not already done, set up the downloaded data cache:
setup_icd_data()
# or setup and optionally download everything at once (currently ~350 MB)
# and a few minutes to generate the data structures.
download_all_icd_data()
```

```{r quicklook}
get_icd10who2016()[1:5, ]
get_icd10fr2019()[1:5, ]
summary(get_icd10who2016())
```

```{r quicklookeval, echo = FALSE}
get_icd10who2016()[1:5, ]
get_icd10fr2019()[1:5, ]
summary(get_icd10who2016())
```

# French edition of ICD-10

ICD-10-FR is significantly different from the WHO version, and is used by official bodies and health systems in France. As with all the features in this document, this is an early attempt at broadening the international capabilities of this package.

```{r french}
fr <- get_icd10fr2019()
fr[fr$code == "C43", ]
```

# Working with different ICD-10-CM annual versions

There is an initial beta-quality mechanism for selecting a particular version of ICD-10-CM to use. This is a US coding system, but Belgian authorities provide translations into French and Dutch, and appear to have made no modifications.

```{r getset}
# get the active version string
get_icd10cm_active_year()
# get the data itself:
devnull <- get_icd10cm_active()
# set the active version, and check it is the one we expected
set_icd10cm_active_year("2015")
identical(get_icd10cm_active(), get_icd10cm2015())
```

The function `with_icd10cm_version` allows temporarily using a particular data set for a computation, analogous to the functions in the `withr` package:

```{r icd10cmoption}
# The code "C4311" goes from being a leaf in 2018, to a parent in 2019
with_icd10cm_version(
  ver = "2018",
  is_leaf(as.icd10cm("C4311"))
)
with_icd10cm_version(
  ver = "2019",
  is_leaf(as.icd10cm("C4311"))
)
# In 2018 the following code was not even defined, but in 2019 was a new child
# code of "C4311"s
with_icd10cm_version(ver = "2018", {
  print(is_defined(as.icd10cm("C4311")))
  print(is_leaf(as.icd10cm("C4311")))
})
with_icd10cm_version(
  ver = "2019",
  is_leaf("C43111")
)
```
