---
output:
  html_document:
    toc: true
    toc_float: true
params:
  CRH: NA
  CRH_seance: NA
  CRH_sans_seance: NA
---

```{r init, warning = F, message = F, echo = F}
library(tidyverse)
library(DT)
library(magrittr)
library(stringr)
library(knitr)
library(lubridate)
library(plotly)

opts_chunk$set(warning = F,
               message = F,
               fig.width = 9,
               fig.height = 6,
               echo = F)
options(DT.options = list(paging = F,
                          info = F,
                          searching = F,
                          dom = "Bfrtip",
                          buttons = c('copy', 'excel')))
```

```{r data, echo = F, message = F, warning = F}
mois_label <- c("Janvier",
                "Février",
                "Mars",
                "Avril",
                "Mai",
                "Juin",
                "Juillet",
                "Août",
                "Septembre",
                "Octobre",
                "Novembre",
                "Décembre")

pdf(NULL)
```

# Tous RUMs confondus

## Par mois

```{r}
CRH() %>%
  count(Mois, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  full_join(CRH() %>% count(Mois)) %>%
  select(Mois, RUM = n, CRHP, CRHA, Manquant) %>%
  full_join(CRH() %>% group_by(Mois) %>% summarise(`Délai médian (j)` = median(Delai))) %>%
  mutate(CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         `% absents` = CRHA / RUM) %>%
  datatable(rownames = F,
            extensions = 'Buttons',
            colnames = c("Non renseigné" = "Manquant",
                         "#CRHA" = "CRHA",
                         "#CRHP" = "CRHP")) %>%
  formatPercentage(7)
```
<br/>  
```{r}
CRH() %>%

  ggplot(aes(x = Mois, fill = Notes)) +
  geom_bar(position = "fill") +
  ylab("Proportion") +
  theme(axis.text.x = element_text(angle = -45, hjust = 1)) +
  ggtitle("Proportion des #CRHA et #CRHP par mois de sortie du RSS") -> p

  ggplotly(p)
```

## Par Pôle

```{r}
CRH() %>%
  count(Pole, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  full_join(CRH() %>% count(Pole)) %>%
  select(Pole, RUM = n, CRHP, CRHA, Manquant) %>%
  full_join(CRH() %>% group_by(Pole) %>% summarise(`Délai médian (j)` = median(Delai))) %>%
  mutate(CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         `% absents` = CRHA / RUM) %>%
  datatable(rownames = F,
            extensions = 'Buttons',
            colnames = c("Pôle" = "Pole",
                         "Non renseigné" = "Manquant",
                         "#CRHA" = "CRHA",
                         "#CRHP" = "CRHP")) %>%
  formatPercentage(7)
```
<br/>  
```{r}
CRH() %>%

  ggplot(aes(x = Pole, fill = Notes)) +
  geom_bar(position = "fill") +
  ylab("Proportion") +
  ggtitle("Proportion des #CRHA et #CRHP par pôle, depuis avril 2016") +
  theme(axis.text.x = element_text(angle = -45, hjust = 1)) -> p

  ggplotly(p)

```

## Par Pôle, par service et par mois

```{r}
CRH() %>%
  count(Pole, Service, Mois, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  full_join(CRH() %>% count(Pole, Service, Mois)) %>%
  ungroup %>%
  select(Pole, Service, Mois, RUM = n, CRHP, CRHA, Manquant) %>%
  full_join(CRH() %>% group_by(Pole, Service, Mois) %>% summarise(`Délai médian (j)` = median(Delai))) %>%
  mutate(CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         `% absents` = CRHA / RUM,
         Pole = Pole %>% factor,
         Mois = Mois %>% factor) %>%
  datatable(rownames = F,
            extensions = 'Buttons',
            colnames = c("Pôle" = "Pole",
                         "Non renseigné" = "Manquant",
                         "#CRHA" = "CRHA",
                         "#CRHP" = "CRHP"),
            filter = "top",
            options = list(searching = T, paging = T)) %>%
  formatPercentage(9)
```
<br/>  
```{r}
CRH() %>%
  count(Pole, Mois, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  mutate(Manquant = ifelse(is.na(Manquant), integer(1), Manquant),
         CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         pourcent = CRHA / (CRHA + CRHP + Manquant)) %>%

  ggplot(aes(x = Mois, y = pourcent, colour = Pole, group = Pole)) +
  geom_line(stat = "identity") +
  geom_point() +
  theme(axis.text.x = element_text(angle = -45, hjust = 1)) +
  ylab("Proportion de #CRHA") +
  ggtitle("Évolution des proportions de #CRHA par pôle") -> p

  ggplotly(p, tooltip = c("x", "y", "colour"))
```

----

# RUMs non séance

## Par mois

```{r}
CRH_sans_seance() %>%
  count(Mois, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  full_join(CRH_sans_seance() %>% count(Mois)) %>%
  select(Mois, RUM = n, CRHP, CRHA, Manquant) %>%
  full_join(CRH_sans_seance() %>% group_by(Mois) %>% summarise(`Délai médian (j)` = median(Delai))) %>%
  mutate(CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         `% absents` = CRHA / RUM) %>%
  datatable(rownames = F,
            extensions = 'Buttons',
            colnames = c("Non renseigné" = "Manquant",
                         "#CRHA" = "CRHA",
                         "#CRHP" = "CRHP")) %>%
  formatPercentage(7)
```
<br/>  
```{r}
CRH_sans_seance() %>%

  ggplot(aes(x = Mois, fill = Notes)) +
  geom_bar(position = "fill") +
  theme(axis.text.x = element_text(angle = -45, hjust = 1)) +
  ylab("Proportion") +
  ggtitle("Proportion des #CRHA et #CRHP par mois de sortie du RSS") -> p

  ggplotly(p)
```

## Par Pôle

```{r}
CRH_sans_seance() %>%
  count(Pole, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  full_join(CRH_sans_seance() %>% count(Pole)) %>%
  select(Pole, RUM = n, CRHP, CRHA, Manquant) %>%
  full_join(CRH_sans_seance() %>% group_by(Pole) %>% summarise(`Délai médian (j)` = median(Delai))) %>%
  mutate(CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         `% absents` = CRHA / RUM) %>%
  datatable(rownames = F,
            extensions = 'Buttons',
            colnames = c("Pôle" = "Pole",
                         "Non renseigné" = "Manquant",
                         "#CRHA" = "CRHA",
                         "#CRHP" = "CRHP")) %>%
  formatPercentage(7)
```
<br/>  
```{r}
CRH_sans_seance() %>%

  ggplot(aes(x = Pole, fill = Notes)) +
  geom_bar(position = "fill") +
  ylab("Proportion") +
  ggtitle("Proportion des #CRHA et #CRHP par pôle, depuis avril 2016") +
  theme(axis.text.x = element_text(angle = -45, hjust = 1)) -> p

  ggplotly(p)

```

## Par Pôle, par service et par mois

```{r}
CRH_sans_seance() %>%
  count(Pole, Service, Mois, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  full_join(CRH_sans_seance() %>% count(Pole, Service, Mois)) %>%
  ungroup %>%
  select(Pole, Service, Mois, RUM = n, CRHP, CRHA, Manquant) %>%
  full_join(CRH_sans_seance() %>% group_by(Pole, Service, Mois) %>% summarise(`Délai médian (j)` = median(Delai))) %>%
  mutate(CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         `% absents` = CRHA / RUM,
         Pole = Pole %>% factor,
         Mois = Mois %>% factor) %>%
  datatable(rownames = F,
            extensions = 'Buttons',
            colnames = c("Pôle" = "Pole",
                         "Non renseigné" = "Manquant",
                         "#CRHA" = "CRHA",
                         "#CRHP" = "CRHP"),
            filter = "top",
            options = list(searching = T, paging = T)) %>%
  formatPercentage(9)
```
<br/>  
```{r}
CRH_sans_seance() %>%
  count(Pole, Mois, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  mutate(Manquant = ifelse(is.na(Manquant), integer(1), Manquant),
         CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         pourcent = CRHA / (CRHA + CRHP + Manquant)) %>%

  ggplot(aes(x = Mois, y = pourcent, colour = Pole, group = Pole)) +
  geom_line(stat = "identity") +
  geom_point() +
  theme(axis.text.x = element_text(angle = -45, hjust = 1)) +
  ylab("Proportion de #CRHA") +
  ggtitle("Évolution des proportions de #CRHA par pôle") -> p

  ggplotly(p, tooltip = c("x", "y", "colour"))
```

----

# RUMs séance

## Par mois

```{r}
CRH_seance() %>%
  count(Mois, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  full_join(CRH_seance() %>% count(Mois)) %>%
  select(Mois, RUM = n, CRHP, CRHA, Manquant) %>%
  full_join(CRH_seance()  %>% group_by(Mois) %>% summarise(`Délai médian (j)` = median(Delai))) %>%
  mutate(CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         `% absents` = CRHA / RUM) %>%
  datatable(rownames = F,
            extensions = 'Buttons',
            colnames = c("Non renseigné" = "Manquant",
                         "#CRHA" = "CRHA",
                         "#CRHP" = "CRHP")) %>%
  formatPercentage(7)
```
<br/>  
```{r}
CRH_seance() %>%

  ggplot(aes(x = Mois, fill = Notes)) +
  geom_bar(position = "fill") +
  theme(axis.text.x = element_text(angle = -45, hjust = 1)) +
  ylab("Proportion") +
  ggtitle("Proportion des #CRHA et #CRHP par mois de sortie du RSS") -> p

  ggplotly(p)
```

## Par Pôle

```{r}
CRH_seance() %>%
  count(Pole, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  full_join(CRH_seance() %>% count(Pole)) %>%
  select(Pole, RUM = n, CRHP, CRHA, Manquant) %>%
  full_join(CRH_seance()  %>% group_by(Pole) %>% summarise(`Délai médian (j)` = median(Delai))) %>%
  mutate(CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         `% absents` = CRHA / RUM) %>%
  datatable(rownames = F,
            extensions = 'Buttons',
            colnames = c("Pôle" = "Pole",
                         "Non renseigné" = "Manquant",
                         "#CRHA" = "CRHA",
                         "#CRHP" = "CRHP")) %>%
  formatPercentage(7)

```
<br/>  
```{r}
CRH_seance() %>%

  ggplot(aes(x = Pole, fill = Notes)) +
  geom_bar(position = "fill") +
  ylab("Proportion") +
  ggtitle("Proportion des #CRHA et #CRHP par pôle, depuis avril 2016") +
  theme(axis.text.x = element_text(angle = -45, hjust = 1)) -> p

  ggplotly(p)

```

## Par Pôle, par service et par mois

```{r}
CRH_seance() %>%
  count(Pole, Service, Mois, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  full_join(CRH_seance() %>% count(Pole, Service, Mois)) %>%
  ungroup %>%
  select(Pole, Service, Mois, RUM = n, CRHP, CRHA, Manquant) %>%
  full_join(CRH_seance()  %>% group_by(Pole, Service, Mois) %>% summarise(`Délai médian (j)` = median(Delai))) %>%
  mutate(CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         `% absents` = CRHA / RUM,
         Pole = Pole %>% factor,
         Mois = Mois %>% factor) %>%
  datatable(rownames = F,
            extensions = 'Buttons',
            colnames = c("Pôle" = "Pole",
                         "Non renseigné" = "Manquant",
                         "#CRHA" = "CRHA",
                         "#CRHP" = "CRHP"),
            filter = "top",
            options = list(searching = T, paging = T)) %>%
  formatPercentage(9)

```
<br/>  
```{r}
CRH_seance() %>%
  count(Pole, Mois, Notes) %>%
  mutate(Notes = ifelse(is.na(Notes), "Manquant", Notes)) %>%
  spread(Notes, n) %>%
  mutate(Manquant = ifelse(is.na(Manquant), integer(1), Manquant),
         CRHA = ifelse(is.na(CRHA), integer(1), CRHA),
         CRHP = ifelse(is.na(CRHP), integer(1), CRHP),
         pourcent = CRHA / (CRHA + CRHP + Manquant)) %>%

  ggplot(aes(x = Mois, y = pourcent, colour = Pole, group = Pole)) +
  geom_line(stat = "identity") +
  geom_point() +
  theme(axis.text.x = element_text(angle = -45, hjust = 1)) +
  ylab("Proportion de #CRHA") +
  ggtitle("Évolution des proportions de #CRHA par pôle") -> p

  ggplotly(p, tooltip = c("x", "y", "colour"))
```
