Explorateur d'ontologies i2b2 HEGP
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.

214 lines
7.1KB

  1. library(tidyverse)
  2. library(data.tree)
  3. library(shiny)
  4. library(shinyjs)
  5. library(shinyTree)
  6. library(rclipboard)
  7. source("helper_functions.R")
  8. source("ui.R")
  9. # Load data
  10. read_csv("metadata.csv") %>%
  11. filter(C_VISUALATTRIBUTES != "LI") %>% # will be dealt in extraction
  12. mutate(C_FULLNAME = paste0("\\i2b2\\", C_FULLNAME), # will be dealt in extraction
  13. C_HLEVEL = if_else(C_VISUALATTRIBUTES == "RA", str_count(C_FULLNAME, "\\\\") - 2, C_HLEVEL), # C_HLEVEL of modifiers are all 1, changing it to the correct number (either 2 or 3)
  14. C_FULLNAME2 = str_replace_all(C_FULLNAME, regex("\\\\( ){1,3}"), "\\\\"), # remove leading whitespace on nodes in the full path
  15. C_FULLNAME2 = str_replace_all(C_FULLNAME2, regex("( ){1,11}\\\\"), "\\\\"), # remove trailing whitespace on nodes in the full path
  16. sticon = recode(C_VISUALATTRIBUTES, # to delete ?
  17. "FA"="glyphicon glyphicon-folder-close",
  18. "LA"="glyphicon glyphicon-file",
  19. "RA"="glyphicon glyphicon-file blue"),
  20. type = C_VISUALATTRIBUTES) -> metadata # to delete too ?
  21. # Generate base trees
  22. metadata %>%
  23. filter(C_HLEVEL == 1) -> explore_tree_df
  24. explore_tree_df %>%
  25. df_to_treelist -> explore_tree
  26. metadata %>%
  27. search_in_df("hyp(o|er)gly", "C_NAME", T) -> search_tree_df
  28. search_tree_df %>%
  29. df_to_tree -> search_tree
  30. server <- shinyServer(function(input, output, session)
  31. {
  32. v <- reactiveValues()
  33. v$df <- explore_tree_df
  34. v$df2 <- search_tree_df
  35. v$requests <- c() # store previous requests for children to prevent doing them twice
  36. v$selected <- NULL
  37. output$explore_tree <- renderTree(explore_tree)
  38. # Controls conditional panel
  39. output$selected <- reactive(length(v$selected) > 0)
  40. # When a node is selected, display information
  41. observeEvent(input$explore_tree,
  42. {
  43. v$selected <- get_selected(input$explore_tree)
  44. if (length(v$selected) > 0)
  45. {
  46. v$c_name <- v$selected[[1]][1]
  47. v$node_ancestors <- attr(v$selected[[1]], 'ancestry')
  48. v$node_path <- get_path(v$c_name, v$node_ancestors)
  49. v$node_line <- get_line(v$df, v$node_path)
  50. v$c_fullname <- v$node_line$C_FULLNAME
  51. v$node_level <- v$node_line$C_HLEVEL
  52. v$is_leaf <- v$node_line$C_VISUALATTRIBUTES %in% c("LA", "RA", "LI")
  53. v$c_basecode <- v$node_line$C_BASECODE
  54. }
  55. })
  56. # Display node info
  57. output$c_basecode <- renderText(v$c_basecode)
  58. output$c_name <- renderText(v$c_name)
  59. output$clip <- renderUI(rclipButton("clipbtn",
  60. v$c_fullname,
  61. str_replace_all(v$c_fullname, fixed("\\"), fixed("\\\\")), # with the button, escape chars are interpreted so \\ have to be doubled
  62. icon("clipboard")))
  63. # render a table with all node whose C_BASECODE match the one of the currently selected node
  64. output$codes <- renderTable(
  65. {
  66. if (length(v$selected)>0)
  67. {
  68. metadata %>%
  69. filter(C_BASECODE == v$c_basecode) %>%
  70. select(C_NAME) %>%
  71. arrange(nchar(C_NAME))
  72. }
  73. })
  74. #Append children
  75. observeEvent(v$selected,
  76. {
  77. if (all(length(v$selected) > 0, # a node was selected
  78. nrow(v$node_line) == 1, # node was found in the dataframe
  79. v$is_leaf == F, # node is not a leaf
  80. !(v$node_path %in% v$requests))) # request was not already done
  81. {
  82. v$requests <- c(v$requests, v$node_path) # store the request
  83. children <- request_children(metadata, v$node_path, v$node_level)
  84. if (nrow(children) > 0) # children found
  85. {
  86. v$df <- v$df %>% bind_rows(children) #%>% distinct()
  87. path_list <- c(v$node_ancestors, v$c_name)
  88. t <- input$explore_tree
  89. # insert the structure(list(), attr = ,) on the corresponding node
  90. pluck(t, !!!path_list) <- structure(df_to_treelist(children), stselected = T, stopened = T, sticon="glyphicon glyphicon-folder-open") # stopened = input$open_children
  91. updateTree(session, "explore_tree", t)
  92. }
  93. }
  94. })
  95. matching_codes <- reactive(
  96. {
  97. if (!is.null(v$c_basecode))
  98. {
  99. metadata %>% filter(C_BASECODE == v$c_basecode)
  100. }
  101. })
  102. matching_code_json <- reactive(
  103. {
  104. matching_codes() %>%
  105. df_to_tree() %>%
  106. treeToJSON()
  107. })
  108. output$matching_code_tree <- renderTree(
  109. {
  110. if (!is.null(v$c_basecode))
  111. {
  112. matching_code_json()
  113. }
  114. })
  115. observeEvent(matching_codes(),
  116. {
  117. updateTree(session, "matching_code_tree", matching_code_json())
  118. })
  119. observeEvent(input$open_matching_code,
  120. {
  121. runjs(HTML('$("#matching_code_tree").jstree("open_all");'))
  122. })
  123. #############
  124. # Search
  125. #############
  126. output$search_tree <- renderTree(
  127. {
  128. treeToJSON(search_tree) # use treeToJSON because there are multiple levels
  129. })
  130. observeEvent(input$search_tree,
  131. {
  132. v$selected_search_tree <- get_selected(input$search_tree)
  133. if (length(v$selected_search_tree) > 0)
  134. {
  135. v$s_name <- v$selected_search_tree[[1]][1]
  136. v$s_node_ancestors <- attr(v$selected_search_tree[[1]], 'ancestry')
  137. v$s_node_path <- get_path(v$s_name, v$s_node_ancestors)
  138. v$s_node_line <- get_line(metadata, v$s_node_path)
  139. v$s_fullname <- v$s_node_line$C_FULLNAME
  140. v$s_node_level <- v$s_node_line$C_HLEVEL
  141. v$s_is_leaf <- v$s_node_line$C_VISUALATTRIBUTES %in% c("LA", "RA", "LI")
  142. v$s_basecode <- v$s_node_line$C_BASECODE
  143. }
  144. })
  145. # command the conditional panel
  146. output$s_selected <- reactive(
  147. {
  148. length(v$selected_search_tree) > 0
  149. })
  150. output$s_basecode <- renderText(v$s_basecode)
  151. output$s_name <- renderText(v$s_name)
  152. output$s_clip <- renderUI(rclipButton("clipbtn",
  153. v$s_fullname,
  154. str_replace_all(v$s_fullname, fixed("\\"), fixed("\\\\")),
  155. icon("clipboard")))
  156. observeEvent(input$perform_search,
  157. {
  158. search_results <- search_in_df(metadata, input$text_to_search, input$search_on, input$use_regex)
  159. if (nrow(search_results) > 0)
  160. {
  161. v$df2 <- search_results
  162. }
  163. })
  164. observeEvent(v$df2,
  165. {
  166. updateTree(session, "search_tree", treeToJSON(df_to_tree(v$df2)))
  167. })
  168. observeEvent(input$open_all,
  169. {
  170. runjs(HTML('$("#search_tree").jstree("open_all");'))
  171. })
  172. outputOptions(output, "selected", suspendWhenHidden = F)
  173. outputOptions(output, "s_selected", suspendWhenHidden = F)
  174. })
  175. shinyApp(ui, server, options = list(host = "0.0.0.0", port = 4321))