Th3NiKo

Projekt R Shiny

Jun 12th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.79 KB | None | 0 0
  1. library(shinydashboard)
  2. library(ggplot2)
  3. library(dplyr)
  4. library("readxl")
  5.  
  6. main_data <- read_excel("Young.xlsx")
  7. names(main_data) = gsub(" ","",names(main_data))
  8. names(main_data) = gsub(",","",names(main_data))
  9. names(main_data) = gsub("-","",names(main_data))
  10.  
  11. musicNames = colnames(main_data)[0:140]
  12. analyzeNames = list("Onlychild", "Education","Houseblockofflats","Villagetown","Leftrighthanded","Height", "Weight", "Age", "Numberofsiblings",
  13.                     "Gender")
  14.  
  15.  
  16. ui <- dashboardPage(
  17.     skin = "blue",
  18.     dashboardHeader(title = "Young People"),
  19.     dashboardSidebar(
  20.         sidebarMenu(
  21.             menuItem("Description", tabName = "main", icon = icon("home")),
  22.             menuItem("Charts", tabName = "music", icon = icon("chart-line"))
  23.         )
  24.     ),
  25.     dashboardBody(
  26.         tabItems(
  27.             ########################################################
  28.             #  MUSIC
  29.             tabItem(tabName = "music",
  30.                 fluidRow(
  31.                     box(
  32.                         sliderInput("sliderAge", "Age:", min=15, max=30, value=c(15,30)),
  33.                         sliderInput("sliderWeight", "Weight:", min=41, max=165, value=c(41,165)),
  34.                         htmlOutput("Number"),
  35.                         htmlOutput("Average"),
  36.                         htmlOutput("Median")
  37.                     ),
  38.                     box(
  39.                         selectInput("type", h3("Category:"),
  40.                             choices = musicNames, selected = 1),
  41.                         selectInput("analyze", h3("Analyze:"),
  42.                                     choices = analyzeNames, selected = "Age"),
  43.                         selectInput("gender", h3("Gender:"),
  44.                                     choices = list("Both", "Male", "Female"), selected = "Both")
  45.                     )
  46.                    
  47.                 ),
  48.                 fluidRow(
  49.                     box(plotOutput("plot1", height = 300)),
  50.                     box(plotOutput("plot2", height = 300))
  51.                 ),
  52.                 fluidRow(
  53.                     box(plotOutput("plot3", height = 300)),
  54.                     box(plotOutput("plot4", height = 300))
  55.                 )
  56.             ),
  57.            
  58.             tabItem(tabName = "main",
  59.                 fluidRow(
  60.                     h1(strong("Young People Survey")),br(),
  61.                     imageOutput("youngImage",height=150),
  62.                     em("Link to dataset: "),a("https://www.kaggle.com/miroslavsabo/young-people-survey"),br(),
  63.                     p("In 2013, students of the Statistics class at FSEV UK were asked to invite their friends to participate in this survey.
  64.                      All participants were of Slovakian nationality, aged between 15-30. Participants could give note from 1 to 5."),
  65.                     h3("Categories of questions"),
  66.                     tags$ul(
  67.                         tags$li("Music preferences"),
  68.                         tags$li("Movie preferences"),
  69.                         tags$li("Hobbies & interests"),
  70.                         tags$li("Phobias"),
  71.                         tags$li("Health habits"),
  72.                         tags$li("Personality traits, views on life, & opinions"),
  73.                         tags$li("Spending habits"),
  74.                         tags$li("Demographics")
  75.                     )
  76.  
  77.                 )
  78.                    
  79.             )
  80.            
  81.         )
  82.     )
  83. )
  84.  
  85.  
  86. #FILTER BY AGE AND GENDER
  87. filterData <- function(func_data,input) {
  88.     data <- func_data %>%
  89.         filter(Age >= input$sliderAge[1] & Age <= input$sliderAge[2]) %>%
  90.         filter(Weight >= input$sliderWeight[1] & Weight <= input$sliderWeight[2])
  91.     if(input$gender != "Both"){
  92.         data = data %>% filter(Gender == tolower(input$gender))
  93.     }
  94.     return (data)
  95. }
  96.  
  97. my_theme = theme(
  98.     plot.title = element_text(color="grey21", size=20, face="bold",hjust=0.5, vjust=0.5),
  99.     axis.title.x = element_text(color="grey21", size=18, face="bold"),
  100.     axis.title.y = element_text(color="grey21", size=18, face="bold")
  101.    
  102. )
  103.  
  104. server <- function(input, output) {
  105.     ##IMG YOUNG PEOPLE
  106.     output$youngImage <- renderImage({
  107.         list(
  108.             src = "images/People.png",
  109.             contentType = "image/png",
  110.             height=150,
  111.             alt = "Young"
  112.         )
  113.     }, deleteFile = FALSE)
  114.     ###########################################################
  115.     #Music plot nr.1
  116.     output$plot1 <- renderPlot({
  117.         data <- filterData(main_data,input)
  118.        
  119.         geomAssing = "line"
  120.         sizeAssing = 1.3
  121.        
  122.         if(nrow(unique(data[,input$analyze])) == 1){
  123.             geomAssing = "point"
  124.             sizeAssing = 3
  125.         }
  126.         ggplot(data) + ggtitle("Average rating") + my_theme +
  127.             stat_summary_bin(
  128.                 mapping = aes_string(x = input$analyze, y = input$type,group=1),
  129.                 fun.ymin = min,
  130.                 fun.ymax = max,
  131.                 fun.y = mean,
  132.                 size = sizeAssing,
  133.                 geom=geomAssing
  134.  
  135.             ) + ylim(1.0, 5)
  136.              
  137.        
  138.     })
  139.     ###########################################################
  140.     #Music plot nr.2
  141.     output$plot2 <- renderPlot({
  142.         data <- filterData(main_data,input)
  143.         ggplot(data) +
  144.         geom_jitter(aes_string(x = input$analyze, y = input$type)) + my_theme +
  145.         ggtitle("Distribution of votes")
  146.        
  147.     })
  148.     ###########################################################
  149.     #Music plot nr.3
  150.     output$plot3 <- renderPlot({
  151.         data <- filterData(main_data,input)
  152.         ggplot(data) +
  153.             geom_histogram(aes_string(x = input$type)) + my_theme +
  154.             ggtitle("Count of votes for category")
  155.        
  156.     })
  157.     ###########################################################
  158.     #Music plot nr.4
  159.     output$plot4 <- renderPlot({
  160.         data <- filterData(main_data,input)
  161.         ggplot(data) +
  162.             geom_histogram(stat="count",aes_string(x = input$analyze),size=1) + my_theme +
  163.             ggtitle("Count of votes for analyze")
  164.        
  165.     })
  166.     ###########################################################
  167.     #Texts
  168.     output$Number <- renderUI({
  169.         data <- filterData(main_data,input)
  170.         h3(strong("Number of people: "), nrow(data))
  171.     })
  172.     output$Average <- renderUI({
  173.         data <- filterData(main_data,input)
  174.         meanValue = mean(na.exclude(data[[input$type]]))
  175.        
  176.         h3(strong("Average vote: "), format(round(meanValue, 3), nsmall = 3))
  177.     })
  178.     output$Median <- renderUI({
  179.         data <- filterData(main_data,input)
  180.         medianValue = median(na.exclude(data[[input$type]]))
  181.        
  182.         h3(strong("Median of votes: "), format(round(medianValue, 1), nsmall = 1))
  183.     })
  184.    
  185. }
  186.  
  187. shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment