Fisseha Berhane, PhD

Data Scientist

443-970-2353 fisseha@jhu.edu CV Resume Linkedin GitHub twitter twitter

A Shiny Dashboard of Adverse Drug Event Reports

This is a shiny dashboard developed using openFDA data. The data is in JSON format. The R library jsonlite is used to access the data from the openFDA website and change it to data frame. The user can select one , a couple or all types of events.


The dashboard can be accessed from Rstudio here.

The ui.R and server.R codes are shown below.

In [ ]:
# start user interface ----
library(shiny)
library(shinydashboard)

dashboardPage(
dashboardHeader(),

dashboardSidebar(
        
                     
                     
                     
                     # Filter events
                     
                     br(),
                     h4(strong(tags$u(em("Filter",style="color:#FFA319;font-size:150%")))),
                     
                     br(),
                     
                     checkboxInput("all", 
                                   label = strong("All adverse event reports ",style="color:#80E680;font-size:130%")),
                     br(),
                     
                     checkboxInput("manufacturer", label = strong("Reported through manufacturers",style="color:#80E680;font-size:130%")),
                     
                     br(),
                     
                    
                     
                     checkboxInput("public", label = strong("Reported directly by public",style="color:#80E680;font-size:130%")),
                     
                     br(),
                     checkboxInput("hypertension", 
                                   label = strong("Where indication for drug use was hypertension  ",style="color:#80E680;font-size:130%")),
                     
                     br()
                     
                     
        ),
        
        
        #####
        ##  Main Panel
        #### help ====        
dashboardBody(    
fluidRow(
            br(),
            h5(em(strong("Adverse drug event reports since 2004", style="color:darkblue;font-size:210%")),align = "center"),
            
        
            
            plotOutput("myplot", height = 380)
            
            
            
            
        )
        ))
In [ ]:
# Load libraries ====

library(shiny)
library(jsonlite)
library(ggplot2)
library(lubridate)
library(dplyr)
library(gridExtra)

fda<-fromJSON("https://api.fda.gov/drug/event.json?search=receivedate:[20040101+TO+20150101]&count=receivedate")
fda<-fda$results
fda$time=strptime(fda$time, "%Y%m%d")

fda$year<-year(fda$time)
fda$month<-month(fda$time)
fda<-select(fda,count,year,month)

fda2<-fda%>%group_by(year,month)%>%summarise(count = sum(count))

fda2$time=paste0(fda2$year,"-",fda2$month,"-1")

fda2$time=strptime(fda2$time,"%Y-%m-%d")

all_drug<-fda2[,3:4]

fda<-fromJSON("https://api.fda.gov/drug/event.json?search=receivedate:[20040101+TO+20150101]+AND+_exists_:companynumb&count=receivedate")
fda<-fda$results
fda$time=strptime(fda$time, "%Y%m%d")

fda$year<-year(fda$time)
fda$month<-month(fda$time)
fda<-select(fda,count,year,month)

fda2<-fda%>%group_by(year,month)%>%summarise(count = sum(count))

fda2$time=paste0(fda2$year,"-",fda2$month,"-1")

fda2$time=strptime(fda2$time,"%Y-%m-%d")

manufacturer_drug<-fda2[,3:4]

fda<-fromJSON("https://api.fda.gov/drug/event.json?search=receivedate:[20040101+TO+20150101]+AND+_missing_:companynumb&count=receivedate")
fda<-fda$results
fda$time=strptime(fda$time, "%Y%m%d")

fda$year<-year(fda$time)
fda$month<-month(fda$time)
fda<-select(fda,count,year,month)

fda2<-fda%>%group_by(year,month)%>%summarise(count = sum(count))

fda2$time=paste0(fda2$year,"-",fda2$month,"-1")

fda2$time=strptime(fda2$time,"%Y-%m-%d")

public_drug<-fda2[,3:4]

fda<-fromJSON("https://api.fda.gov/drug/event.json?search=receivedate:[20040101+TO+20150101]+AND+patient.drug.drugindication:hypertension&count=receivedate")
fda<-fda$results
fda$time=strptime(fda$time, "%Y%m%d")

fda$year<-year(fda$time)
fda$month<-month(fda$time)
fda<-select(fda,count,year,month)

fda2<-fda%>%group_by(year,month)%>%summarise(count = sum(count))

fda2$time=paste0(fda2$year,"-",fda2$month,"-1")

fda2$time=strptime(fda2$time,"%Y-%m-%d")

hypertension_drug<-fda2[,3:4]

myfig<-function(data)
    
    {
    
    ggplot(data,aes(x=time,y=count))+geom_line(color='blue')+
        theme(plot.title = element_text(size = 18,colour="#00007A"))+
        xlab('')+theme(axis.title.y = element_text(colour="#00007A",size=14,angle=90,hjust=.5,vjust=1),
                       axis.text.y = element_text(colour="darkred",size=14,angle=0,hjust=1,vjust=0),
                       axis.text.x = element_text(colour="darkred",size=14,angle=0,hjust=1,vjust=0))
}

shinyServer(function(input, output) {
    
    alldrug <- reactive({
            if(input$all==T) {
                all_drug
            }
        })
    
    manufacturerdrug <- reactive({
        if(input$manufacturer==T) {
            manufacturer_drug
        }
    })
    
    publicdrug <- reactive({
        if(input$public==T) {
            public_drug
        }
    })
    
    
    hypertensiondrug <- reactive({
        if(input$hypertension==T) {
            hypertension_drug
        }
    })
    
    gall<-reactive({
        myfig(alldrug())+ggtitle("All adverse event reports ")
    })
    
    gmanufacturer<-reactive({
        myfig(manufacturerdrug())+ggtitle("Reported through manufacturers ")
            
    })  
    
    gpublic<-reactive({
        myfig(publicdrug())+ggtitle("Reported directly by public ")
            
    })
    
    
    ghypertension<-reactive({
        myfig(hypertensiondrug())+ggtitle("Where indication for drug use was hypertension")

    })
    
    
    indices <- reactive({
        z<-c(length(alldrug()),length(manufacturerdrug()),
          length(publicdrug()),length(hypertensiondrug()))
        which(z>0)
    })
   
    output$myplot <- renderPlot({
        
        if(length(indices())==1){
            
            if(indices()==1){
                gall()
             }
            
            else if(indices()==2){
                gmanufacturer() 
            }
        
            else if(indices()==3){
                gpublic()
            }
        
        
            else if(indices()==4){
                ghypertension() 
            }
        }
        
        
        else if(length(indices())==2){
            if (all(c(1,2) ==indices())==T){
                grid.arrange(gall(), gmanufacturer(),ncol=2)
            }
            
            else if (all(c(1,3)==indices())==T){
                grid.arrange(gall(), gpublic(),ncol=2)
            }
            
            else if (all(c(1,4) ==indices())==T){
                grid.arrange(gall(), ghypertension(),ncol=2)
            }
            
            else if (all(c(2,3) ==indices())==T){
                grid.arrange(gmanufacturer(), gpublic(),ncol=2)
            }
            
            else if (all(c(2,4) ==indices())==T){
                grid.arrange(gmanufacturer(), ghypertension(),ncol=2)
            }
            
            else if (all(c(3,4)==indices())==T){
                grid.arrange(gpublic(), ghypertension(),ncol=2)
            }
            
        }
        
        
        else if(length(indices())==3){
            if (all(c(1,2,3) ==indices())==T){
                grid.arrange(gall(), gmanufacturer(),gpublic(),ncol=3)
            }
            
            else if (all(c(1,2,4)==indices())==T){
                grid.arrange(gall(),gmanufacturer(),ghypertension(),ncol=3)
            }
            
            else if (all(c(1,3,4)==indices())==T){
                grid.arrange(gall(),gpublic(),ghypertension(),ncol=3)
            }
            
            else if (all(c(2,3,4) ==indices())==T){
                grid.arrange(gmanufacturer(),gpublic(), ghypertension(),ncol=3)
            }
        }
        
        else if(length(indices())==4){
            
                grid.arrange(gall(), gmanufacturer(),gpublic(),ghypertension(),ncol=2)
            }
    })
            
})




comments powered by Disqus