Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #suicides <- readRDS("D:/Dokumenty/Studia/5sem/Awd/Projekt/Suicides/healthexp.Rds")
- #suicides$Region <- as.factor(suicides$Region)
- suicides <- read.csv2("D:/Dokumenty/Studia/5sem/Awd/Projekt/Suicides/master3.csv", sep=";")
- #suicides[c(2,5,6,11)] <- lapply(suicides[c(2,5,6,11)], as.numeric)
- suicides[c(5:11)] <- lapply(suicides[c(5:11)], as.numeric)
- colnames(suicides)[1] <- "country"
- colnames(suicides)[11] <- "gdp_per_cap"
- library(shiny)
- library(googleCharts)
- library(dplyr)
- library(data.table)
- {
- xlim <- list(
- min = min(suicides$population) - 500,
- max = max(suicides$population) + 500
- )
- ylim <- list(
- min = min(suicides$suicides_no),
- max = 15000
- #max = max(suicides$suicides_no) + 10
- )
- }
- {
- server <- function(input, output, session) {
- defaultColors <- c("#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477")
- series <- structure(
- lapply(defaultColors, function(color) { list(color=color) }),
- #names = levels(suicides$Region)
- #names = levels(suicides$age)
- names = levels(suicides$age)
- )
- yearData <- reactive({
- #temp values:
- fyear <- input$year
- fminsui <- input$sui[1]
- fmaxsui <- input$sui[2]
- fminpop <- input$pop[1]
- fmaxpop <- input$pop[2]
- # Apply filters
- tmp <- suicides %>%
- filter(
- year == fyear,
- suicides_no >= fminsui,
- suicides_no <= fmaxsui,
- population >= fminpop,
- population <= fmaxpop,
- ) %>%
- select(country, population, suicides_no,
- age, gdp_per_cap, sex) %>%
- arrange(age)
- # Optional: filter by gender
- tmpgender <- if (input$gender != "All") {
- if(input$gender != "Male") { "male" } else { "female" }
- }
- <- if(input$gender != "Male") { "male" } else { "female" }
- fgender <- paste0("%", tmpgender, "%")
- tmp <- tmp %>% filter(sex %like% fgender)
- tmp <- as.data.frame(tmp)
- tmp
- })
- # yearData <- reactive({
- # df <- suicides %>%
- # filter(year == input$year) %>%
- # #select(Country, Health.Expenditure, Life.Expectancy,
- # # Region, Population) %>%
- # #arrange(Region)
- # select(country, population, suicides_no,
- # age, gdp_per_cap) %>%
- # arrange(age)
- # })
- output$chart <- reactive({
- # Return the data and options
- list(
- data = googleDataTable(yearData()),
- options = list(
- title = sprintf("Suicides, %s", input$year),
- series = series,
- hAxis = list(
- title = "Population",
- viewWindow = list(0.9*input$pop[1], 1.1*input$pop[2])
- ),
- vAxis = list(
- title = "Suicides number",
- viewWindow = list(0.9*input$sui[1], 1.1*input$sui[2])
- )
- )
- )
- })
- }
- }
- {
- ui <- fluidPage(
- googleChartsInit(),
- tags$link(
- href=paste0("http://fonts.googleapis.com/css?", "family=Source+Sans+Pro:300,600,300italic"),
- rel="stylesheet", type="text/css"
- ),
- tags$style(type="text/css", "body {font-family: 'Source Sans Pro'}"),
- titlePanel("title panel"),
- sidebarLayout(
- sidebarPanel(
- fluidRow(
- shiny::column(12,
- wellPanel(
- sliderInput("year", "Year",
- min = min(suicides$year), max = max(suicides$year),
- value = min(suicides$year), step=1, animate = TRUE)
- )
- ),
- ),
- fluidRow(
- shiny::column(12,
- wellPanel(
- sliderInput("pop", "X axis range",
- 0, max(suicides$population), c(0, 40000000), step = 100000),
- sliderInput("sui", "Y axis range",
- min(suicides$suicides_no), max(suicides$suicides_no), c(0, 15000), step = 10),
- )
- )
- ),
- fluidRow(
- shiny::column(12,
- wellPanel(
- selectInput("gender", "Gender:",
- c("All", "Male", "Female")
- )
- )
- )
- )
- ),
- mainPanel(
- googleBubbleChart("chart",
- width="100%", height = "1000px",
- options = list(
- fontName = "Source Sans Pro",
- fontSize = 13,
- # Set axis labels and ranges
- hAxis = list(
- title = "Population",
- viewWindow = xlim
- ),
- vAxis = list(
- title = "Suicides number",
- viewWindow = ylim
- ),
- # The default padding is a little too spaced out
- chartArea = list(
- top = 50, left = 75,
- height = "75%", width = "75%"
- ),
- #sizeAxis.maxSize = 20,
- #sizeAxis.minSize = 5,
- sortBubblesBySize = TRUE,
- # Allow pan/zoom
- explorer = list(),
- # Set bubble visual props
- bubble = list(
- opacity = 0.7, stroke = "none",
- # Hide bubble label
- #textStyle = list(color = "none")
- textStyle = list(fontSize = 10)
- ),
- # Set fonts
- titleTextStyle = list(fontSize = 16),
- tooltip = list(textStyle = list(fontSize = 12))
- )
- )
- )
- )
- #h2("Google Charts demo"),
- )
- }
- shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement