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.


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

titlePanel("Daily Alcohol Consumption And Grades/Gender"),

helpText("Exploring student alcohol Consumption"),

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

label = "Binwidth",
min = 0, max = 10, value = 5),
label="Choose a Gender",
label="Higher Education",
choices = c("Yes","No"),
radioButtons("health", label = h3("Health Level"),
choices = list("1" = 1, "2" = 2,
"3" = 3,"4"=4,"5"=5),selected = 1),
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)





  • 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

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)
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,
title <- switch(input$gender,
'Male'=paste(title,"for Male Students"),
"Female"=paste(title,"for female students"))
data <- switch(input$higher,
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,
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,
title <- switch(input$nursery,
"1"=paste(title,"who attended Nursery"),
"2"=paste(title,"who did not attend Nursery"))

data <- switch(input$romantic,
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,

The link to the app is here.


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s