-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathglobal.R
More file actions
84 lines (61 loc) · 3.27 KB
/
Copy pathglobal.R
File metadata and controls
84 lines (61 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# What is global.R? -------------------------------------------------------
# a script executed before app launches
# the objects generated here can be used both in user and sever
# these are actions that can be done once per session
# such as library calls, data source loading and custom function sourcing
# PACKAGES ---------------------------------------------------------------
library(leaflet)
library(RColorBrewer)
library(sp)
library(tidyverse)
library(rgdal)
library(testthat)
library(xtable)
library(DT)
# library(checkpoint)
# checkpoint("2016-12-28") # ymd
# DATA INPUT -----------------------------------------------------------------
# LA POLYGON --------------------------------------------------------------
# Get polygon for LA of interest boundary
## load LA shapefile (to identify LA boundaries)
la_s <- readOGR("data/.", "England_LA_2009a", verbose = FALSE)
## convert LA shapefile coordinates to lat/long
la_s_ll <- spTransform(la_s, CRS("+init=epsg:4326"))
# QA ----------------------------------------------------------------------
expect_equal(length(la_s_ll@data$LEA_NAME), 152,
info = "there are 152 LA, do we have data for all?")
# We can add some tests into our code using the testthat package
# This provides quality assurance for our inputs and outputs
# http://www.machinegurning.com/rstats/test-driven-development/
# SCHOOL COORD, SCAP and Surplus land data ----------------------------------
school_locations <- read_rds("data/school_locations.rds")
apples <- read_rds("data/apples_data.rds")
pears <- read_rds("data/pears_data.rds")
# JOIN DATA ---------------------------------------------------------------
fruits <- dplyr::left_join(apples, pears)
# MAPPING SETUP -----------------------------------------------------------
# Variables for holding the coordinate system types (see: # http://www.epsg.org/ for details)
ukgrid <- "+init=epsg:27700"
latlong <- "+init=epsg:4326"
# Create coordinates variable
coords <- dplyr::select(fruits, easting, northing)
# Create the SpatialPointsDataFrame, note coords and data are distinct slots in S4 object
surplus_sp <- sp::SpatialPointsDataFrame(coords,
data = dplyr::select(fruits, -easting, -northing),
proj4string = CRS("+init=epsg:27700"))
# CONVERT TO LONG & LAT ---------------------------------------------------
# Convert from Eastings and Northings to Latitude and Longitude
surplus_sp_ll <- spTransform(surplus_sp, CRS(latlong))
# we also need to rename the columns
colnames(surplus_sp_ll@coords)[colnames(surplus_sp_ll@coords) == "easting"] <- "longitude"
colnames(surplus_sp_ll@coords)[colnames(surplus_sp_ll@coords) == "northing"] <- "latitude"
# With the data in place, the user, via the app, can select the appropriate LA of interest to filter for
# EFFICIENCY --------------------------------------------------------------
la_code_list_sorted <- sort(unique(school_locations$la_number))
# USER Friendly labels for drop down list
la_user_friendly_list <- unique(select(school_locations, la_number, la)) %>%
mutate(la_combined = paste(la_number, la, sep = " - ")) %>%
select(la_combined) %>%
as.vector()
# POPUP DIAGNOSIS ---------------------------------------------------------
source("make_popup_vector_from_numeric.R")