1- # ' Get a SIMD Postcode Lookup
1+ # ' Get Postcode- SIMD lookup
22# '
3- # ' @param postcode_version Default is "latest", otherwise supply a tag
4- # ' e.g. "2023_2"
5- # ' @param simd_version Default is "latest", otherwise supply a version
6- # ' e.g. "2020v2"
7- # ' @inheritParams arrow::read_parquet
3+ # ' Read a Postcode-Scottish Index of Multiple Deprivation (SIMD) lookup file
4+ # ' from cl-out into a tibble.
5+ # ' @param postcode_version A string defining a postcode version. The default
6+ # ' value is "latest". Alternatively you can supply a string defining
7+ # ' a specific version that you would like to load. It should follow pattern
8+ # ' "YYYY_1" or "YYYY_2", e.g. "2023_2".
9+ # ' @param simd_version A string defining a SIMD version. The default value
10+ # ' is "latest". Alternatively you can supply a string defining a specific
11+ # ' version. It should follow pattern "YYYY" or "YYYYv2", e.g. "2020v2".
12+ # ' @inheritParams readr::read_csv
813# '
914# ' @return a [tibble][tibble::tibble-package] of the SIMD Postcode lookup
1015# ' @export
1116# '
1217# ' @examples
1318# ' get_simd_postcode()
14- # ' get_simd_postcode(col_select = c("pc7", "simd2020v2_rank"))
19+ # ' get_simd_postcode(postcode_version = "2016_1", simd_version = "2012")
20+ # '
21+ # ' library(dplyr)
22+ # ' get_simd_postcode(
23+ # ' postcode_version = "2016_1",
24+ # ' simd_version = "2012",
25+ # ' col_select = c("pc7", starts_with("simd"))
26+ # ' )
1527get_simd_postcode <- function (
1628 postcode_version = " latest" ,
1729 simd_version = " latest" ,
1830 col_select = NULL ) {
1931 dir <- fs :: path(get_lookups_dir(), " Deprivation" )
2032
21- if (postcode_version != " latest" && simd_version != " latest" ) {
22- simd_postcode_path <- fs :: path(
23- dir ,
24- glue :: glue(" postcode_{postcode_version}_simd{simd_version}.parquet" )
25- )
26- } else {
33+ if (postcode_version == " latest" && simd_version == " latest" ) {
2734 regexp <- paste0(
2835 " postcode_" ,
2936 ifelse(postcode_version == " latest" , " \\ d{4}_[1-2]" , postcode_version ),
@@ -37,6 +44,39 @@ get_simd_postcode <- function(
3744 regexp = regexp ,
3845 selection_method = " file_name"
3946 )
47+ } else if (postcode_version != " latest" && simd_version != " latest" ) {
48+ valid_postcode_version <- stringr :: str_detect(
49+ postcode_version ,
50+ " \\ d{4}_[1-2]"
51+ )
52+ valid_simd_version <- stringr :: str_detect(
53+ simd_version ,
54+ " ^20[0-9]{2}(:?v2)?$"
55+ )
56+
57+ if (! valid_postcode_version || ! valid_simd_version ) {
58+ cli :: cli_abort(c(
59+ " x" = " Invalid version specification, Postcode:
60+ {.val {postcode_version}}, SIMD: {.val {simd_version}}" ,
61+ " i" = " Postcode should be follow the pattern YYYY_1 or YYYY_2" ,
62+ " i" = " SIMD should follow the pattern YYYY or YYYYv2"
63+ ))
64+ }
65+
66+ simd_postcode_path <- find_specific_file(
67+ directory = dir ,
68+ lookup_type = " SIMD Postcode" ,
69+ version = list (
70+ postcode_version = postcode_version ,
71+ simd_version = simd_version
72+ )
73+ )
74+ } else {
75+ # Case when one of the versions is 'latest' but the other isn't
76+ cli :: cli_abort(c(
77+ " x" = " When using a version other than {.val latest} both
78+ {.arg postcode_version} and {.arg simd_version} must be specified."
79+ ))
4080 }
4181
4282 return (read_file(
0 commit comments