From 3a9c87c4fca8c5f069717dd9e4158bc1df81d99f Mon Sep 17 00:00:00 2001 From: nicolc11 Date: Thu, 31 Jul 2025 11:28:24 +0000 Subject: [PATCH 01/26] Added spd metadata. --- R/get_spd.R | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/R/get_spd.R b/R/get_spd.R index 8be2c04..e0f07cc 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -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, @@ -53,8 +55,21 @@ get_spd <- function(version = "latest", col_select = NULL) { ) } - return(read_file( + metadata <- readr::read_csv(fs::path(metadata_dir, "spd_metadata.csv")) %>% + dplyr::select(1:2) %>% + stats::setNames(c("variable", "description")) + + cli::cli_inform(c("", "i" = "SPD metadata has been attached to the data and can be accessed via {.run attr(, 'metadata')}")) + + + cli::cat_line("\n--- Metadata ---\n", col = "blue") + cli::cat_print(metadata) + cli::cat_line("") + + spd <- read_file( spd_path, col_select = {{ col_select }} - )) + ) + attr(spd, "metadata") <- metadata + return(spd) } From ca376b9a4c01df81fcac03ca824dccad53e2f6c1 Mon Sep 17 00:00:00 2001 From: nicolc11 Date: Fri, 29 Aug 2025 10:25:54 +0000 Subject: [PATCH 02/26] Added metadata helper. --- NAMESPACE | 1 + R/metadata.R | 26 ++++++++++++++++++++++++++ man/metadata.Rd | 27 +++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 R/metadata.R create mode 100644 man/metadata.Rd diff --git a/NAMESPACE b/NAMESPACE index a0e96f4..ea81929 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/metadata.R b/R/metadata.R new file mode 100644 index 0000000..6adea6a --- /dev/null +++ b/R/metadata.R @@ -0,0 +1,26 @@ +#' 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") + + if (is.null(out)){ + cli::cli_abort("Metadata could not be found, please check") + } + + out +} diff --git a/man/metadata.Rd b/man/metadata.Rd new file mode 100644 index 0000000..f65a7f4 --- /dev/null +++ b/man/metadata.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/metadata.R +\name{metadata} +\alias{metadata} +\title{Function to access metadata} +\usage{ +metadata(data) +} +\arguments{ +\item{data}{Dataset imported via e.g. \code{get_spd()}.} +} +\value{ +Metadata \code{tibble} associated with dataset. +} +\description{ +Function to access metadata +} +\examples{ +library(phslookups) + +\dontrun{ +spd <- get_spd() + +metadata(spd) +} + +} From 6abe8b3445fdab9cb0373e892d3c2a18fa4672ef Mon Sep 17 00:00:00 2001 From: nicolc11 Date: Fri, 29 Aug 2025 10:30:14 +0000 Subject: [PATCH 03/26] New helpers and better messaging. --- R/get_spd.R | 5 ++--- R/metadata.R | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/R/get_spd.R b/R/get_spd.R index e0f07cc..bf6d226 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -59,8 +59,7 @@ get_spd <- function(version = "latest", col_select = NULL) { dplyr::select(1:2) %>% stats::setNames(c("variable", "description")) - cli::cli_inform(c("", "i" = "SPD metadata has been attached to the data and can be accessed via {.run attr(, 'metadata')}")) - + inform_metadata_access() cli::cat_line("\n--- Metadata ---\n", col = "blue") cli::cat_print(metadata) @@ -70,6 +69,6 @@ get_spd <- function(version = "latest", col_select = NULL) { spd_path, col_select = {{ col_select }} ) - attr(spd, "metadata") <- metadata + spd <- set_metadata(spd, metadata) return(spd) } diff --git a/R/metadata.R b/R/metadata.R index 6adea6a..2c28bdf 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -24,3 +24,12 @@ metadata <- function(data){ 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()`")) +} From 71a3c9a2a4c074fc170ffc26c1314ab5271d4296 Mon Sep 17 00:00:00 2001 From: nicolc11 Date: Fri, 29 Aug 2025 10:34:23 +0000 Subject: [PATCH 04/26] Added helpful messages. --- R/get_spd.R | 2 ++ R/metadata.R | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/R/get_spd.R b/R/get_spd.R index bf6d226..fd3506b 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -61,6 +61,8 @@ get_spd <- function(version = "latest", col_select = NULL) { inform_metadata_access() + inform_metadata_version(version) + cli::cat_line("\n--- Metadata ---\n", col = "blue") cli::cat_print(metadata) cli::cat_line("") diff --git a/R/metadata.R b/R/metadata.R index 2c28bdf..e4c7d0c 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -33,3 +33,9 @@ set_metadata <- function(data, metadata){ 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") + } +} From 7cdc9025df4fa053a5be5aca1a083c95af9127f9 Mon Sep 17 00:00:00 2001 From: Nic-Chr <74770457+Nic-Chr@users.noreply.github.com> Date: Fri, 29 Aug 2025 10:39:15 +0000 Subject: [PATCH 05/26] Style code (GHA) --- R/metadata.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/metadata.R b/R/metadata.R index e4c7d0c..462e472 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -15,27 +15,27 @@ #' } #' #' @export -metadata <- function(data){ +metadata <- function(data) { out <- attr(data, "metadata") - if (is.null(out)){ + if (is.null(out)) { cli::cli_abort("Metadata could not be found, please check") } out } -set_metadata <- function(data, metadata){ +set_metadata <- function(data, metadata) { attr(data, "metadata") <- metadata data } -inform_metadata_access <- function(){ +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"){ +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") } } From 88472464afe8d8b9788aae0d820ef2c6358424b9 Mon Sep 17 00:00:00 2001 From: nicolc11 Date: Mon, 9 Mar 2026 13:22:19 +0000 Subject: [PATCH 06/26] Rename cols with readr --- R/get_spd.R | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/R/get_spd.R b/R/get_spd.R index fd3506b..758b357 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -55,9 +55,22 @@ get_spd <- function(version = "latest", col_select = NULL) { ) } - metadata <- readr::read_csv(fs::path(metadata_dir, "spd_metadata.csv")) %>% - dplyr::select(1:2) %>% - stats::setNames(c("variable", "description")) + spd <- read_file( + spd_path, + col_select = {{ col_select }} + ) + + metadata <- readr::read_csv( + file = fs::path(metadata_dir, "spd_metadata.csv"), + col_names = c("variable", "description"), + skip = 1, + col_types = readr::cols_only( + variable = readr::col_character(), + description = readr::col_character() + ), + show_col_types = FALSE, + lazy = FALSE + ) inform_metadata_access() @@ -67,10 +80,7 @@ get_spd <- function(version = "latest", col_select = NULL) { cli::cat_print(metadata) cli::cat_line("") - spd <- read_file( - spd_path, - col_select = {{ col_select }} - ) spd <- set_metadata(spd, metadata) + return(spd) } From 193c59a7a656812ab7b6078a74af107278a8b7dd Mon Sep 17 00:00:00 2001 From: nicolc11 Date: Mon, 9 Mar 2026 13:22:36 +0000 Subject: [PATCH 07/26] Use base print --- R/get_spd.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/get_spd.R b/R/get_spd.R index 758b357..ed309ff 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -77,7 +77,7 @@ get_spd <- function(version = "latest", col_select = NULL) { inform_metadata_version(version) cli::cat_line("\n--- Metadata ---\n", col = "blue") - cli::cat_print(metadata) + print(metadata, n = 5) cli::cat_line("") spd <- set_metadata(spd, metadata) From e71f447e3db8090ef889560ab282a6af7b0212cf Mon Sep 17 00:00:00 2001 From: nicolc11 Date: Mon, 9 Mar 2026 13:28:52 +0000 Subject: [PATCH 08/26] Use read_file and added file check warning --- R/get_spd.R | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/R/get_spd.R b/R/get_spd.R index ed309ff..3833c34 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -60,27 +60,33 @@ get_spd <- function(version = "latest", col_select = NULL) { col_select = {{ col_select }} ) - metadata <- readr::read_csv( - file = fs::path(metadata_dir, "spd_metadata.csv"), - col_names = c("variable", "description"), - skip = 1, - col_types = readr::cols_only( - variable = readr::col_character(), - description = readr::col_character() - ), - show_col_types = FALSE, - lazy = FALSE - ) + metadata_path <- fs::path(metadata_dir, "spd_metadata.csv") + + if (fs::file_exists(metadata_path)){ + metadata <- read_file( + metadata_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() + ), + lazy = FALSE + ) - inform_metadata_access() + inform_metadata_access() - inform_metadata_version(version) + inform_metadata_version(version) - cli::cat_line("\n--- Metadata ---\n", col = "blue") - print(metadata, n = 5) - cli::cat_line("") + cli::cat_line("\n--- Metadata ---\n", col = "blue") + print(metadata, n = 5) + cli::cat_line("") - spd <- set_metadata(spd, metadata) + spd <- set_metadata(spd, metadata) + } else { + cli::cli_warn("SPD metadata file is not available and has not been attached") + } return(spd) } From e7f4b34d235ad6fb9fee163e6e94b11460d96170 Mon Sep 17 00:00:00 2001 From: Nic-Chr <74770457+Nic-Chr@users.noreply.github.com> Date: Mon, 9 Mar 2026 13:30:37 +0000 Subject: [PATCH 09/26] Style code (GHA) --- R/get_spd.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/get_spd.R b/R/get_spd.R index 3833c34..b9bde55 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -62,7 +62,7 @@ get_spd <- function(version = "latest", col_select = NULL) { metadata_path <- fs::path(metadata_dir, "spd_metadata.csv") - if (fs::file_exists(metadata_path)){ + if (fs::file_exists(metadata_path)) { metadata <- read_file( metadata_path, col_select = 1:2, @@ -85,7 +85,7 @@ get_spd <- function(version = "latest", col_select = NULL) { spd <- set_metadata(spd, metadata) } else { - cli::cli_warn("SPD metadata file is not available and has not been attached") + cli::cli_warn("SPD metadata file is not available and has not been attached") } return(spd) From cfd03354c25790c2f1934da9d964ad4fe9350536 Mon Sep 17 00:00:00 2001 From: James Hayes Date: Fri, 1 May 2026 11:25:25 +0000 Subject: [PATCH 10/26] Refactor metadata messaging into the metadata fucntions --- R/get_spd.R | 15 +++++++-------- R/metadata.R | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/R/get_spd.R b/R/get_spd.R index f5f6055..8b1b5b2 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -27,11 +27,12 @@ #' 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(dir, "Metadata") + metadata_dir <- fs::path(spd_dir, "Metadata") if (version == "latest") { spd_path <- find_latest_file( @@ -75,17 +76,15 @@ get_spd <- function(version = "latest", col_select = NULL) { lazy = FALSE ) - inform_metadata_access() + inform_metadata_access(metadata) inform_metadata_version(version) - cli::cat_line("\n--- Metadata ---\n", col = "blue") - print(metadata, n = 5) - cli::cat_line("") - spd <- set_metadata(spd, metadata) } else { - cli::cli_warn("SPD metadata file is not available and has not been attached") + cli::cli_warn( + "SPD metadata file is not available and has not been attached" + ) } return(spd) diff --git a/R/metadata.R b/R/metadata.R index 462e472..c138baf 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -30,12 +30,24 @@ set_metadata <- function(data, 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_access <- function(metadata) { + meta_print <- utils::capture.output( + print(metadata, n = 5) + ) + + cli::cli_inform(c( + i = "Metadata has been attached and can be accessed using {.fun metadata}.", + cli::col_blue("------ Metadata -----"), + meta_print, + cli::col_blue("------") + )) } 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") + cli::cli_warn( + "Metadata is correct for the latest version of the data, + and may not be relevant to the version you have loaded." + ) } } From 2a1e6927919192192f19afafaf792d48cb13998d Mon Sep 17 00:00:00 2001 From: James Hayes Date: Fri, 1 May 2026 11:25:56 +0000 Subject: [PATCH 11/26] Add an error if `metadata` is used on a non-tibble --- R/metadata.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/metadata.R b/R/metadata.R index c138baf..4318ca6 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -16,6 +16,12 @@ #' #' @export metadata <- function(data) { + if (!inherits(data, "tbl_df")) { + cli::cli_abort( + "{.arg data} must must be a tibble loaded using {.pkg phslookups}." + ) + } + out <- attr(data, "metadata") if (is.null(out)) { From 055cf4ecb38d23b836d15330c58d01a9438d0c12 Mon Sep 17 00:00:00 2001 From: James Hayes Date: Fri, 1 May 2026 11:31:53 +0000 Subject: [PATCH 12/26] Fix and improve tests to expect the messaging --- tests/testthat/test-get_spd.R | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-get_spd.R b/tests/testthat/test-get_spd.R index 2e186ef..98ef40f 100755 --- a/tests/testthat/test-get_spd.R +++ b/tests/testthat/test-get_spd.R @@ -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 has been attached ") spd <- suppressMessages(get_spd()) @@ -19,30 +20,37 @@ test_that("col selection works", { get_spd(col_select = "pc7"), "pc7" ) |> - expect_message() + expect_message("Using the latest available version") |> + expect_message("Metadata has been attached ") expect_named( get_spd(col_select = c("pc7", "pc8")), c("pc7", "pc8") ) |> - expect_message() + expect_message("Using the latest available version") |> + expect_message("Metadata has been attached ") }) 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 has been attached ") 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 has been attached ") }) 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_warning("Metadata is correct for the latest version ") |> + expect_message("Metadata has been attached ") expect_error(get_spd(version = "2010_1", "SPD version .+? is NOT available")) expect_error(get_spd(version = "20243"), "Invalid version name:") }) From 352ff49545910697bbbccaa06d3de42924a9cc1a Mon Sep 17 00:00:00 2001 From: James Hayes Date: Fri, 1 May 2026 11:35:17 +0000 Subject: [PATCH 13/26] Improve CSV lazy read and suppress progress messages The progress message was being printed to the console as `0s` or `1s`, not required and potentially confusing. CSV lazy reading is fine if the user is on UNIX (linux/mac), it's only not good when on Windows, so limit it to that. This should make reading CSV files (metadata + some of the older lookups) faster on Posit Workbench. --- R/get_spd.R | 3 +-- R/read_file.R | 15 +++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/R/get_spd.R b/R/get_spd.R index 8b1b5b2..3dd0c5a 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -72,8 +72,7 @@ get_spd <- function(version = "latest", col_select = NULL) { col_types = readr::cols_only( variable = readr::col_character(), description = readr::col_character() - ), - lazy = FALSE + ) ) inform_metadata_access(metadata) diff --git a/R/read_file.R b/R/read_file.R index 6f33962..1151529 100755 --- a/R/read_file.R +++ b/R/read_file.R @@ -41,13 +41,16 @@ read_file <- function(path, col_select = NULL, ...) { ) } - lookup <- switch(ext, - rds = tibble::as_tibble(readr::read_rds(file = path)), - csv = readr::read_csv( + lookup <- switch( + ext, + "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, From 77ef99c0836f4acf1dbdf2e9ab5d06df5be45e38 Mon Sep 17 00:00:00 2001 From: Moohan <5982260+Moohan@users.noreply.github.com> Date: Fri, 1 May 2026 11:40:33 +0000 Subject: [PATCH 14/26] Style code (GHA) --- R/read_file.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/read_file.R b/R/read_file.R index 1151529..475bed5 100755 --- a/R/read_file.R +++ b/R/read_file.R @@ -41,8 +41,7 @@ read_file <- function(path, col_select = NULL, ...) { ) } - lookup <- switch( - ext, + lookup <- switch(ext, "rds" = tibble::as_tibble(readr::read_rds(file = path)), "csv" = readr::read_csv( file = path, From 808a8ba1efec04fe02b2c746a859dbe8e2d20df6 Mon Sep 17 00:00:00 2001 From: James Hayes Date: Fri, 1 May 2026 12:26:14 +0000 Subject: [PATCH 15/26] Refactor to make loading the metadata be on demand This offloads metadata loading to the first call of `metadata()`. When the lookup is loaded, it sets some attributes, such as where the metadata file is and whether it exists, but it's not loaded initially. Overall, I think this is a better approach. The only downside is I couldn't figure out a good way to partially print the top few rows of the metadata, as that (obviously) involves reading it. --- R/get_spd.R | 30 +++++++------------- R/metadata.R | 52 +++++++++++++++++++++++++++-------- tests/testthat/test-get_spd.R | 13 ++++----- 3 files changed, 56 insertions(+), 39 deletions(-) diff --git a/R/get_spd.R b/R/get_spd.R index 3dd0c5a..de0b428 100755 --- a/R/get_spd.R +++ b/R/get_spd.R @@ -62,28 +62,18 @@ get_spd <- function(version = "latest", col_select = NULL) { ) metadata_path <- fs::path(metadata_dir, "spd_metadata.csv") + metadata_exists <- fs::file_exists(metadata_path) - if (fs::file_exists(metadata_path)) { - metadata <- read_file( - metadata_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_access(metadata) - - inform_metadata_version(version) + spd <- set_metadata_ref( + spd, + type = "SPD", + path = metadata_path, + version = version, + exists = metadata_exists + ) - spd <- set_metadata(spd, metadata) - } else { - cli::cli_warn( - "SPD metadata file is not available and has not been attached" - ) + if (metadata_exists) { + inform_metadata_access(spd) } return(spd) diff --git a/R/metadata.R b/R/metadata.R index 4318ca6..c77128c 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -22,13 +22,48 @@ metadata <- function(data) { ) } - out <- attr(data, "metadata") + # 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(out)) { - cli::cli_abort("Metadata could not be found, please check") + if (rlang::is_false(ref$exists)) { + cli::cli_abort(c( + "x" = "{ref$type} metadata not available.", + "i" = "Expected at {.path {ref$path}}" + )) } - out + 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) { @@ -37,15 +72,8 @@ set_metadata <- function(data, metadata) { } inform_metadata_access <- function(metadata) { - meta_print <- utils::capture.output( - print(metadata, n = 5) - ) - cli::cli_inform(c( - i = "Metadata has been attached and can be accessed using {.fun metadata}.", - cli::col_blue("------ Metadata -----"), - meta_print, - cli::col_blue("------") + i = "Metadata is available and can be accessed using {.fun metadata}." )) } diff --git a/tests/testthat/test-get_spd.R b/tests/testthat/test-get_spd.R index 98ef40f..52e7cb2 100755 --- a/tests/testthat/test-get_spd.R +++ b/tests/testthat/test-get_spd.R @@ -3,7 +3,7 @@ skip_on_ci() test_that("spd is returned", { get_spd() |> expect_message("Using the latest available version") |> - expect_message("Metadata has been attached ") + expect_message("Metadata is available ") spd <- suppressMessages(get_spd()) @@ -21,13 +21,13 @@ test_that("col selection works", { "pc7" ) |> expect_message("Using the latest available version") |> - expect_message("Metadata has been attached ") + expect_message("Metadata is available ") expect_named( get_spd(col_select = c("pc7", "pc8")), c("pc7", "pc8") ) |> expect_message("Using the latest available version") |> - expect_message("Metadata has been attached ") + expect_message("Metadata is available ") }) test_that("col selection works with tidyselect", { @@ -35,22 +35,21 @@ test_that("col selection works with tidyselect", { get_spd(col_select = c("pc7", dplyr::starts_with("hb"))) ) |> expect_message("Using the latest available version") |> - expect_message("Metadata has been attached ") + expect_message("Metadata is available ") expect_named( get_spd(col_select = dplyr::matches("pc[78]")), c("pc7", "pc8") ) |> expect_message("Using the latest available version") |> - expect_message("Metadata has been attached ") + expect_message("Metadata is available ") }) test_that("reading from archive works", { get_spd(version = "2024_1") |> expect_s3_class("tbl_df") |> - expect_warning("Metadata is correct for the latest version ") |> - expect_message("Metadata has been attached ") + 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:") }) From 764cb2ed82b45042374afb4355165134b07a429e Mon Sep 17 00:00:00 2001 From: Moohan <5982260+Moohan@users.noreply.github.com> Date: Mon, 4 May 2026 13:14:20 +0000 Subject: [PATCH 16/26] Document package (GHA) --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index ea2b5e7..dcaa3aa 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,4 +30,4 @@ Config/testthat/edition: 3 Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.3 +Config/roxygen2/version: 8.0.0 From c38bae4b0a97b6a1acefae3822e4a06c26148cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?James=20Hayes=20=28n=C3=A9=20McMahon=29?= Date: Wed, 6 May 2026 15:27:06 +0100 Subject: [PATCH 17/26] Add error handling for missing metadata reference --- R/metadata.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/metadata.R b/R/metadata.R index c77128c..feb8714 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -30,6 +30,10 @@ metadata <- function(data) { 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.", From d6bd04fa4334780de4b1cd0fc9d42f1a529f8974 Mon Sep 17 00:00:00 2001 From: Beata Nowok Date: Wed, 6 May 2026 16:40:48 +0100 Subject: [PATCH 18/26] Update warning message for metadata version --- R/metadata.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/metadata.R b/R/metadata.R index feb8714..0cedc7c 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -84,8 +84,8 @@ inform_metadata_access <- function(metadata) { inform_metadata_version <- function(version) { if (version != "latest") { cli::cli_warn( - "Metadata is correct for the latest version of the data, - and may not be relevant to the version you have loaded." + "Metadata corresponds to the latest version of the data + and may not exactly match the data currently loaded." ) } } From 9a279d9dbf19114922d930a11593250a20e0fc32 Mon Sep 17 00:00:00 2001 From: Beata Nowok Date: Wed, 6 May 2026 16:45:48 +0100 Subject: [PATCH 19/26] Clarify parameter description in metadata function Updated parameter documentation for clarity. --- R/metadata.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/metadata.R b/R/metadata.R index 0cedc7c..45a55b5 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -1,16 +1,15 @@ #' Function to access metadata #' -#' @param data Dataset imported via e.g. `get_spd()`. +#' @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) #' } #' From 2aaa7c01549220342e0bcae65a9a6f3639be1a56 Mon Sep 17 00:00:00 2001 From: Beata Nowok Date: Wed, 6 May 2026 16:51:47 +0100 Subject: [PATCH 20/26] Fix roxygen2 configuration in DESCRIPTION file --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index dcaa3aa..bd9734f 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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) -Config/roxygen2/version: 8.0.0 From 274655975207a9ea83eb12f096e1fbe6c018fa68 Mon Sep 17 00:00:00 2001 From: bnowok <28870946+bnowok@users.noreply.github.com> Date: Wed, 6 May 2026 15:53:20 +0000 Subject: [PATCH 21/26] Style code (GHA) --- R/metadata.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/metadata.R b/R/metadata.R index 45a55b5..d440039 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -83,7 +83,7 @@ inform_metadata_access <- function(metadata) { inform_metadata_version <- function(version) { if (version != "latest") { cli::cli_warn( - "Metadata corresponds to the latest version of the data + "Metadata corresponds to the latest version of the data and may not exactly match the data currently loaded." ) } From 857b276d7772ddca97c88e11b701cf4a29a3a955 Mon Sep 17 00:00:00 2001 From: bnowok <28870946+bnowok@users.noreply.github.com> Date: Wed, 6 May 2026 15:53:24 +0000 Subject: [PATCH 22/26] Document package (GHA) --- man/metadata.Rd | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/man/metadata.Rd b/man/metadata.Rd index f65a7f4..57dbb0b 100644 --- a/man/metadata.Rd +++ b/man/metadata.Rd @@ -7,7 +7,8 @@ metadata(data) } \arguments{ -\item{data}{Dataset imported via e.g. \code{get_spd()}.} +\item{data}{Dataset imported using one of the \link{phslookups} functions, +e.g. \code{get_spd()}.} } \value{ Metadata \code{tibble} associated with dataset. @@ -17,10 +18,8 @@ Function to access metadata } \examples{ library(phslookups) - \dontrun{ spd <- get_spd() - metadata(spd) } From a833657cee1d4b9fb7f47b313c2d84bc589e4fac Mon Sep 17 00:00:00 2001 From: Beata Nowok Date: Wed, 6 May 2026 17:12:21 +0100 Subject: [PATCH 23/26] Fix comment formatting in metadata.R --- R/metadata.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/metadata.R b/R/metadata.R index d440039..f008ae1 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -10,8 +10,7 @@ #' library(phslookups) #' \dontrun{ #' spd <- get_spd() -#' metadata(spd) -#' } +#' metadata(spd)} #' #' @export metadata <- function(data) { From 28eb00009847323a92372cdf651ad0fba14020ee Mon Sep 17 00:00:00 2001 From: bnowok <28870946+bnowok@users.noreply.github.com> Date: Wed, 6 May 2026 16:13:52 +0000 Subject: [PATCH 24/26] Style code (GHA) --- R/metadata.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/metadata.R b/R/metadata.R index f008ae1..d440039 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -10,7 +10,8 @@ #' library(phslookups) #' \dontrun{ #' spd <- get_spd() -#' metadata(spd)} +#' metadata(spd) +#' } #' #' @export metadata <- function(data) { From d4586d5ebce8ff31bb3f9340113d5e7cef97a512 Mon Sep 17 00:00:00 2001 From: Beata Nowok Date: Wed, 6 May 2026 17:14:39 +0100 Subject: [PATCH 25/26] Clean up comments in metadata function --- R/metadata.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/metadata.R b/R/metadata.R index d440039..dc7c282 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -12,7 +12,6 @@ #' spd <- get_spd() #' metadata(spd) #' } -#' #' @export metadata <- function(data) { if (!inherits(data, "tbl_df")) { From c528474e4dfe504a07521a520413b24d481713fa Mon Sep 17 00:00:00 2001 From: bnowok <28870946+bnowok@users.noreply.github.com> Date: Wed, 6 May 2026 16:16:00 +0000 Subject: [PATCH 26/26] Document package (GHA) --- man/metadata.Rd | 1 - 1 file changed, 1 deletion(-) diff --git a/man/metadata.Rd b/man/metadata.Rd index 57dbb0b..f8735e8 100644 --- a/man/metadata.Rd +++ b/man/metadata.Rd @@ -22,5 +22,4 @@ library(phslookups) spd <- get_spd() metadata(spd) } - }