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.

169 lines
4.3KB

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