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.

274 lines
7.9KB

  1. server <- shinyServer(function(input, output, session)
  2. {
  3. ## updateDb <- reactiveTimer(4 * 60 * 1000, session)
  4. db <- reactive(
  5. {
  6. req(input$CESP)
  7. ## updateDb()
  8. db <- dbConnect(SQLite(), "ecn.db", on.exit(dbDisconnect(db), add = T))
  9. if (input$CESP == "CESP")
  10. {
  11. db %>%
  12. tbl("trajectoires") %>%
  13. filter(CESP == "CESP") %>%
  14. collect %>%
  15. rename(Date = timestamp)
  16. } else
  17. {
  18. db %>%
  19. tbl("trajectoires") %>%
  20. filter(CESP == "") %>%
  21. collect %>%
  22. rename(Date = timestamp)
  23. }
  24. })
  25. output$dates <- renderUI(
  26. {
  27. req(db())
  28. db()$Date %>%
  29. range %>%
  30. map(~as.POSIXct(.x) %>%
  31. format(format = "%A %d %B %Y %H:%M") %>%
  32. str_to_title) -> epoch
  33. list(p(str_c("Données enregistrées du ", epoch[1], " au ", epoch[2])))
  34. })
  35. # Listing ----
  36. listing <- reactive(
  37. {
  38. db() %>%
  39. group_by(Etudiant) %>%
  40. filter(Date == max(Date)) %>%
  41. ungroup %>%
  42. mutate(Etudiant = Etudiant %>% as.numeric,
  43. Subdivision = Subdivision %>% as.factor,
  44. Discipline = Discipline %>% as.factor,
  45. Etat = Etat %>% as.factor) %>%
  46. arrange(Etudiant) %>%
  47. select(Etudiant, Etat, Absence, `Vœu`, Subdivision, Discipline, Rang, Date)
  48. })
  49. output$tableListing <- DT::renderDataTable(
  50. {
  51. listing()
  52. },
  53. options = list(paging = F, info = F),
  54. selection = "none",
  55. rownames = F,
  56. filter = "top"
  57. )
  58. # Etudiant ----
  59. listingEtudiant <- reactive(
  60. {
  61. req(input$Etudiant)
  62. db() %>%
  63. filter(Etudiant == input$Etudiant) %>%
  64. arrange(Date) %>%
  65. select(Etudiant, Date, Etat, `Vœu`, Subdivision, Discipline, Rang) %>%
  66. collect
  67. })
  68. tableEtudiant <- reactive(
  69. {
  70. req(listingEtudiant())
  71. if (!input$Rang)
  72. {
  73. listingEtudiant() %>%
  74. select(-Rang, -Etudiant) %>%
  75. uniq(Discipline, Subdivision, first = T) -> lst
  76. } else
  77. {
  78. listingEtudiant() -> lst
  79. }
  80. lst
  81. })
  82. output$tableEtudiant <- DT::renderDataTable(
  83. {
  84. req(tableEtudiant())
  85. tableEtudiant()
  86. },
  87. options = list(paging = F, searching = F, info = F),
  88. selection = "none",
  89. rownames = F
  90. )
  91. plotEtudiant <- reactive(
  92. {
  93. req(listingEtudiant())
  94. db()$Date %>% max -> epoch
  95. listingEtudiant() %>%
  96. select(Etudiant, Subdivision, Discipline, Date, Vœu) %>%
  97. uniq(Subdivision, Discipline, first = T) %>%
  98. add_row(Date = epoch, Etudiant = last(.$Etudiant), Subdivision = last(.$Subdivision), Discipline = last(.$Discipline)) %>%
  99. unite(Choix, Subdivision, Discipline, sep = " - ") %>%
  100. mutate(Choix = Choix %>% factor %>% fct_inorder) %>%
  101. mutate(Date = Date %>% as.POSIXct)
  102. })
  103. output$plotEtudiant <- renderPlotly(
  104. {
  105. req(plotEtudiant())
  106. plotEtudiant() %>%
  107. ggplot(aes(x = Date, y = Choix, group = Etudiant)) +
  108. geom_step() +
  109. geom_point() +
  110. theme(axis.text.x = element_text(angle = -45, hjust = 0)) +
  111. scale_y_discrete(limits = plotEtudiant()$Choix %>% levels %>% rev)
  112. })
  113. # VilleSpe ----
  114. listingVilleSpe <- reactive(
  115. {
  116. req(input$Ville, input$Spe)
  117. db() %>%
  118. filter(Subdivision == input$Ville,
  119. Discipline == input$Spe) -> lst
  120. if (nrow(lst) == 0)
  121. {
  122. NULL
  123. }
  124. else
  125. {
  126. lst$Etudiant %>%
  127. unique -> etudiants
  128. db() %>%
  129. filter(Etudiant %in% etudiants) %>%
  130. mutate(Etudiant = Etudiant %>% factor,
  131. Rang = if(Rang %>% is.character) Rang %>% str_replace("(\\d+)(/\\d+)?", "\\1") %>% as.numeric else Rang) %>%
  132. mutate(Rang = ifelse(Subdivision != input$Ville | Discipline != input$Spe, -1, Rang)) %>%
  133. select(Date, Etudiant, Rang) %>%
  134. uniq(Etudiant, Rang) %>%
  135. complete(Etudiant, Date) %>%
  136. by(.$Etudiant, function(df)
  137. {
  138. which(df$Rang %>% is.na & !lead(df$Rang) %>% is.na) -> chg
  139. which(!df$Rang %>% is.na & lead(df$Rang) %>% is.na) -> chg2
  140. df$Rang[setdiff(chg, 1)] <- df$Rang[chg2]
  141. if (last(df$Rang) %>% is.na)
  142. df$Rang[length(df$Rang)] <- na.omit(df$Rang)[length(na.omit(df$Rang))]
  143. df %>%
  144. filter(!Rang %>% is.na)
  145. }) %>%
  146. Reduce(f = bind_rows) %>%
  147. select(Date, Etudiant, Rang) %>%
  148. mutate(Rang = ifelse(Rang == -1, NA, Rang))
  149. }
  150. })
  151. tableVilleSpe <- reactive (
  152. {
  153. req(listingVilleSpe())
  154. listingVilleSpe() %>%
  155. select(Date, Etudiant, Rang) %>%
  156. group_by(Etudiant) %>%
  157. filter(!Rang %>% is.na) %>%
  158. filter(Date == max(Date)) %>%
  159. ungroup %>%
  160. arrange(Date)
  161. })
  162. output$tableVilleSpe <- DT::renderDataTable(
  163. {
  164. req(tableVilleSpe())
  165. tableVilleSpe()
  166. },
  167. options = list(paging = F, searching = F, info = F),
  168. selection = "none",
  169. rownames = F
  170. )
  171. output$plotVilleSpe <- renderPlotly(
  172. {
  173. req(listingVilleSpe())
  174. listingVilleSpe() %>%
  175. mutate(Date = Date %>% as.POSIXct) %>%
  176. ggplot(aes(x = Date, y = Rang, color = Etudiant, key = Etudiant)) +
  177. geom_line() +
  178. geom_point(data = listingVilleSpe() %>%
  179. mutate(Date = Date %>% as.POSIXct) %>%
  180. group_by(Etudiant) %>%
  181. filter(!Rang %>% is.na & (lag(Rang) %>% is.na | lead(Rang) %>% is.na))) +
  182. scale_y_continuous(trans = "reverse", breaks = unique(listingVilleSpe()$Rang))
  183. })
  184. # Interactivity ----
  185. observeEvent(input$tableListing_cell_clicked,
  186. {
  187. info = input$tableListing_cell_clicked
  188. if (is.null(info$value)) return()
  189. if (info$col == 0)
  190. {
  191. updateNumericInput(session, "Etudiant", value = listing()$Etudiant[info$row])
  192. updateTabsetPanel(session, "nav", selected = "Par étudiant")
  193. } else if (info$col %in% 4:5)
  194. {
  195. updateSelectInput(session, "Ville", selected = listing()$Subdivision[info$row])
  196. updateSelectInput(session, "Spe", selected = listing()$Discipline[info$row])
  197. updateTabsetPanel(session, "nav", selected = "Par discipline et subdivision")
  198. }
  199. })
  200. observeEvent(input$tableEtudiant_cell_clicked,
  201. {
  202. info = input$tableEtudiant_cell_clicked
  203. if (is.null(info$value)) return()
  204. updateSelectInput(session, "Ville", selected = tableEtudiant()$Subdivision[info$row])
  205. updateSelectInput(session, "Spe", selected = tableEtudiant()$Discipline[info$row])
  206. updateTabsetPanel(session, "nav", selected = "Par discipline et subdivision")
  207. })
  208. observeEvent(input$tableVilleSpe_cell_clicked,
  209. {
  210. info = input$tableVilleSpe_cell_clicked
  211. if (is.null(info$value)) return()
  212. updateNumericInput(session, "Etudiant", value = tableVilleSpe()$Etudiant[info$row])
  213. updateTabsetPanel(session, "nav", selected = "Par étudiant")
  214. })
  215. observeEvent(event_data("plotly_click"),
  216. {
  217. info <- event_data("plotly_click")
  218. if (!info$key %>% is.null)
  219. {
  220. updateNumericInput(session, "Etudiant", value = info$key)
  221. updateTabsetPanel(session, "nav", selected = "Par étudiant")
  222. } else
  223. {
  224. plotEtudiant()$Choix %>% factor %>% fct_inorder %>% levels %>% rev -> choix
  225. choix[info$y] %>% str_replace("(.*?) - .*", "\\1") -> ville
  226. choix[info$y] %>% str_replace(".*? - (.*)", "\\1") -> spe
  227. updateSelectInput(session, "Ville", selected = ville)
  228. updateSelectInput(session, "Spe", selected = spe)
  229. updateTabsetPanel(session, "nav", selected = "Par discipline et subdivision")
  230. }
  231. })
  232. })