---
title: "`r renderText({input$drug})`"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
library(flexdashboard)
library(stringr)
library(openfda)
library(dplyr)

library(ggplot2)
library(ggthemes)

get_adverse <- function(gender, brand_name, age) {
  fda_query("/drug/event.json") %>%
    fda_filter("patient.drug.openfda.brand_name", brand_name) %>% 
    fda_filter("patient.patientsex", gender) %>% 
    fda_filter("patient.patientonsetage", age) %>% 
    fda_count("patient.reaction.reactionmeddrapt.exact") %>% 
    fda_limit(10) %>% 
    fda_exec()
}

# %d is a placeholder for a integer variable inside a string
create_age <- function(min, max){#
  sprintf('[%d+TO+%d]', min, max)
}
```


```{r inputs, echo = FALSE}

textInput('drug', 'Brand Name Drug', value = "Prednisone")

sliderInput('ages', 'Age Range', min = 10, max = 80, value = c(20,60))


age <- reactive({create_age(input$ages[1],input$ages[2])})

male <- reactive({
  ad <- get_adverse("1", input$drug, age())
  if (!is.null(ad)) {
    ad$gender = 'male'
  }
  ad
})

female <- reactive({
  ad <- get_adverse("2", input$drug, age())
  if (!is.null(ad)) {
    ad$gender = 'female'
  }
  ad
})

adverse <- reactive({
  req(male(), female())
  rbind(male(), female())
})

events <- reactive({
  req(adverse())
  adverse() %>%
    group_by(term) %>%
    summarise(count = sum(count))
})
```

### All Events

The graph below displays the *adverse events* reported for `r renderText({input$drug})`, the most common of which is `r renderText({events()$term[which.max(events()$count)]})`.

```{r echo = FALSE}
renderPlot({
  req(adverse())
  adverse() %>% 
  group_by(term) %>% 
  summarise(count = sum(count)) %>% 
  ggplot() +
    geom_bar(aes(reorder(term,count), count), stat = 'identity') +
    coord_flip() +
    labs(
      title = input$drug,
      x = NULL,
      y = NULL
    ) +
    theme_minimal()
})

```

### Age Range

```{r echo = FALSE}
age_label <- reactive({
  str_replace_all(age(), fixed("+"), " ") %>% 
    str_replace(fixed("["), "") %>% 
    str_replace(fixed("]"), "") %>% 
    str_replace(fixed("TO"), "-")     
})
renderValueBox({valueBox(age_label(), icon = 'glyphicon-user')})
```

### Events by Gender

```{r echo = FALSE}
renderPlot({
  req(adverse())
  ggplot(adverse()) +
    geom_bar(aes(reorder(term,count), count, fill = gender), stat = 'identity') +
    facet_wrap(~gender)+
    coord_flip() +
    labs(
      title = input$drug,
      x = NULL,
      y = NULL
    ) +
    theme_minimal() + 
    guides(fill = FALSE) + 
    scale_fill_manual(values = c("#d54a30","#4c83b6"))
})
```

## Appendix 1

The raw data is available below.

```{r echo = FALSE}
# Use DT, an htmlwidget, to create an interactive table
renderDataTable({adverse()}, options = list(pageLength = 5))
```
