Building Shiny App for Alcohol Consumption Data Set

Building a shiny app to look at Student Alcohol Consumption

Shiny is an R packages made by Rstudio . This package allows us to build simple , interactive web applications in RStudio.In this post, we will be using the Alcohol Consumption Data set that can be found on Kaggle.

ui.R

This part of the code takes care of user interface of the application

  • The titlePanel deals with the header at the top left hand corner of the app.
  • The application is made of UI widgets like radio buttons and selection bars
# ui function for daily student alcohol consumption

# ui function for daily student alcohol consumption

library(shiny)
shinyUI(fluidPage(
titlePanel("Daily Alcohol Consumption And Grades/Gender"),

sidebarLayout(
sidebarPanel(
helpText("Exploring student alcohol Consumption"),

selectInput("var",
label = "Choose a Grade to display G1,G2,G3",
choices = c("G1", "G2",
"G3"),
selected = "G3"),

sliderInput("bins",
label = "Binwidth",
min = 0, max = 10, value = 5),
selectInput("gender",
label="Choose a Gender",
choices=c("Male","Female"),
selected="Female"),
selectInput("higher",
label="Higher Education",
choices = c("Yes","No"),
selected="Yes"),
radioButtons("health", label = h3("Health Level"),
choices = list("1" = 1, "2" = 2,
"3" = 3,"4"=4,"5"=5),selected = 1),
selectInput("nursery",
label = h3("Nursery"),
choices = list("Yes" = 1,
"No" = 2),
selected = 1),
radioButtons("romantic", label = h3("Romantic Relationship"),
choices = list("Yes" = 1, "No" = 2
),selected = 1)

),

mainPanel(plotOutput("plot"))

)
))

server.R

  • This code represents the functional part of the application. This the is part where all the data processing happens
# server function for daily student alchohol consumption

# server function for daily student alchohol consumption

library(ggplot2)
alc <- read.csv("student-por.csv",stringsAsFactors = F,header=T)
plot_function <- function(var,color,wbins,title_text)
{
names(var)<- c("Sex","Nursery","Higher","romantic","Dalc","health","grade")
var <- var[,c(-1,-2,-3,-4,-6)]
var$Dalc <- as.factor(var$Dalc)
g <- ggplot(var,aes(x=grade))+geom_histogram(binwidth = wbins,fill=color)+facet_grid(Dalc~.)+ggtitle(title_text)+theme(
plot.title = element_text(hjust=0.4)
)
g
}
shinyServer(
function(input, output) {
output$plot <- renderPlot({
data <- switch(input$var,
"G1" = alc[,c(2,20,21,23,27,29,31)],
"G2" = alc[,c(2,20,21,23,27,29,32)],
"G3" = alc[,c(2,20,21,23,27,29,33)]
)

color <- switch(input$var,
"G1" = "darkgreen",
"G2" = "black",
"G3" = "darkorange"
)
title <- switch(input$var,
"G1"="Daily Alcohol Consumption and Grade 1",
"G2"="Daily Alcohol Consumption and Grade 2",
"G3"="Daily Alcohol Consumption and Grade 3")
data <- switch(input$gender,
"Male"=subset(data,sex=="M"),
"Female"=subset(data,sex=="F"))
title <- switch(input$gender,
'Male'=paste(title,"for Male Students"),
"Female"=paste(title,"for female students"))
data <- switch(input$higher,
"Yes"=subset(data,higher=="yes"),
"No"=subset(data,higher=="no"))
title <- switch(input$higher,
"Yes"=paste(title,"who intend to study further"
),
"No"=paste(title,"who do not intend to study further"))
data <- switch(input$health,
"1"=subset(data,health==1),
"2"=subset(data,health==2),
"3"=subset(data,health==3),
"4"=subset(data,health==4),
"5"=subset(data,health==5))
title <- switch(input$health,
"1"=paste(title,"with health level 1"),
"2"=paste(title,"with health level 2"),
"3"=paste(title,"with health level 3"),
"4"=paste(title,"with health level 4"),
"5"=paste(title,"with health level 5"))
data <- switch(input$nursery,
"1"=subset(data,nursery=="yes"),
"2"=subset(data,nursery=="no"))
title <- switch(input$nursery,
"1"=paste(title,"who attended Nursery"),
"2"=paste(title,"who did not attend Nursery"))

data <- switch(input$romantic,
"1"=subset(data,romantic=="yes"),
"2"=subset(data,romantic=="no"))
title <- switch(input$romantic,
"1"=paste(title,"(In a romantic relationship)"),
"2"=paste(title,"(Not in a romantic relationship)"))


plot_function(var = data,
color = color,
wbins = input$bins,
title_text=title)
})
}
)

The link to the app is here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s