#install necessary libraries
library(shiny)
library(ggplot2)
library(dplyr)
Link to the deployed shiny app: https://olia.shinyapps.io/shiny_app/
Code of app.R is shown below:
#load the data
data <-read.csv('cleaned-cdc-mortality-1999-2010-2.csv')
#select the data from the Year 2010 (for Question 1)
dataset_2010 <- subset(data,data$Year==2010)
#find out death causes for the Year 2010 (for Question 1)
death_causes_2010 <- unique(dataset_2010$ICD.Chapter)
#list of states
states <- unique(data$State)
#death causes
death_causes <- unique(data$ICD.Chapter)
#years
years <- unique(data$Year)
#calculate population by year
population_by_year <- data %>% select(Year,Population) %>% group_by(Year) %>% summarise(Year,Population)
#Define server logic to plot various variables
server <- function(input, output) {
#dataset for Question 1
dataset_q1 <- reactive({
#calculate number of deaths by state
subset(dataset_2010,dataset_2010$ICD.Chapter==input$death_cause_2010) %>% select(State,Deaths) %>% group_by(State) %>% summarise(State,Deaths) %>% arrange(desc(Deaths))
})
#graph title for Question 1
formulaText_q1 <- reactive({
paste("Death Cause - ", input$death_cause_2010)
})
#return graph title for printing as a caption
output$caption_q1 <- renderText({
formulaText()
})
#generate a plot for Question 1
output$plot_q1 <- renderPlot({
#graph show number of deaths by state for a selected death cause
ggplot(dataset_q1(), aes(reorder(State, Deaths), Deaths)) +
geom_bar(position="dodge",stat="identity") +
labs(x="State", y = "Number of Deaths") +
coord_flip() +
ggtitle("Mortality Rate by State")
})
#graph title for Question 2
dataset_q2 <- reactive({
#calculate percentage of deaths per year for a selected state and death cause
subset(data,data$ICD.Chapter==input$death_cause & data$State==input$state) %>% select(Deaths,Year) %>% group_by(Year) %>% inner_join(population_by_year) %>% mutate(Deaths=(Deaths/Population)*100) %>% summarise(Deaths,Year)
})
#graph title for Question 2
formulaText_q2 <- reactive({
paste("Death Cause - ", input$death_cause)
})
#return graph title for printing as a caption
output$caption_q2 <- renderText({
formulaText_q2()
})
#generate a plot for Question 2
output$plot_q2 <- renderPlot({
ggplot(dataset_q2(), aes(Year, Deaths)) +
geom_bar(position="dodge",stat="identity") +
labs(x="Year", y = "YearMortality Rate, %") +
ggtitle("Mortality Rate by Death Cause by State")
})
}
#define UI
ui <- fluidPage(
navbarPage("",
tabPanel(
"Question 1",
titlePanel("Mortality Rate From Partucular Causes Across Different States"),
#sidebar layout with input and output definitions for Question 1
sidebarLayout(
#sidebar panel for inputs
sidebarPanel(
#input: selector for variable death cause in 2010
selectInput("death_causes_2010_variable",
"Death Cause",death_causes_2010)
),
#main panel for displaying outputs
mainPanel(
#output: formatted text for caption
h3(textOutput("caption")),
#output: plot graph for a selected cause of death
plotOutput("plot_q1")
)
)),
tabPanel(
"Question 2",
titlePanel("Mortality Rate From Partucular Causes by States per Year"),
#sidebar layout with input and output definitions for Question 2
sidebarLayout(
#sidebar panel for inputs
sidebarPanel(
#input: selector for variables state and death cause
selectInput("death_cause", "Select Death Cause",
death_causes),
selectInput("state", "Select State",
states)
),
#main panel for displaying outputs
mainPanel(
#output: formatted text for caption
h3(textOutput("caption_q2")),
#output: plot graph for a selected state and cause of death
plotOutput("plot_q2")
)
))
))
#run shiny app
shinyApp(ui, server)