---
title: "Paediatric Growth Data"
author: "Paridhi"
date: "29/04/2022"
runtime: shiny
output: html_document

---

```{r setup, include=FALSE, echo=FALSE}

library(plotly)
options(warn = -1)

library(tidyr)
library(ggplot2)
library(dplyr)
library(readr)
```

```{r, echo = FALSE}
  radioButtons("yScale",
               label = "Scale",
  #                #HTML('<FONT color="black"><FONT size="5pt">Y-Scale</FONT></FONT><br> #<b>Your favorite color is red ?</b>'),
               choices = c("Z-score", "Percentile"),
               selected = "Z-score",
               inline = T,
               width = "100%")

# par(mfrow = c(2, 1))

# bp1
# bp2

```

```{r, echo = FALSE, include=FALSE}
PedsGrowth <- read_csv("data/2022/2022-05-11/PedsGrowth.csv")
 PedsGrowth$TIME <- as.factor(PedsGrowth$TIME)
 width <- PedsGrowth %>% group_by(MEASURE, TIME) %>% count()
subset <- reactive({
  if(input$yScale == "Z-score"){
  "ZSCORES"
  }else{
      "PERCENTILES"
  }
})

ylabel <- reactive({
  if(input$yScale == "Z-score")
    "Z-score"
  else
    "Percentile"
})
shading <- reactive({
  if(input$yScale == "Z-score"){
    c(-1, 1)
  }
  # else if(input$yScale == "Z-score" & input$data == "Change"){
  #   c(-0.5, 0.5)
  # }
  else
    c(0.25, 0.75)
    
})
output$bp1 <- renderPlot({
  if(subset() == "ZSCORES"){
  PedsGrowth %>% filter(complete.cases(.)) %>% group_by(MEASURE, TIME) %>% ggplot(aes(x = TIME, y = ZSCORES)) +          
  geom_boxplot() + facet_wrap(.~MEASURE, scales="free_y", ncol=1) + theme_minimal() +
  stat_summary(fun = mean, geom = "point", col = "red") +
      scale_x_discrete(limits=0:76, breaks = seq(0,76,4), labels = paste(seq(0,76,4), "Weeks")) + scale_y_discrete(limits=-3:3, breaks = seq(-3,3,1)) + 
      annotate("rect", xmin = -Inf, xmax = Inf, ymin = shading()[1], ymax = shading()[2], 
           alpha = .2, fill = "grey") + theme(panel.grid.major.x = element_blank())
  }
  else
  {
    PedsGrowth %>% filter(complete.cases(.)) %>% group_by(MEASURE, TIME) %>% ggplot(aes(x = TIME, y = PERCENTILES)) +          
  geom_boxplot() + facet_wrap(.~MEASURE, scales="free_y", ncol=1) + theme_minimal() +
  stat_summary(fun = mean, geom = "point", col = "red") +
      scale_x_discrete(limits=0:76, breaks = seq(0,76,4), labels = paste(seq(0,76,4), "Weeks")) +
      annotate("rect", xmin = -Inf, xmax = Inf, ymin = shading()[1], ymax = shading()[2], 
           alpha = .2, fill = "grey") + theme(panel.grid.major.x = element_blank())
  }
  })

```

```{r, echo = FALSE}
plotOutput("bp1")
```

