Skip to content

Instantly share code, notes, and snippets.

Created January 30, 2018 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/602948bce91e61f36fd3eb0c4259d26c to your computer and use it in GitHub Desktop.
Save anonymous/602948bce91e61f36fd3eb0c4259d26c to your computer and use it in GitHub Desktop.
Main script for Dutch quality-of-life-o-meter Shiny app
# Set working directory to folder containing the current running script
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
# Clear working space
rm(list=ls())
# Check whether required packages are availale and if not, install them
if(!("readxl" %in% rownames(installed.packages()))) {
install.packages("readxl")}
if(!("raster" %in% rownames(installed.packages()))) {
install.packages("raster")}
if(!("rgdal" %in% rownames(installed.packages()))) {
install.packages("rgdal")}
if(!("sp" %in% rownames(installed.packages()))) {
install.packages("sp")}
if(!("sf" %in% rownames(installed.packages()))) {
install.packages("sf")}
if(!("leaflet" %in% rownames(installed.packages()))) {
install.packages("leaflet")}
library("readxl")
library("raster")
library("rgdal")
library("sp")
library("sf")
library("leaflet")
# Check if data folder exists, and if not create folder
if (dir.exists("data") == FALSE)
{dir.create("data")}
# Download, unzip, delete zip and load the Municipalities borders 2016
download.file(url = 'https://data.overheid.nl/OpenDataSets/LBM2016/Gemeente2016.zip', destfile = 'Gemeente2016shp.zip', method = 'auto')
unzip('Gemeente2016shp.zip', exdir = "./data")
unlink('Gemeente2016shp.zip')
# Download, unzip, delete zip and load the Gemeente data 2016
download.file(url = 'https://data.overheid.nl/OpenDataSets/LBM2016/Gemeente.zip', destfile = 'Gemeente2016XLS.zip', method = 'auto')
unzip('Gemeente2016XLS.zip', exdir = "./data")
unlink('Gemeente2016XLS.zip')
# Read first excelsheet of excel file
GemeenteStand121416 <- read_excel("./data/Gemeente/dimensiescore_gemeente (stand).xlsx", 1)
# Load shape file of municipalities boundaries
Mun_boundaries <- readOGR(dsn = "./data/Gemeente2016/", layer = "gem_2016")
# Select municipalities scores of year 2016
GemeenteStand16 <- GemeenteStand121416[which(GemeenteStand121416$JAAR == "2016"), ]
# Create data frame
GemeenteStand2016df <- data.frame(GemeenteStand16)
# Change first column of GremeenteStand2016df 'GBD' into 'GM_CODE' in order to merge with shapefile
colnames(GemeenteStand2016df)[1] <- "GM_CODE"
# Before merging, check if number of rows corresponds, if TRUE, proceed
if (nrow(Mun_boundaries) != nrow(GemeenteStand2016df)){
print ("Number of rows unequal, merge failed")
} else {
# Merge shapefile with GemeenteStand2016 based on common variable 'GM_CODE'
MunScores2016_long <- merge(Mun_boundaries, GemeenteStand2016df, by='GM_CODE');
# Select only required columns
MunScores2016 <- subset(MunScores2016_long, select= c(GM_CODE, GM_NAAM, AANT_INW, RLBRMTR : RLBFYS))
# Rename columns
colnames(MunScores2016@data) <- c("Municipality_Code", "Municipality_Name", "Population",
"Total_Score_2016", "Housing_Score_2016",
"Population_Score_2016", "Provisions_Score_2016",
"Safety_Score_2016", "PhysicalEnvironment_Score_2016")
# Write final MunScores2016 to file
write.csv(MunScores2016, file = "./data/MunScores2016.csv")
# Save MunScores2016 as shapefile again
shapefile(MunScores2016, "./data/MunScores2016.shp", overwrite = TRUE)
}
#Transform shapefile to WGS84
MunScores2016 <- spTransform(MunScores2016, CRS("+proj=longlat +datum=WGS84 +no_defs"))
shapefile(MunScores2016, "./data/MunScores2016wgs84.shp", overwrite = TRUE)
######################### Visualization
#Create colour palette for map colours
pal <- colorNumeric(
palette = "Greens",
domain = MunScores2016$Total_Score_2016,
n = 6)
# Format popup data for leaflet map.
popup_dat <- paste0("<strong>Municipality:</strong>",
MunScores2016$Municipality_Name,
"<br><strong>Quality-of-life-o-meter says: </strong>",
MunScores2016$Total_Score_2016)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment