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.

166 lines
4.2KB

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