Tomás A. Maccor
15/4/2020
This Shiny app enables the user to analyze fate of the RMS Titanic’s passengers & crew. via exploratory plotting of the passenger survival data.
The data used by the App comes from one of R’s built in datasets –Titanic. But it can very easily be modified to accept other dataset & plot them.
It downloads the dataset & converts it to a dataframe so that it can be later passed on to the FILTER command from the DPLYR package
library(shiny)
library(shinythemes)
library(dplyr, quietly = TRUE , warn.conflicts = FALSE)
library(ggplot2, quietly = TRUE)
library(readr, quietly = TRUE)
data("Titanic")
Titanic <- as.data.frame(Titanic)The dataset has 5 variables and 32 observations:
| Class | Sex | Age | Survived | Freq |
|---|---|---|---|---|
| 1st | Male | Child | No | 0 |
| 2nd | Male | Child | No | 0 |
| 3rd | Male | Child | No | 35 |
| Crew | Male | Child | No | 0 |
| 1st | Female | Child | No | 0 |
| 2nd | Female | Child | No | 0 |
The UI code accepts User Input via 2 “selectInput” Shiny commands:
* PassengerClass
* Sex
selectInput(inputId = "PassengerClass", label = "Enter passenger category",
choices = c(levels(Titanic$Class), "All") ),
selectInput("Sex", "Choose sex", choices = c(levels(Titanic$Sex), "Both") ),
submitButton(text = "Show Plot")The Shiny SELECT boxes were improved in their functionality, so that they can also accept an input of ALL categories of the PASSENGER CLASS & SEX variables from the dataset.
The algorithm in the server.R file used to subset the Titanic dataset (based on the user selection) is provided below:
ifelse (input$PassengerClass =="All", Titanic_subset <- Titanic,
Titanic_subset <- Titanic %>% filter(Class == input$PassengerClass)
)
if (input$Sex == "Male") {Titanic_subset <- Titanic_subset %>% filter(Sex == "Male")}
if (input$Sex == "Female") {Titanic_subset <- Titanic_subset %>% filter(Sex == "Female")}
if (input$Sex == "Both") {Titanic_subset <- Titanic_subset}
ploteo <- ggplot(Titanic_subset, aes(x = Class, y = Freq, fill = Survived ) ) +
geom_bar(stat = "identity", position = position_dodge() ) +
ylab("Number of passengers")
ploteoBy making exploratory plots, we can quickly obtain insights from this dataset.
For example, the overall survival for 2nd Class passenger tells us that 1/3 of the passengers survived, but if we differentiate by sex we will see that the % of females that survived was roughly inversely proportional to the male survivors. Thus, we can confirm that the “women first” call was indeed observed for the 2nd Class passengers.
ALL 2nd Class passengers
User input PASSENGERCLASS = “All”
User input SEX = “Both”
## [[1]]
## [1] 2nd 2nd 2nd 2nd 2nd 2nd 2nd 2nd
## Levels: 1st 2nd 3rd Crew
2nd Class FEMALE passengers
## [[1]]
## [1] 2nd 2nd 2nd 2nd 2nd 2nd 2nd 2nd
## Levels: 1st 2nd 3rd Crew
2nd Class MALE passengers
## [[1]]
## [1] 2nd 2nd 2nd 2nd 2nd 2nd 2nd 2nd
## Levels: 1st 2nd 3rd Crew