Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.29 KB | None | 0 0
  1. #suicides <- readRDS("D:/Dokumenty/Studia/5sem/Awd/Projekt/Suicides/healthexp.Rds")
  2. #suicides$Region <- as.factor(suicides$Region)
  3.  
  4. suicides <- read.csv2("D:/Dokumenty/Studia/5sem/Awd/Projekt/Suicides/master3.csv", sep=";")
  5. #suicides[c(2,5,6,11)] <- lapply(suicides[c(2,5,6,11)], as.numeric)
  6. suicides[c(5:11)] <- lapply(suicides[c(5:11)], as.numeric)
  7. colnames(suicides)[1] <- "country"
  8. colnames(suicides)[11] <- "gdp_per_cap"
  9.  
  10. library(shiny)
  11. library(googleCharts)
  12. library(dplyr)
  13. library(data.table)
  14.  
  15. {
  16.   xlim <- list(
  17.     min = min(suicides$population) - 500,
  18.     max = max(suicides$population) + 500
  19.   )
  20.   ylim <- list(
  21.     min = min(suicides$suicides_no),
  22.     max = 15000
  23.     #max = max(suicides$suicides_no) + 10
  24.   )
  25. }
  26.  
  27.  
  28. {
  29.   server <- function(input, output, session) {
  30.    
  31.     defaultColors <- c("#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477")
  32.     series <- structure(
  33.       lapply(defaultColors, function(color) { list(color=color) }),
  34.       #names = levels(suicides$Region)
  35.       #names = levels(suicides$age)
  36.       names = levels(suicides$age)
  37.     )
  38.    
  39.     yearData <- reactive({
  40.       #temp values:
  41.       fyear <- input$year
  42.       fminsui <- input$sui[1]
  43.       fmaxsui <- input$sui[2]
  44.       fminpop <- input$pop[1]
  45.       fmaxpop <- input$pop[2]
  46.      
  47.       # Apply filters
  48.       tmp <- suicides %>%
  49.         filter(
  50.           year == fyear,
  51.           suicides_no >= fminsui,
  52.           suicides_no <= fmaxsui,
  53.           population >= fminpop,
  54.           population <= fmaxpop,
  55.         ) %>%
  56.         select(country, population, suicides_no,
  57.                age, gdp_per_cap, sex) %>%
  58.         arrange(age)
  59.      
  60.       # Optional: filter by gender
  61.       tmpgender <- if (input$gender != "All") {
  62.         if(input$gender != "Male") { "male" } else { "female" }
  63.       }
  64.        <- if(input$gender != "Male") { "male" } else { "female" }
  65.       fgender <- paste0("%", tmpgender, "%")
  66.       tmp <- tmp %>% filter(sex %like% fgender)
  67.      
  68.       tmp <- as.data.frame(tmp)
  69.       tmp
  70.     })
  71.    
  72.     # yearData <- reactive({
  73.     #   df <- suicides %>%
  74.     #     filter(year == input$year) %>%
  75.     #     #select(Country, Health.Expenditure, Life.Expectancy,
  76.     #     #       Region, Population) %>%
  77.     #     #arrange(Region)
  78.     #     select(country, population, suicides_no,
  79.     #            age, gdp_per_cap) %>%
  80.     #     arrange(age)
  81.     # })
  82.    
  83.     output$chart <- reactive({
  84.       # Return the data and options
  85.       list(
  86.         data = googleDataTable(yearData()),
  87.         options = list(
  88.           title = sprintf("Suicides, %s", input$year),
  89.           series = series,
  90.           hAxis = list(
  91.             title = "Population",
  92.             viewWindow = list(0.9*input$pop[1], 1.1*input$pop[2])
  93.           ),
  94.           vAxis = list(
  95.             title = "Suicides number",
  96.             viewWindow = list(0.9*input$sui[1], 1.1*input$sui[2])
  97.           )
  98.         )
  99.       )
  100.     })
  101.   }
  102. }
  103.  
  104.  
  105.  
  106. {
  107.   ui <- fluidPage(
  108.     googleChartsInit(),
  109.     tags$link(
  110.       href=paste0("http://fonts.googleapis.com/css?", "family=Source+Sans+Pro:300,600,300italic"),
  111.       rel="stylesheet", type="text/css"
  112.     ),
  113.     tags$style(type="text/css", "body {font-family: 'Source Sans Pro'}"),
  114.    
  115.     titlePanel("title panel"),
  116.    
  117.     sidebarLayout(
  118.       sidebarPanel(
  119.         fluidRow(
  120.           shiny::column(12,
  121.                         wellPanel(
  122.                           sliderInput("year", "Year",
  123.                                       min = min(suicides$year), max = max(suicides$year),
  124.                                       value = min(suicides$year), step=1, animate = TRUE)
  125.                         )
  126.           ),
  127.         ),
  128.         fluidRow(
  129.           shiny::column(12,
  130.                         wellPanel(
  131.                           sliderInput("pop", "X axis range",
  132.                                       0, max(suicides$population), c(0, 40000000), step = 100000),
  133.                           sliderInput("sui", "Y axis range",
  134.                                       min(suicides$suicides_no), max(suicides$suicides_no), c(0, 15000), step = 10),
  135.                         )
  136.           )
  137.         ),
  138.         fluidRow(
  139.           shiny::column(12,
  140.                         wellPanel(
  141.                           selectInput("gender", "Gender:",
  142.                                       c("All", "Male", "Female")
  143.                           )
  144.                         )
  145.           )
  146.         )
  147.       ),
  148.       mainPanel(
  149.         googleBubbleChart("chart",
  150.                           width="100%", height = "1000px",
  151.                           options = list(
  152.                             fontName = "Source Sans Pro",
  153.                             fontSize = 13,
  154.                             # Set axis labels and ranges
  155.                             hAxis = list(
  156.                               title = "Population",
  157.                               viewWindow = xlim
  158.                             ),
  159.                             vAxis = list(
  160.                               title = "Suicides number",
  161.                               viewWindow = ylim
  162.                             ),
  163.                             # The default padding is a little too spaced out
  164.                             chartArea = list(
  165.                               top = 50, left = 75,
  166.                               height = "75%", width = "75%"
  167.                             ),
  168.                             #sizeAxis.maxSize = 20,
  169.                             #sizeAxis.minSize = 5,
  170.                             sortBubblesBySize = TRUE,
  171.                             # Allow pan/zoom
  172.                             explorer = list(),
  173.                             # Set bubble visual props
  174.                             bubble = list(
  175.                               opacity = 0.7, stroke = "none",
  176.                               # Hide bubble label
  177.                               #textStyle = list(color = "none")
  178.                               textStyle = list(fontSize = 10)
  179.                             ),
  180.                             # Set fonts
  181.                             titleTextStyle = list(fontSize = 16),
  182.                             tooltip = list(textStyle = list(fontSize = 12))
  183.                           )
  184.         )
  185.       )
  186.     )
  187.     #h2("Google Charts demo"),
  188.   )
  189. }
  190.  
  191. shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement