Shiny is an R package that makes it easy to build interactive web apps straight from R. It is easy to host standalone applications on a webpage or embed them in R Markdown documents, or build dashboards. Shiny apps can also be extended with CSS themes, htmlwidgets, and JavaScript actions.
I have provided you with data about mortality from all 50 states and the District of Columbia. Please access it here You are invited to gather more data from our provider, the CDC WONDER system, from here. This assignment must be done in R. It must be done using the shiny
package. It is recommended you use an R package that supports interactive graphing such as plotly
, or vegalite
, but this is not required.
In this module we’ll be moving onto interactive graphing. Shiny is a data driven web app development framework based in R that lets you not only define visualizations, but define user interactions that allow users to explore data. Shiny is technically a web app development framework, but abstracts away a lot of the underlying technology. If you’re good with R, the learning curve is pretty shallow.
library(shiny)
library(dplyr)
library(tidyr)
library(ggplot2)
library(tibble)
github <- "https://raw.githubusercontent.com/jzuniga123"
file <- "/SPS/master/DATA%20608/cleaned-cdc-mortality-1999-2010-2.csv"
cdc <- read.csv(paste0(github, file), header= TRUE, stringsAsFactors=TRUE)
cdc$Population <- as.numeric(cdc$Population) # Prevent overflow errors
And lets preview this data:
as_tibble(head(cdc))
## # A tibble: 6 x 6
## ICD.Chapter State Year Deaths Population Crude.Rate
## * <fct> <fct> <int> <int> <dbl> <dbl>
## 1 Certain infectious and parasit~ AL 1999 1092 4430141. 24.6
## 2 Certain infectious and parasit~ AL 2000 1188 4447100. 26.7
## 3 Certain infectious and parasit~ AL 2001 1211 4467634. 27.1
## 4 Certain infectious and parasit~ AL 2002 1215 4480089. 27.1
## 5 Certain infectious and parasit~ AL 2003 1350 4503491. 30.0
## 6 Certain infectious and parasit~ AL 2004 1251 4530729. 27.6
According to the Data Dictionary, Crude Rates are expressed as the number of deaths reported each calendar year per 100,000 population by default, hence the death rate per 100,000 persons: \(\textrm{Crude Rate}=10^5(\textrm{Count}/ \textrm{Population})\).
round((cor(sapply(cdc, as.integer), use = "complete.obs")), 3)
## ICD.Chapter State Year Deaths Population Crude.Rate
## ICD.Chapter 1.000 0.000 0.009 0.048 0.024 0.086
## State 0.000 1.000 0.000 -0.026 -0.077 0.010
## Year 0.009 0.000 1.000 0.001 0.027 -0.016
## Deaths 0.048 -0.026 0.001 1.000 0.386 0.626
## Population 0.024 -0.077 0.027 0.386 1.000 -0.032
## Crude.Rate 0.086 0.010 -0.016 0.626 -0.032 1.000
As a researcher, you frequently compare mortality rates from particular causes across different States. You need a visualization that will let you see (for 2010 only) the crude mortality rate, across all States, from one cause (for example, Neoplasms, which are effectively cancers). Create a visualization that allows you to rank States by crude mortality for each cause of death.
cdcQ1 <- cdc %>%
filter(Year == 2010) %>%
group_by(State, ICD.Chapter) %>%
mutate(Count = sum(Deaths), Crude.Rate = 10^5 * (Count / Population))
head(cdcQ1)
## # A tibble: 6 x 7
## # Groups: State, ICD.Chapter [6]
## ICD.Chapter State Year Deaths Population Crude.Rate Count
## <fct> <fct> <int> <int> <dbl> <dbl> <int>
## 1 Certain infectious and p~ AL 2010 1358 4779736. 28.4 1358
## 2 Certain infectious and p~ AK 2010 88 710231. 12.4 88
## 3 Certain infectious and p~ AZ 2010 1249 6392017. 19.5 1249
## 4 Certain infectious and p~ AR 2010 731 2915918. 25.1 731
## 5 Certain infectious and p~ CA 2010 5090 37253956. 13.7 5090
## 6 Certain infectious and p~ CO 2010 722 5029196. 14.4 722
In a Shiny object, the sidebarPanel()
function is placed inside ui <- fluidPage(...)
along with plotOutput("PlotName")
. The renderPlot()
function is assigned to output$PlotName
inside server <- function(input, output) {...}
. They are both brought together with a call to shinyApp(ui = ui, server = server)
at the end of the file.
sidebarPanel(
selectInput("Q1.1", "Cause of Death:",
width = "auto",
choices=cdcQ1$ICD.Chapter, 1
),
helpText("States by crude mortality for each cause of death."),
width = "auto"
)
renderPlot({
ggplot(data=cdcQ1[cdcQ1$ICD.Chapter == input$Q1.1,]
, aes(x=reorder(State, Crude.Rate), y=Crude.Rate)) +
labs(x="State", y="Crude Death Rate per 100,000 Persons") +
geom_bar(stat="identity", fill="steelblue") +
coord_flip()
})
Below is a static image of what the Shiny Object would look like. RPubs.com does not support Shiny applications.
Often you are asked whether particular States are improving their mortality rates (per cause) faster than, or slower than, the national average. Create a visualization that lets your clients see this for themselves for one cause of death at the time. Keep in mind that the national average should be weighted by the national population.
cdcQ2 <- cdc %>%
group_by(Year, ICD.Chapter) %>%
mutate(N.Population = sum(Population),
N.Count = sum(Deaths),
N.Crude.Rate = 10^5*(N.Count/N.Population)) %>%
group_by(Year, ICD.Chapter, State) %>%
mutate(S.Count=sum(Deaths),
S.Crude.Rate=10^5*(S.Count/Population)) %>%
select(ICD.Chapter, State, Year, N.Crude.Rate, S.Crude.Rate)
head(cdcQ2)
## # A tibble: 6 x 5
## # Groups: Year, ICD.Chapter, State [6]
## ICD.Chapter State Year N.Crude.Rate S.Crude.Rate
## <fct> <fct> <int> <dbl> <dbl>
## 1 Certain infectious and parasitic ~ AL 1999 21.5 24.6
## 2 Certain infectious and parasitic ~ AL 2000 21.0 26.7
## 3 Certain infectious and parasitic ~ AL 2001 21.2 27.1
## 4 Certain infectious and parasitic ~ AL 2002 22.2 27.1
## 5 Certain infectious and parasitic ~ AL 2003 22.3 30.0
## 6 Certain infectious and parasitic ~ AL 2004 22.1 27.6
In a Shiny object, the sidebarPanel()
function is placed inside ui <- fluidPage(...)
along with plotOutput("PlotName")
. The renderPlot()
function is assigned to output$PlotName
inside server <- function(input, output) {...}
. They are both brought together with a call to shinyApp(ui = ui, server = server)
at the end of the file.
sidebarPanel(
selectInput("Q2.1", "State:",
width = "auto",
choices=cdcQ2$State, 1
),
selectInput("Q2.2", "Cause of Death:",
width = "auto",
choices=cdcQ2$ICD.Chapter, 1
),
checkboxInput(inputId = "nat", label = strong("Overlay National Average"), value = FALSE),
helpText("Crude mortality by State for each cause of death versus National Average."),
width = "auto"
)
renderPlot({
ggplot(data=cdcQ2[cdcQ2$State == input$Q2.1 & cdcQ2$ICD.Chapter == input$Q2.2,]
, aes(x=Year, y=S.Crude.Rate)) +
labs(x="Year", y="Crude Death Rate per 100,000 Person") +
geom_bar(stat="identity", fill="steelblue") +
if(input$nat){
geom_line(aes(x=Year, y=N.Crude.Rate), col="red", lwd=1)
}
else {
NULL
}
})
Below is a static image of what the Shiny Object would look like. RPubs.com does not support Shiny applications.