projekt-shiny-iwab/server.R
2020-06-28 15:52:07 +02:00

178 lines
7.4 KiB
R

library(shiny)
library(eurostat)
library(cshapes)
library(tidyverse)
library(dplyr)
library(ggplot2)
dat <- get_eurostat(id = 'sdg_11_40', time_format = 'num') %>%
filter(!geo %in% c('EU28', 'EU27_2020', 'TR')) %>%
mutate(country = label_eurostat(geo, dic = "geo", lang = 'en', custom_dic = c(DE = "Germany")))
geo <- get_eurostat_geospatial(nuts_level = 0, resolution = 20, output_class = "sf")
dat_vehicle <- get_eurostat(id = 'tran_sf_roadve', time_format = 'num') %>%
mutate(country = label_eurostat(geo, dic = "geo", lang = 'en', custom_dic = c(DE = "Germany")))
dat_users <- get_eurostat(id = 'tran_sf_roadus', time_format = 'num') %>%
mutate(country = label_eurostat(geo, dic = "geo", lang = 'en', custom_dic = c(DE = "Germany")))
dat_road <- get_eurostat(id = 'tran_sf_roadro', time_format = 'num') %>%
mutate(country = label_eurostat(geo, dic = "geo", lang = 'en', custom_dic = c(DE = "Germany")))
dat_nuts <- get_eurostat(id = 'tran_r_acci', time_format = 'num')
geo_nuts <- get_eurostat_geospatial(nuts_level = 2)
hist_geo <- cshapes::cshp(as.Date("1989-1-1"), useGW = TRUE)
hist_geo@data$geo_code <- as.character(hist_geo@data$ISO1AL3)
div_de <- hist_geo[hist_geo@data$geo_code %in% c('DDR'), ]
shinyServer(function(input, output) {
output$roadAccidentsPlot <- renderPlot({
dat %>%
filter(time == input$year & unit == ifelse(input$factor, 'RT', 'NR')) %>%
ggplot(aes(x = reorder(country, values), y = values, fill = ifelse(geo == "PL", "Highlighted", "Normal"))) +
geom_bar(stat = "identity") +
theme(legend.position = "none", axis.title.y = element_blank()) +
labs(y = 'Ofiary', x = NULL, caption = "Źródło: Eurostat") +
coord_flip()
}, width = 687, height = 700)
output$mapPlot <- renderPlot({
mapdata <- geo %>%
right_join(filter(dat, time == input$year & unit == ifelse(input$factor, 'RT', 'NR'))) %>%
mutate(cat = cut_to_classes(values, n = 4, decimals = 1))
ggplot(mapdata, aes(fill = cat)) +
scale_fill_brewer(palette = 'Reds') +
geom_sf(color = alpha('black', 1/3), alpha = .6) +
coord_sf(xlim = c(-20,44), ylim = c(30,70)) +
labs(fill = 'Ofiary', caption = "Źródło: Eurostat")
}, width = 800, height = 700)
output$roadAccidentsByVehiclePlot <- renderPlot({
dat_vehicle %>%
filter(time == input$vehYear & vehicle == input$vehicle) %>%
ggplot(aes(x = reorder(country, values), y = values, fill = ifelse(geo == "PL", "Highlighted", "Normal"))) +
geom_bar(stat = "identity") +
theme(legend.position = "none", axis.title.y = element_blank()) +
labs(y = 'Ofiary', x = NULL, caption = "Źródło: Eurostat") +
coord_flip()
}, width = 687, height = 'auto')
output$mapByVehiclePlot <- renderPlot({
mapdata_veh <- geo %>%
right_join(filter(dat_vehicle, time == input$vehYear & vehicle == input$vehicle))
ggplot(mapdata_veh, aes(fill = values)) +
geom_sf(color = alpha('black', 1/3), alpha = .6) +
scale_fill_gradient(low="white", high="red") +
coord_sf(xlim = c(-20,44), ylim = c(30,70)) +
labs(fill = 'Ofairy', caption = "Źródło: Eurostat")
}, width = 800, height = 700)
output$roadAccidentsByUserPlot <- renderPlot({
dat_users %>%
filter(time == input$userYear & pers_inv == input$user) %>%
ggplot(aes(x = reorder(country, values), y = values, fill = ifelse(geo == "PL", "Highlighted", "Normal"))) +
geom_bar(stat = "identity") +
theme(legend.position = "none", axis.title.y = element_blank()) +
labs(y = 'Ofiary', x = NULL, caption = "Źródło: Eurostat") +
coord_flip()
}, width = 687, height = 'auto')
output$mapByUserPlot <- renderPlot({
mapdata_user <- geo %>%
right_join(filter(dat_users, time == input$userYear & pers_inv == input$user))
ggplot(mapdata_user, aes(fill = values)) +
geom_sf(color = alpha('black', 1/3), alpha = .6) +
scale_fill_gradient(low="white", high="red") +
coord_sf(xlim = c(-20,44), ylim = c(30,70)) +
labs(fill = 'Ofairy', caption = "Źródło: Eurostat")
}, width = 800, height = 700)
output$roadAccidentsByRoadTypePlot <- renderPlot({
dat_road %>%
filter(time == input$roadTypeYear & tra_infr == input$roadType) %>%
ggplot(aes(x = reorder(country, values), y = values, fill = ifelse(geo == "PL", "Highlighted", "Normal"))) +
geom_bar(stat = "identity") +
theme(legend.position = "none", axis.title.y = element_blank()) +
labs(y = 'Ofiary', x = NULL, caption = "Źródło: Eurostat") +
coord_flip()
}, width = 687, height = 'auto')
output$mapByRoadTypePlot <- renderPlot({
mapdata_road <- geo %>%
right_join(filter(dat_road, time == input$roadTypeYear & tra_infr == input$roadType))
ggplot(mapdata_road, aes(fill = values)) +
geom_sf(color = alpha('black', 1/3), alpha = .6) +
scale_fill_gradient(low="white", high="red") +
coord_sf(xlim = c(-20,44), ylim = c(30,70)) +
labs(fill = "Ofiary", caption = "Źródło: Eurostat")
}, width = 800, height = 700)
output$timeChangePlot <- renderPlot({
dat_timechange <- dat %>%
filter(unit == ifelse(input$timeChangefactor, 'RT', 'NR') & geo %in% input$countries)
dat_timechange %>%
ggplot(aes(x = time, y = values, color = geo, label = country)) +
geom_line(size = .9, alpha = .5) +
geom_text(data = dat_timechange %>% group_by(geo) %>% filter(time == max(time)), size = 3) +
theme(legend.position = 'none') +
labs(x = "Rok", y = "Ofiary", caption = "Źródło: Eurostat")
}, width = 800, height = 'auto')
output$nutsPlot <- renderPlot({
mapdata_nuts <- geo_nuts %>%
left_join(dat_nuts) %>%
filter(time == input$nutsYear, victim == input$victimType, unit == ifelse(input$nutsfactor, 'P_MHAB', 'NR'), CNTR_CODE != 'TR') %>%
mutate(cat = cut_to_classes(values, n = 5, decimals = 1))
ggplot(mapdata_nuts, aes(fill = cat)) +
scale_fill_brewer(palette = 'Reds') +
geom_sf(color = alpha('black', 1/3), alpha = .6) +
xlim(c(-12,44)) + ylim(c(35,70)) +
labs(fill = "Ofiary", caption = "Źródło: Eurostat")
}, width = 800, height = 700)
output$germanyPlot <- renderPlot({
mapdata_de <- geo_nuts %>%
left_join(dat_nuts) %>%
filter(time == input$germanyYear, victim == input$germanyVictimType, unit == ifelse(input$germanyFactor, 'P_MHAB', 'NR'), CNTR_CODE == 'DE') %>%
mutate(cat = cut_to_classes(values, n = 5, decimals = 1))
min_cat <- mapdata_de %>%
filter(values == min(values)) %>%
slice(1)
if (input$ddrBorder == FALSE) {
ggplot(mapdata_de, aes(fill = cat)) +
scale_fill_brewer(palette = 'Reds') +
geom_sf(color = alpha('black', 1/3), alpha = .6) +
xlim(c(5,15)) + ylim(c(47,55)) +
labs(fill = "Ofiary", caption = "Źródło: Eurostat")
} else {
ggplot() +
geom_sf(data = mapdata_de, mapping = aes(fill = cat), color = alpha('white', 1/3)) +
scale_fill_brewer(palette = 'Reds', direction = 1, guide = 'legend') +
geom_polygon(data = div_de, mapping = aes(long, lat, group = group, fill = min_cat$cat, alpha = 1), color = 'black', show.legend = FALSE) +
labs(fill = "Ofiary", caption = "Źródło: Eurostat")
}
}, width = 800, height = 700)
output$info <- renderText({
"Test"
})
})