Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3a9c87c
Added spd metadata.
Nic-Chr Jul 31, 2025
ca376b9
Added metadata helper.
Nic-Chr Aug 29, 2025
6abe8b3
New helpers and better messaging.
Nic-Chr Aug 29, 2025
71a3c9a
Added helpful messages.
Nic-Chr Aug 29, 2025
7cdc902
Style code (GHA)
Nic-Chr Aug 29, 2025
0192ec8
Merge branch 'main' into dev/metadata
Moohan Nov 17, 2025
95d3a94
Merge branch 'main' into dev/metadata
Moohan Jan 12, 2026
8847246
Rename cols with readr
Nic-Chr Mar 9, 2026
193c59a
Use base print
Nic-Chr Mar 9, 2026
e71f447
Use read_file and added file check warning
Nic-Chr Mar 9, 2026
e7f4b34
Style code (GHA)
Nic-Chr Mar 9, 2026
ac7a3aa
Merge branch 'main' into dev/metadata
bnowok Apr 13, 2026
067ef01
Merge branch 'main' into dev/metadata
Moohan Apr 29, 2026
cfd0335
Refactor metadata messaging into the metadata fucntions
Moohan May 1, 2026
2a1e692
Add an error if `metadata` is used on a non-tibble
Moohan May 1, 2026
055cf4e
Fix and improve tests to expect the messaging
Moohan May 1, 2026
352ff49
Improve CSV lazy read and suppress progress messages
Moohan May 1, 2026
77ef99c
Style code (GHA)
Moohan May 1, 2026
808a8ba
Refactor to make loading the metadata be on demand
Moohan May 1, 2026
c05d856
Merge pull request #51 from Public-Health-Scotland/dev/metadata-lazy-…
Moohan May 4, 2026
764cb2e
Document package (GHA)
Moohan May 4, 2026
c38bae4
Add error handling for missing metadata reference
Moohan May 6, 2026
d6bd04f
Update warning message for metadata version
bnowok May 6, 2026
9a279d9
Clarify parameter description in metadata function
bnowok May 6, 2026
2aaa7c0
Fix roxygen2 configuration in DESCRIPTION file
bnowok May 6, 2026
2746559
Style code (GHA)
bnowok May 6, 2026
857b276
Document package (GHA)
bnowok May 6, 2026
a833657
Fix comment formatting in metadata.R
bnowok May 6, 2026
28eb000
Style code (GHA)
bnowok May 6, 2026
d4586d5
Clean up comments in metadata function
bnowok May 6, 2026
c528474
Document package (GHA)
bnowok May 6, 2026
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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Imports:
tibble
Suggests:
testthat (>= 3.0.0)
Config/roxygen2/markdown: TRUE
Config/roxygen2/version: 8.0.0
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.3
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export(get_hscp_locality)
export(get_simd_datazone)
export(get_simd_postcode)
export(get_spd)
export(metadata)
importFrom(rlang,.data)
importFrom(tibble,tibble)
26 changes: 23 additions & 3 deletions R/get_spd.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
#' get_spd(version = "2023_2", col_select = c("pc7", "latitude", "longitude"))
get_spd <- function(version = "latest", col_select = NULL) {
spd_dir <- fs::path(
get_lookups_dir(), "Geography",
get_lookups_dir(),
"Geography",
"Scottish Postcode Directory"
)

metadata_dir <- fs::path(spd_dir, "Metadata")

if (version == "latest") {
spd_path <- find_latest_file(
directory = spd_dir,
Expand All @@ -53,8 +56,25 @@ get_spd <- function(version = "latest", col_select = NULL) {
)
}

return(read_file(
spd <- read_file(
spd_path,
col_select = {{ col_select }}
))
)
Comment thread
Moohan marked this conversation as resolved.

metadata_path <- fs::path(metadata_dir, "spd_metadata.csv")
metadata_exists <- fs::file_exists(metadata_path)

spd <- set_metadata_ref(
spd,
type = "SPD",
path = metadata_path,
version = version,
exists = metadata_exists
)

if (metadata_exists) {
inform_metadata_access(spd)
}

return(spd)
}
89 changes: 89 additions & 0 deletions R/metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#' Function to access metadata
#'
#' @param data Dataset imported using one of the [phslookups] functions,
#' e.g. `get_spd()`.
#'
#' @returns
#' Metadata `tibble` associated with dataset.
#'
#' @examples
#' library(phslookups)
#' \dontrun{
#' spd <- get_spd()
#' metadata(spd)
#' }
#' @export
metadata <- function(data) {
if (!inherits(data, "tbl_df")) {
cli::cli_abort(
"{.arg data} must must be a tibble loaded using {.pkg phslookups}."
)
}

# If already loaded, return immediately
metadata <- attr(data, "metadata", exact = TRUE)
if (!is.null(metadata)) {
return(metadata)
}

ref <- attr(data, "metadata_ref", exact = TRUE)

if (is.null(ref)) {
cli::cli_abort("Metadata is not available for this data.")
}

if (rlang::is_false(ref$exists)) {
cli::cli_abort(c(
"x" = "{ref$type} metadata not available.",
"i" = "Expected at {.path {ref$path}}"
))
}

metadata <- read_file(
ref$path,
col_select = 1:2,
col_names = c("variable", "description"),
skip = 1,
col_types = readr::cols_only(
variable = readr::col_character(),
description = readr::col_character()
)
)

inform_metadata_version(ref$version)

# Attach metadata to the object
set_metadata(data, metadata)

metadata
}

set_metadata_ref <- function(data, path, type, version, exists) {
attr(data, "metadata_ref") <- list(
path = path,
type = type,
version = version,
exists = exists
)
data
}

set_metadata <- function(data, metadata) {
attr(data, "metadata") <- metadata
data
}

inform_metadata_access <- function(metadata) {
cli::cli_inform(c(
i = "Metadata is available and can be accessed using {.fun metadata}."
))
}

inform_metadata_version <- function(version) {
if (version != "latest") {
cli::cli_warn(
"Metadata corresponds to the latest version of the data
and may not exactly match the data currently loaded."
)
}
}
12 changes: 7 additions & 5 deletions R/read_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ read_file <- function(path, col_select = NULL, ...) {
}

lookup <- switch(ext,
rds = tibble::as_tibble(readr::read_rds(file = path)),
csv = readr::read_csv(
"rds" = tibble::as_tibble(readr::read_rds(file = path)),
"csv" = readr::read_csv(
file = path,
guess_max = 50000L,
...,
show_col_types = FALSE
guess_max = 50000,
progress = FALSE,
show_col_types = FALSE,
lazy = .Platform$OS.type == "unix",
...
),
parquet = tibble::as_tibble(arrow::read_parquet(
file = path,
Expand Down
25 changes: 25 additions & 0 deletions man/metadata.Rd

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

19 changes: 13 additions & 6 deletions tests/testthat/test-get_spd.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ skip_on_ci()

test_that("spd is returned", {
get_spd() |>
expect_message()
expect_message("Using the latest available version") |>
expect_message("Metadata is available ")

spd <- suppressMessages(get_spd())

Expand All @@ -19,30 +20,36 @@ test_that("col selection works", {
get_spd(col_select = "pc7"),
"pc7"
) |>
expect_message()
expect_message("Using the latest available version") |>
expect_message("Metadata is available ")
expect_named(
get_spd(col_select = c("pc7", "pc8")),
c("pc7", "pc8")
) |>
expect_message()
expect_message("Using the latest available version") |>
expect_message("Metadata is available ")
})

test_that("col selection works with tidyselect", {
expect_named(
get_spd(col_select = c("pc7", dplyr::starts_with("hb")))
) |>
expect_message()
expect_message("Using the latest available version") |>
expect_message("Metadata is available ")

expect_named(
get_spd(col_select = dplyr::matches("pc[78]")),
c("pc7", "pc8")
) |>
expect_message()
expect_message("Using the latest available version") |>
expect_message("Metadata is available ")
})


test_that("reading from archive works", {
expect_s3_class(get_spd(version = "2024_1"), "tbl_df")
get_spd(version = "2024_1") |>
expect_s3_class("tbl_df") |>
expect_message("Metadata is available ")
expect_error(get_spd(version = "2010_1", "SPD version .+? is NOT available"))
expect_error(get_spd(version = "20243"), "Invalid version name:")
})
Expand Down