Skip to content

Commit be48583

Browse files
authored
Merge branch 'main' into update_readme
2 parents 0c448a8 + ed894f4 commit be48583

7 files changed

Lines changed: 163 additions & 14 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export(get_hscp_locality)
44
export(get_simd_datazone)
55
export(get_simd_postcode)
66
export(get_spd)
7+
export(metadata)
78
importFrom(rlang,.data)
89
importFrom(tibble,tibble)

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
- Fixed a typo in the lookup path that meant the package wouldn't work on Windows (RStudio Desktop).
44
- Make the minimum required R version 4.1
55
- Overhaul the README; fixed out of date information, and added more detail and examples.
6+
- Metadata is now available! Currently, this is only for the Scottish Postcode Directory (`get_spd()`), but we will bring it to the other lookups soon, too. You can see the metadata by using `metadata()` on the lookup object, for example:
7+
```r
8+
spd <- get_spd()
9+
metadata(spd)
10+
```
611

712
# phslookups 0.1.0
813

R/get_spd.R

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
#' get_spd(version = "2023_2", col_select = c("pc7", "latitude", "longitude"))
2828
get_spd <- function(version = "latest", col_select = NULL) {
2929
spd_dir <- fs::path(
30-
get_lookups_dir(), "Geography",
30+
get_lookups_dir(),
31+
"Geography",
3132
"Scottish Postcode Directory"
3233
)
3334

35+
metadata_dir <- fs::path(spd_dir, "Metadata")
36+
3437
if (version == "latest") {
3538
spd_path <- find_latest_file(
3639
directory = spd_dir,
@@ -53,8 +56,25 @@ get_spd <- function(version = "latest", col_select = NULL) {
5356
)
5457
}
5558

56-
return(read_file(
59+
spd <- read_file(
5760
spd_path,
5861
col_select = {{ col_select }}
59-
))
62+
)
63+
64+
metadata_path <- fs::path(metadata_dir, "spd_metadata.csv")
65+
metadata_exists <- fs::file_exists(metadata_path)
66+
67+
spd <- set_metadata_ref(
68+
spd,
69+
type = "SPD",
70+
path = metadata_path,
71+
version = version,
72+
exists = metadata_exists
73+
)
74+
75+
if (metadata_exists) {
76+
inform_metadata_access(spd)
77+
}
78+
79+
return(spd)
6080
}

R/metadata.R

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#' Function to access metadata
2+
#'
3+
#' @param data Dataset imported using one of the [phslookups] functions,
4+
#' e.g. `get_spd()`.
5+
#'
6+
#' @returns
7+
#' Metadata `tibble` associated with dataset.
8+
#'
9+
#' @examples
10+
#' library(phslookups)
11+
#' \dontrun{
12+
#' spd <- get_spd()
13+
#' metadata(spd)
14+
#' }
15+
#' @export
16+
metadata <- function(data) {
17+
if (!inherits(data, "tbl_df")) {
18+
cli::cli_abort(
19+
"{.arg data} must must be a tibble loaded using {.pkg phslookups}."
20+
)
21+
}
22+
23+
# If already loaded, return immediately
24+
metadata <- attr(data, "metadata", exact = TRUE)
25+
if (!is.null(metadata)) {
26+
return(metadata)
27+
}
28+
29+
ref <- attr(data, "metadata_ref", exact = TRUE)
30+
31+
if (is.null(ref)) {
32+
cli::cli_abort("Metadata is not available for this data.")
33+
}
34+
35+
if (rlang::is_false(ref$exists)) {
36+
cli::cli_abort(c(
37+
"x" = "{ref$type} metadata not available.",
38+
"i" = "Expected at {.path {ref$path}}"
39+
))
40+
}
41+
42+
metadata <- read_file(
43+
ref$path,
44+
col_select = 1:2,
45+
col_names = c("variable", "description"),
46+
skip = 1,
47+
col_types = readr::cols_only(
48+
variable = readr::col_character(),
49+
description = readr::col_character()
50+
)
51+
)
52+
53+
inform_metadata_version(ref$version)
54+
55+
# Attach metadata to the object
56+
set_metadata(data, metadata)
57+
58+
metadata
59+
}
60+
61+
set_metadata_ref <- function(data, path, type, version, exists) {
62+
attr(data, "metadata_ref") <- list(
63+
path = path,
64+
type = type,
65+
version = version,
66+
exists = exists
67+
)
68+
data
69+
}
70+
71+
set_metadata <- function(data, metadata) {
72+
attr(data, "metadata") <- metadata
73+
data
74+
}
75+
76+
inform_metadata_access <- function(metadata) {
77+
cli::cli_inform(c(
78+
i = "Metadata is available and can be accessed using {.fun metadata}."
79+
))
80+
}
81+
82+
inform_metadata_version <- function(version) {
83+
if (version != "latest") {
84+
cli::cli_warn(
85+
"Metadata corresponds to the latest version of the data
86+
and may not exactly match the data currently loaded."
87+
)
88+
}
89+
}

R/read_file.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ read_file <- function(path, col_select = NULL, ...) {
4242
}
4343

4444
lookup <- switch(ext,
45-
rds = tibble::as_tibble(readr::read_rds(file = path)),
46-
csv = readr::read_csv(
45+
"rds" = tibble::as_tibble(readr::read_rds(file = path)),
46+
"csv" = readr::read_csv(
4747
file = path,
48-
guess_max = 50000L,
49-
...,
50-
show_col_types = FALSE
48+
guess_max = 50000,
49+
progress = FALSE,
50+
show_col_types = FALSE,
51+
lazy = .Platform$OS.type == "unix",
52+
...
5153
),
5254
parquet = tibble::as_tibble(arrow::read_parquet(
5355
file = path,

man/metadata.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-get_spd.R

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ skip_on_ci()
22

33
test_that("spd is returned", {
44
get_spd() |>
5-
expect_message()
5+
expect_message("Using the latest available version") |>
6+
expect_message("Metadata is available ")
67

78
spd <- suppressMessages(get_spd())
89

@@ -19,30 +20,36 @@ test_that("col selection works", {
1920
get_spd(col_select = "pc7"),
2021
"pc7"
2122
) |>
22-
expect_message()
23+
expect_message("Using the latest available version") |>
24+
expect_message("Metadata is available ")
2325
expect_named(
2426
get_spd(col_select = c("pc7", "pc8")),
2527
c("pc7", "pc8")
2628
) |>
27-
expect_message()
29+
expect_message("Using the latest available version") |>
30+
expect_message("Metadata is available ")
2831
})
2932

3033
test_that("col selection works with tidyselect", {
3134
expect_named(
3235
get_spd(col_select = c("pc7", dplyr::starts_with("hb")))
3336
) |>
34-
expect_message()
37+
expect_message("Using the latest available version") |>
38+
expect_message("Metadata is available ")
3539

3640
expect_named(
3741
get_spd(col_select = dplyr::matches("pc[78]")),
3842
c("pc7", "pc8")
3943
) |>
40-
expect_message()
44+
expect_message("Using the latest available version") |>
45+
expect_message("Metadata is available ")
4146
})
4247

4348

4449
test_that("reading from archive works", {
45-
expect_s3_class(get_spd(version = "2024_1"), "tbl_df")
50+
get_spd(version = "2024_1") |>
51+
expect_s3_class("tbl_df") |>
52+
expect_message("Metadata is available ")
4653
expect_error(get_spd(version = "2010_1", "SPD version .+? is NOT available"))
4754
expect_error(get_spd(version = "20243"), "Invalid version name:")
4855
})

0 commit comments

Comments
 (0)