---
title: "Aberdeen hospital admissions dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(lubridate) 
library(DataExplorer)
library(kableExtra)
library(scales)
library(ggridges)
library(DT)
library(ggplot2)
library(plotly)
library(shiny)
```
```{r}
hospital_adm_df <- read_csv('https://raw.githubusercontent.com/AbdnCHDS/skills-test/main/hospital_admissions.csv')
pop_dem_df <- read_csv('https://raw.githubusercontent.com/AbdnCHDS/skills-test/main/population_demographics.csv')
dt <- left_join(hospital_adm_df, pop_dem_df, by = "id")
### After the dataset "summary" check, 3 things require attention. 
dt <- dt %>% 
  mutate(date_admit = dmy(date_admit),
         date_discharge = dmy(date_discharge))
dt$date_discharge <- as.Date(sub('2002', '2020', dt$date_discharge))
dt$hosp_length <- difftime(dt$date_discharge ,dt$date_admit , units = c("days"))
dt$hosp_length <- as.numeric(as.difftime(dt$hosp_length))
dt$age <- as.numeric(sub('999','99',dt$age))
dt$age_group <- case_when(
  dt$age >= 0  & dt$age <= 13 ~ 'Children',
  dt$age >= 14  & dt$age <= 17 ~ 'Young adults',
  dt$age >= 18  & dt$age <= 65 ~ 'Adults',
  dt$age >= 65  & dt$age <= 99 ~ 'Elderly')
dt$age_group <- as.factor(dt$age_group)
```
Part 1
=======================================================================
Column {data-width=400, .tabset}
-----------------------------------------------------------------------
### Age bar chart
```{r}
p1 <- ggplot(data= dt, aes(age, fill = factor(deprivation))) +
  geom_bar() +
  labs(x = "age", y = "") +
  theme_light()
ggplotly(p1, height= 500, width= 700)
```
### Hospital days distribution
```{r}
p2 <- dt %>%
  filter(!is.na(deprivation)) %>%
  ggplot(aes(x = hosp_length, fill = factor(deprivation))) +
  geom_density(alpha=0.4, position = "stack") +
  theme_light()
ggplotly(p2, height= 450, width= 700) 
```
Column {data-width=300}
-----------------------------------------------------------------------
### Dataset
```{r}
datatable(dt, extensions = 'Buttons',
          options = list(pageLength = 15,
                         dom = 'Bfrtip',
                             buttons = c('copy','excel','print')),rownames = FALSE)
```
Part 2
=======================================================================
Column {data-width=450}
-----------------------------------------------------------------------
### boxplot distribution by age groups and days in hosp.
```{r}
p<-dt %>%
  filter(!is.na(deprivation)) %>%
  ggplot( aes(x = age_group, y = hosp_length, fill = age_group)) +
  geom_violin(alpha = 0.5, size = .2) +
  geom_jitter(alpha = .2, width = .2) +
  geom_boxplot(alpha = .2) +
  theme(legend.position = "none") +
  labs(x = "age groups", y = "number of days in hospital")
ggplotly(p, height=500, width=700)
```
Column {data-width=300}
-----------------------------------------------------------------------
### Grouped dataset info
```{r, fig.width=50, fig.height=15}
dt1<-dt %>%
  group_by(age, deprivation) %>%
  summarise(tot_hosp_length = sum(hosp_length)) %>%
  arrange(desc(tot_hosp_length))
datatable(dt1, extensions = 'Buttons',
          options = list(pageLength = 15,
                         dom = 'Bfrtip',
                             buttons = c('copy','excel','print')),rownames = FALSE)
```