You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.1KB

  1. library(readr)
  2. library(dplyr)
  3. library(stringr)
  4. library(tidyr)
  5. library(purrr)
  6. library(magrittr)
  7. read_csv2("cim10.csv") %>%
  8. mutate(famille_libelle = famille_libelle %>% str_replace("\\w\\d+ ", ""),
  9. CMD_libelle = CMD_libelle %>% str_replace("\\d+ ", "")) %>%
  10. left_join(read_csv("CMA_Neo.txt", col_names = "code") %>%
  11. mutate(neo = "N"),
  12. by = c("diag_code" = "code")) %>%
  13. left_join(read_csv("CMA_Obs.txt", col_names = "code") %>%
  14. mutate(obs = "O"),
  15. by = c("diag_code" = "code")) -> cim10
  16. # groupes ----------
  17. read_csv2("groupes.csv") %>%
  18. separate(groupe_code, sep = "-", into = c("borne_inf", "borne_sup"), remove = F) %>%
  19. unite(groupe, groupe_code, groupe_libelle, sep = " ", remove = F) -> groupes
  20. cim10 %<>%
  21. mutate(groupe = famille_code %>%
  22. map_chr(. %>%
  23. {
  24. ifelse(identical(groupes$groupe[. >= groupes$borne_inf & . <= groupes$borne_sup], character(0)),
  25. "",
  26. groupes$groupe[. >= groupes$borne_inf & . <= groupes$borne_sup] %>%
  27. str_c(collapse = "|"))
  28. })) %>%
  29. unite(chapitre, chapitre_code, chapitre_libelle, sep = " ") %>%
  30. unite(famille, famille_code, famille_libelle, sep = " ") %>%
  31. unite(diag, diag_code, diag_libelle, sep = " ") %>%
  32. unite(CMD, CMD_code, CMD_libelle, sep = " ") %>%
  33. unite(label, CMA, neo, obs, sep = "|") %>%
  34. mutate(famille = ifelse(famille == diag, "", famille)) %>%
  35. mutate(path = str_c(chapitre, groupe, famille, diag, sep = "|") %>%
  36. str_replace("\\|{2,}", "|") %>%
  37. str_split("\\|"),
  38. label = label %>%
  39. str_replace_all("NA|1", "") %>%
  40. str_replace_all("\\|{2,}", "|") %>%
  41. str_replace_all("(^\\|)|(\\|$)", "")) %>%
  42. filter(label != "") %>%
  43. mutate(label = label %>%
  44. str_split("\\|")) %>%
  45. select(path, label)
  46. rm(groupes)
  47. cim10 %<>% tree
  48. cim10 %<>% summ_var("label")
  49. cim10 %>% tree2html -> cim10_html
  50. tree <- function(df)
  51. {
  52. if ((df$path %>% map_dbl(length) == 0) %>% all)
  53. {
  54. df %>% select(-path)
  55. } else
  56. {
  57. df$path %>% map(head, 1) -> df$nodes
  58. df$path %<>% map(tail, -1)
  59. df$nodes %>% flatten_chr %>% unique %>%
  60. sapply(simplify = F, function(node)
  61. {
  62. df %>% filter(nodes == node) %>% select(-nodes)
  63. }) %>%
  64. sapply(simplify = F, tree)
  65. }
  66. }
  67. add_names <- function(tr, name = NULL)
  68. {
  69. if (tr %>% is.data.frame)
  70. {
  71. tr$name <- name
  72. tr
  73. } else if (tr %>% is.atomic)
  74. {
  75. tr
  76. } else
  77. {
  78. tr %>% map2(tr %>% names, add_names)
  79. }
  80. }
  81. untree <- function(tr)
  82. {
  83. empty_path <- function(tr)
  84. {
  85. if (tr %>% is.data.frame)
  86. {
  87. tr$path <- rep(list(character(0)), nrow(tr))
  88. tr
  89. } else
  90. {
  91. tr %>% map(empty_path)
  92. }
  93. }
  94. tr %<>% empty_path
  95. untree_ <- function(tr)
  96. {
  97. if (tr %>% map_lgl(is.data.frame) %>% all)
  98. {
  99. tr %>% names %>% rep(tr %>% map_dbl(nrow)) -> nodes
  100. tr %>%
  101. reduce(bind_rows) %>%
  102. mutate(path = map2(nodes, path, splice))
  103. } else if (tr %>% is.data.frame)
  104. {
  105. tr
  106. } else
  107. {
  108. tr %>% map(untree_)
  109. }
  110. }
  111. while (!is.data.frame(tr))
  112. {
  113. tr %<>% untree_
  114. }
  115. tr
  116. }
  117. summ_var <- function(tr, varname = NULL)
  118. {
  119. if (varname %>% is.null)
  120. {
  121. return(tr)
  122. }
  123. if (tr %>% is.data.frame | tr %>% is.atomic)
  124. {
  125. tr
  126. } else
  127. {
  128. tr %<>% map(summ_var, varname)
  129. tr[[varname]] <- tr %>% map(varname) %>% unlist %>% unique %>% sort %>% list
  130. tr
  131. }
  132. }
  133. tree2html <- function(tr)
  134. {
  135. if (!is.data.frame(tr))
  136. {
  137. tr %>% Filter(is.list, .) %>% names %>% setdiff("label") -> names
  138. str_c('<ul>\n',
  139. tr %>% Filter(is.list, .) %>% names %>% setdiff("label") %>% tr[.] %>% map2(names, function(tr, name)
  140. {
  141. # labels <- c("2" = "success", "3" = "warning", "4" = "danger", "O" = "info", "N" = "info")
  142. # labels <- labels[tr$CMA]
  143. str_c("<li>", name, str_c('<span class = "label label-', tr$label %>% unlist, '">', tr$label %>% unlist, "</span>", collapse = " "), tree2html(tr), "</li>", sep = " ")
  144. }) %>% str_c(collapse = "\n"),
  145. "</ul>")
  146. }
  147. }