Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .Rbuildignore
100644 → 100755
Empty file.
Empty file modified .github/.gitignore
100644 → 100755
Empty file.
Empty file modified .github/workflows/lint.yaml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions DESCRIPTION
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Imports:
magrittr,
readr,
rlang,
stringr,
tibble
Suggests:
testthat (>= 3.0.0)
Expand Down
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified LICENSE.md
100644 → 100755
Empty file.
Empty file modified NAMESPACE
100644 → 100755
Empty file.
Empty file modified NEWS.md
100644 → 100755
Empty file.
Empty file modified R/access.R
100644 → 100755
Empty file.
10 changes: 7 additions & 3 deletions R/find_latest_file.R
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ find_latest_file <- function(directory,
path = directory,
type = "file",
regexp = regexp,
recurse = TRUE
recurse = FALSE
) |>
dplyr::arrange(
dplyr::desc(.data$path),
Expand All @@ -55,11 +55,15 @@ find_latest_file <- function(directory,
}

if (nrow(latest_file) == 1L) {
cli::cli_alert_info("Using {.val {fs::path_file(latest_file$path)}}.")
cli::cli_alert_info(
"Using the latest available version: {.val {fs::path_file(
fs::path_ext_remove(latest_file$path))}}.
If you require an older version specify an argument `version`."
)
} else {
cli::cli_abort(
"There was no file in {.path {directory}} that matched the
regular expression {.val {regexp}}"
regular expression {.val {regexp}}"
)
}

Expand Down
Empty file modified R/get_hscp_locality.R
100644 → 100755
Empty file.
Empty file modified R/get_lookups_dir.R
100644 → 100755
Empty file.
Empty file modified R/get_simd_datazone.R
100644 → 100755
Empty file.
Empty file modified R/get_simd_postcode.R
100644 → 100755
Empty file.
40 changes: 33 additions & 7 deletions R/get_spd.R
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#' Get the Scottish Postcode Directory
#'
#' @param version Default is "latest", otherwise supply a tag e.g. "2023_2"
#' @inheritParams arrow::read_parquet
#' Read a Scottish Postcode Directory (SPD) lookup file into a tibble.
#' @param version A string defining a version to read in. The default value
#' is "latest" and the latest SPD file available on cl-out will be loaded.
#' Alternatively you can supply a tag, e.g. "2023_2", to load a specific file.
#' @param col_select A character vector of column names to keep, as in
#' the "select" argument to data.table::fread(), or a tidy selection
#' specification of columns, as used in dplyr::select().
#'
#' @return a [tibble][tibble::tibble-package] of the Scottish Postcode Directory
#' @return A [tibble][tibble::tibble-package] of the Scottish Postcode
#' Directory lookup file or its selected columns.
#' @export
#'
#' @examples
#' get_spd()
#' get_spd(col_select = c("pc7", "latitude", "longitude"))
#' get_spd(version = "2023_2", col_select = c("pc7", "latitude", "longitude"))
get_spd <- function(
version = "latest",
col_select = NULL) {
Expand All @@ -21,9 +27,29 @@ get_spd <- function(
selection_method = "file_name"
)
} else {
spd_path <- fs::path(
dir,
glue::glue("Scottish_Postcode_Directory_{version}.parquet")
if (!stringr::str_detect(version, "^20\\d{2}_[1-2]$")) {
cli::cli_abort(c(
"x" = "Invalid version name: {.val {version}}",
"i" = "It should follow pattern YYYY_1 or YYYY_2",
call = NULL
))
}

name_ver_list <- paste0(
"Scottish_Postcode_Directory_", version,
c(".parquet", ".rds", ".csv")
)
path_ver_list <- fs::path(dir, "Archive", name_ver_list)
spd_path <- path_ver_list[file.exists(path_ver_list)][1]
Comment thread
bnowok marked this conversation as resolved.
Outdated
}
Comment thread
Moohan marked this conversation as resolved.
Outdated

if (is.na(spd_path)) {
cli::cli_abort(
c(
"x" = "SPD version {.val {version}} is NOT available",
"i" = "Contact phs.geography@phs.scot"
),
call = NULL, rlang_backtrace_on_error = "none"
)
}

Expand Down
Empty file modified R/phslookups-package.R
100644 → 100755
Empty file.
15 changes: 12 additions & 3 deletions R/read_file.R
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ read_file <- function(path, col_select = NULL, ...) {
))
}

if (!(file.exists(path))) {
cli::cli_abort(
c(
"x" = "File {.val {fs::path_file(fs::path_ext_remove(path))}}
is NOT available",
"i" = "Contact phs.geography@phs.scot"
Comment thread
bnowok marked this conversation as resolved.
Outdated
),
call = NULL, rlang_backtrace_on_error = "none"
)
}

data <- switch(ext,
"rds" = readr::read_rds(file = path),
Comment thread
bnowok marked this conversation as resolved.
Outdated
"csv" = readr::read_csv(
file = path,
...,
col_select = {{ col_select }},
file = path, guess_max = 50000, ...,
show_col_types = FALSE
),
"parquet" = tibble::as_tibble(arrow::read_parquet(
Expand Down
Empty file modified R/zzz.R
100644 → 100755
Empty file.
Empty file modified README.Rmd
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified _pkgdown.yml
100644 → 100755
Empty file.
Empty file modified man/get_hscp_locality.Rd
100644 → 100755
Empty file.
Empty file modified man/get_simd_datazone.Rd
100644 → 100755
Empty file.
Empty file modified man/get_simd_postcode.Rd
100644 → 100755
Empty file.
18 changes: 10 additions & 8 deletions man/get_spd.Rd
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified phslookups.Rproj
100644 → 100755
Empty file.
Empty file modified tests/testthat.R
100644 → 100755
Empty file.
Empty file modified tests/testthat/_snaps/linux-gnu/get_lookups_dir.md
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions tests/testthat/test-find_latest_file.R
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ test_that("By default will find the last created file", {
find_latest_file(dir, "[a-z]\\d.txt"),
fs::path(dir, "b0.txt")
) |>
expect_message("\"b0.txt\"")
expect_message("\"b0\"")
})

test_that("Can find latest file by file name", {
expect_equal(
find_latest_file(dir, "[a-z]\\d.txt", "file_name"),
fs::path(dir, "z9.txt")
) |>
expect_message("\"z9.txt\"")
expect_message("\"z9\"")
})
Empty file modified tests/testthat/test-get_hscp_locality.R
100644 → 100755
Empty file.
Empty file modified tests/testthat/test-get_lookups_dir.R
100644 → 100755
Empty file.
Empty file modified tests/testthat/test-get_simd_datazone.R
100644 → 100755
Empty file.
Empty file modified tests/testthat/test-get_simd_postcode.R
100644 → 100755
Empty file.
Empty file modified tests/testthat/test-get_spd.R
100644 → 100755
Empty file.
Empty file modified tests/testthat/test-read_file.R
100644 → 100755
Empty file.