Skip to content
This repository was archived by the owner on Jan 7, 2024. It is now read-only.

Commit 8898336

Browse files
committed
Improve use of oa_lad21_lookup custom lookup
1 parent 466fb0c commit 8898336

File tree

7 files changed

+105
-37
lines changed

7 files changed

+105
-37
lines changed

DESCRIPTION

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: jogger
2-
Version: 0.2.15
2+
Version: 0.2.16
33
Title: Retrieve Area Boundaries and Data from the ONS Open Geography Portal
44
Description: The main function of this package is to download area boundaries
55
(in GeoJSON format) using the ONS Open Geography API, for all sub-areas - at a
@@ -12,7 +12,6 @@ Authors@R: person("Francis", "Barton",
1212
role = c("aut", "cre"))
1313
License: MIT + file LICENSE
1414
Encoding: UTF-8
15-
LazyData: true
1615
Roxygen: list(markdown = TRUE)
1716
RoxygenNote: 7.1.1
1817
Imports:

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.2.16
2+
3+
* Simplifies `treat_results` routine and use of `oa_lad21_lookup` custom lookup
4+
15
# 0.2.15
26

37
* Fixes internal data issue where datasets weren't being found

R/create_custom_lookup.R

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ create_custom_lookup <- function(bounds_level,
159159
if (within_level == "cty") {
160160
within_level <- "utla"
161161
}
162-
if (within_level == "ltla") {
163-
within_level <- "lad"
164-
}
162+
# if (within_level == "ltla") {
163+
# within_level <- "lad"
164+
# }
165165
}
166166

167167
if (bounds_level == "lad" && within_level %in% c("utla", "rgn")) {
@@ -246,24 +246,22 @@ create_custom_lookup <- function(bounds_level,
246246
}
247247

248248
if (return_style == "minimal") {
249-
df %>%
249+
df1 <- df %>%
250250
# return just first two columns
251-
dplyr::select(!!rlang::sym(fields[1]):!!rlang::sym(fields[2])) %>%
252-
dplyr::distinct() %>%
253-
janitor::remove_empty("cols")
251+
dplyr::select(!!rlang::sym(fields[1]):!!rlang::sym(fields[2]))
254252
} else if (return_style == "simple") {
255-
df %>%
253+
df1 <- df %>%
256254
# return just the columns in "fields"
257-
dplyr::select(any_of(fields)) %>%
258-
dplyr::distinct() %>%
259-
janitor::remove_empty("cols")
255+
dplyr::select(any_of(fields))
260256
} else {
261-
df %>%
257+
df1 <- df %>%
262258
# return all columns between first and last specified fields
263-
dplyr::select(!!rlang::sym(fields[1]):!!rlang::sym(fields[end_col])) %>%
264-
dplyr::distinct() %>%
265-
janitor::remove_empty("cols")
259+
dplyr::select(!!rlang::sym(fields[1]):!!rlang::sym(fields[end_col]))
266260
}
261+
262+
df1 %>%
263+
dplyr::distinct() %>%
264+
janitor::remove_empty("cols")
267265
}
268266

269267

@@ -272,38 +270,29 @@ create_custom_lookup <- function(bounds_level,
272270
if (within_cd) nth_field <- -2
273271
if (bounds_cd) nth_field <- 1
274272

273+
where_level <- dplyr::nth(fields, nth_field)
275274

276-
# no available ONS API lookup for UTLA:RGN, so use our built-in table
277-
# (also prefer it for LAD:RGN lookups!):
278-
if (bounds_level %in% c("upper", "utla") && within_level %in% c("region", "rgn")) {
279-
df_out <- upper_tier_region_ctry_lookup %>%
280-
dplyr::select(!(c(ltla21cd, ltla21nm, ctry21cd, ctry21nm))) %>%
281-
dplyr::filter(rgn21nm %in% within) %>%
282-
dplyr::distinct()
283-
} else if (bounds_level %in% c("lad", "ltla") && within_level %in% c("region", "rgn")) {
284-
df_out <- upper_tier_region_ctry_lookup %>%
285-
dplyr::select(!(c(ctry21cd, ctry21nm))) %>%
286-
dplyr::filter(rgn21nm %in% within)
287-
} else if (bounds_level %in% c("upper", "utla") && within_level %in% c("country", "ctry")) {
288-
df_out <- upper_tier_region_ctry_lookup %>%
289-
dplyr::filter(ctry21nm %in% within) %>%
275+
if (all(fields %in% names(oa_lad21_lookup))) {
276+
df1 <- oa_lad21_lookup %>%
277+
dplyr::filter(.data[[where_level]] %in% within) %>%
290278
dplyr::distinct()
291279
} else {
292280
# and for other things use the API as usual:
293-
294-
df_out <- within %>%
281+
df1 <- within %>%
295282
batch_it_simple(batch_size = 25) %>% # from my myrmidon pkg
296283
purrr::map_df( ~ build_api_query(
297284
ref = table_code_ref,
298-
where_level = dplyr::nth(fields, nth_field),
285+
where_level = where_level,
299286
where = .,
300287
fields = return_fields
301288
) %>%
302-
extract_lookup() %>%
303-
treat_results(return_style = return_style)
289+
extract_lookup()
304290
)
305291
}
306292

293+
df_out <- df1 %>%
294+
treat_results(return_style = return_style)
295+
307296

308297
# if not specified by the user, make an educated decision about
309298
# including Welsh language MSOA and LAD names (LAD19NMW / MSOA11NMW /

R/oa_lad21_lookup.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#' A lookup of OAs in England and Wales to higher geographies
2+
#'
3+
#' @format A data frame with 181408 rows and 15 variables:
4+
#' \describe{
5+
#' \item{oa11cd}{OA code}
6+
#' \item{lsoa21cd}{LSOA code}
7+
#' \item{lsoa21nm}{LSOA name}
8+
#' \item{msoa11cd}{MSOA code}
9+
#' \item{msoa11nm}{MSOA name}
10+
#' \item{lad20cd}{LAD code}
11+
#' \item{lad20nm}{LAD name}
12+
#' \item{ltla21cd}{LTLA code}
13+
#' \item{ltla21nm}{LTLA name}
14+
#' \item{utla21cd}{UTLA code}
15+
#' \item{utla21nm}{UTLA name}
16+
#' \item{rgn21cd}{Region code}
17+
#' \item{rgn21nm}{Region name}
18+
#' \item{ctry21cd}{Country code}
19+
#' \item{ctry21nm}{Country name}
20+
#' }
21+
"oa_lad21_lookup"

R/sysdata.rda

1.01 MB
Binary file not shown.

data-raw/datasets.R

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ lad21nmw_lookup <- jsonlite::fromJSON(
6464

6565

6666

67+
nnhts <- c("Corby", "East Northamptonshire", "Kettering", "Wellingborough")
68+
wnhts <- c("Daventry", "Northampton", "South Northamptonshire")
69+
70+
71+
oa_lad21_lookup <- jsonlite::fromJSON(
72+
# https://geoportal.statistics.gov.uk/datasets/ons::output-area-to-lower-layer-super-output-area-to-middle-layer-super-output-area-to-local-authority-district-december-2020-lookup-in-england-and-wales/
73+
"https://opendata.arcgis.com/datasets/65664b00231444edb3f6f83c9d40591f_0.geojson"
74+
) %>%
75+
purrr::pluck("features", "properties") %>%
76+
janitor::clean_names() %>%
77+
dplyr::select(!c(fid, rgn20cd, rgn20nm)) %>%
78+
dplyr::mutate(ltla21nm = dplyr::case_when(
79+
lad20nm %in% nnhts ~ "North Northamptonshire",
80+
lad20nm %in% wnhts ~ "West Northamptonshire",
81+
TRUE ~ lad20nm
82+
)) %>%
83+
dplyr::left_join(upper_tier_region_ctry_lookup) %>%
84+
dplyr::relocate(ltla21cd, .before = ltla21nm)
85+
86+
87+
88+
6789

6890
hocl_msoa_names <- paste0(
6991
"https://houseofcommonslibrary.github.io/",
@@ -72,4 +94,4 @@ hocl_msoa_names <- paste0(
7294
dplyr::select(-Laname)
7395

7496

75-
usethis::use_data(lsoa11cdnm, upper_tier_region_ctry_lookup, lad21nmw_lookup, hocl_msoa_names, overwrite = TRUE, internal = TRUE, compress = "bzip2")
97+
usethis::use_data(lsoa11cdnm, upper_tier_region_ctry_lookup, lad21nmw_lookup, hocl_msoa_names, oa_lad21_lookup, overwrite = TRUE, internal = TRUE, compress = "bzip2")

man/oa_lad21_lookup.Rd

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)