Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
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)
20 changes: 18 additions & 2 deletions R/get_spd.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ get_spd <- function(version = "latest", col_select = NULL) {
"Scottish Postcode Directory"
)

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

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

return(read_file(
metadata <- readr::read_csv(fs::path(metadata_dir, "spd_metadata.csv")) %>%
Comment thread
Moohan marked this conversation as resolved.
Outdated
dplyr::select(1:2) %>%
stats::setNames(c("variable", "description"))
Comment thread
Moohan marked this conversation as resolved.
Outdated

inform_metadata_access()

inform_metadata_version(version)

cli::cat_line("\n--- Metadata ---\n", col = "blue")
cli::cat_print(metadata)
cli::cat_line("")
Comment thread
bnowok marked this conversation as resolved.
Outdated

spd <- read_file(
spd_path,
col_select = {{ col_select }}
))
)
Comment thread
Moohan marked this conversation as resolved.
spd <- set_metadata(spd, metadata)
return(spd)
}
41 changes: 41 additions & 0 deletions R/metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' Function to access metadata
#'
#' @param data Dataset imported via e.g. `get_spd()`.
#'
#' @returns
#' Metadata `tibble` associated with dataset.
#'
#' @examples
#' library(phslookups)
#'
#' \dontrun{
#' spd <- get_spd()
#'
#' metadata(spd)
#' }
#'
#' @export
metadata <- function(data) {
out <- attr(data, "metadata")
Comment thread
Moohan marked this conversation as resolved.
Outdated

if (is.null(out)) {
cli::cli_abort("Metadata could not be found, please check")
}

out
}

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

inform_metadata_access <- function() {
cli::cli_inform(c("", "i" = "SPD metadata has been attached to the data and can be accessed via `metadata()`"))
}

inform_metadata_version <- function(version) {
if (version != "latest") {
cli::cli_warn("Metadata is based on the latest version of the data and may not be relevant to the requested data")
Comment thread
Moohan marked this conversation as resolved.
Outdated
}
}
27 changes: 27 additions & 0 deletions man/metadata.Rd

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