Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(shinydashboard)
- library(ggplot2)
- library(dplyr)
- library("readxl")
- main_data <- read_excel("Young.xlsx")
- names(main_data) = gsub(" ","",names(main_data))
- names(main_data) = gsub(",","",names(main_data))
- names(main_data) = gsub("-","",names(main_data))
- musicNames = colnames(main_data)[0:140]
- analyzeNames = list("Onlychild", "Education","Houseblockofflats","Villagetown","Leftrighthanded","Height", "Weight", "Age", "Numberofsiblings",
- "Gender")
- ui <- dashboardPage(
- skin = "blue",
- dashboardHeader(title = "Young People"),
- dashboardSidebar(
- sidebarMenu(
- menuItem("Description", tabName = "main", icon = icon("home")),
- menuItem("Charts", tabName = "music", icon = icon("chart-line"))
- )
- ),
- dashboardBody(
- tabItems(
- ########################################################
- # MUSIC
- tabItem(tabName = "music",
- fluidRow(
- box(
- sliderInput("sliderAge", "Age:", min=15, max=30, value=c(15,30)),
- sliderInput("sliderWeight", "Weight:", min=41, max=165, value=c(41,165)),
- htmlOutput("Number"),
- htmlOutput("Average"),
- htmlOutput("Median")
- ),
- box(
- selectInput("type", h3("Category:"),
- choices = musicNames, selected = 1),
- selectInput("analyze", h3("Analyze:"),
- choices = analyzeNames, selected = "Age"),
- selectInput("gender", h3("Gender:"),
- choices = list("Both", "Male", "Female"), selected = "Both")
- )
- ),
- fluidRow(
- box(plotOutput("plot1", height = 300)),
- box(plotOutput("plot2", height = 300))
- ),
- fluidRow(
- box(plotOutput("plot3", height = 300)),
- box(plotOutput("plot4", height = 300))
- )
- ),
- tabItem(tabName = "main",
- fluidRow(
- h1(strong("Young People Survey")),br(),
- imageOutput("youngImage",height=150),
- em("Link to dataset: "),a("https://www.kaggle.com/miroslavsabo/young-people-survey"),br(),
- p("In 2013, students of the Statistics class at FSEV UK were asked to invite their friends to participate in this survey.
- All participants were of Slovakian nationality, aged between 15-30. Participants could give note from 1 to 5."),
- h3("Categories of questions"),
- tags$ul(
- tags$li("Music preferences"),
- tags$li("Movie preferences"),
- tags$li("Hobbies & interests"),
- tags$li("Phobias"),
- tags$li("Health habits"),
- tags$li("Personality traits, views on life, & opinions"),
- tags$li("Spending habits"),
- tags$li("Demographics")
- )
- )
- )
- )
- )
- )
- #FILTER BY AGE AND GENDER
- filterData <- function(func_data,input) {
- data <- func_data %>%
- filter(Age >= input$sliderAge[1] & Age <= input$sliderAge[2]) %>%
- filter(Weight >= input$sliderWeight[1] & Weight <= input$sliderWeight[2])
- if(input$gender != "Both"){
- data = data %>% filter(Gender == tolower(input$gender))
- }
- return (data)
- }
- my_theme = theme(
- plot.title = element_text(color="grey21", size=20, face="bold",hjust=0.5, vjust=0.5),
- axis.title.x = element_text(color="grey21", size=18, face="bold"),
- axis.title.y = element_text(color="grey21", size=18, face="bold")
- )
- server <- function(input, output) {
- ##IMG YOUNG PEOPLE
- output$youngImage <- renderImage({
- list(
- src = "images/People.png",
- contentType = "image/png",
- height=150,
- alt = "Young"
- )
- }, deleteFile = FALSE)
- ###########################################################
- #Music plot nr.1
- output$plot1 <- renderPlot({
- data <- filterData(main_data,input)
- geomAssing = "line"
- sizeAssing = 1.3
- if(nrow(unique(data[,input$analyze])) == 1){
- geomAssing = "point"
- sizeAssing = 3
- }
- ggplot(data) + ggtitle("Average rating") + my_theme +
- stat_summary_bin(
- mapping = aes_string(x = input$analyze, y = input$type,group=1),
- fun.ymin = min,
- fun.ymax = max,
- fun.y = mean,
- size = sizeAssing,
- geom=geomAssing
- ) + ylim(1.0, 5)
- })
- ###########################################################
- #Music plot nr.2
- output$plot2 <- renderPlot({
- data <- filterData(main_data,input)
- ggplot(data) +
- geom_jitter(aes_string(x = input$analyze, y = input$type)) + my_theme +
- ggtitle("Distribution of votes")
- })
- ###########################################################
- #Music plot nr.3
- output$plot3 <- renderPlot({
- data <- filterData(main_data,input)
- ggplot(data) +
- geom_histogram(aes_string(x = input$type)) + my_theme +
- ggtitle("Count of votes for category")
- })
- ###########################################################
- #Music plot nr.4
- output$plot4 <- renderPlot({
- data <- filterData(main_data,input)
- ggplot(data) +
- geom_histogram(stat="count",aes_string(x = input$analyze),size=1) + my_theme +
- ggtitle("Count of votes for analyze")
- })
- ###########################################################
- #Texts
- output$Number <- renderUI({
- data <- filterData(main_data,input)
- h3(strong("Number of people: "), nrow(data))
- })
- output$Average <- renderUI({
- data <- filterData(main_data,input)
- meanValue = mean(na.exclude(data[[input$type]]))
- h3(strong("Average vote: "), format(round(meanValue, 3), nsmall = 3))
- })
- output$Median <- renderUI({
- data <- filterData(main_data,input)
- medianValue = median(na.exclude(data[[input$type]]))
- h3(strong("Median of votes: "), format(round(medianValue, 1), nsmall = 1))
- })
- }
- shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment