Skip to content

Commit f1f3eaa

Browse files
committed
🦙 Misc changes cos of lintr
1 parent 618e8c8 commit f1f3eaa

File tree

4 files changed

+36
-38
lines changed

4 files changed

+36
-38
lines changed

‎R/get_postcode_data.R‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
#' @export
1919
get_postcode_data <- function(x, as_list = FALSE, include_codes = TRUE) {
2020
x <- unique(toupper(purrr::discard(x, is.na)))
21-
assertthat::assert_that(length(x) > 0, msg = "No postcodes have been found.")
21+
assertthat::assert_that(length(x) > 0L, msg = "No postcodes have been found.")
2222

2323
valid_index <- purrr::map_lgl(x, validate_code, .progress = "Checking codes")
2424
valid_codes <- x[valid_index]
2525
invalid_codes <- x[!valid_index]
2626

2727
if (length(invalid_codes) > 0L) {
28-
inv <- cli::cli_vec(invalid_codes, list(`vec-trunc` = 5))
28+
inv <- cli::cli_vec(invalid_codes, list(`vec-trunc` = 5L)) # nolint
2929
paste0(
3030
"{.fn get_postcode_data} found {length(invalid_codes)} invalid ",
3131
"postcode{?s}. Example{?s}: {.val {inv}}. You can use ",
@@ -64,7 +64,7 @@ get_postcode_data <- function(x, as_list = FALSE, include_codes = TRUE) {
6464
#'
6565
#' @examples
6666
#' tibble::tibble(
67-
#' place = paste0("place_", 1:3),
67+
#' place = paste0("place_", seq(3L)),
6868
#' postcode = c("NP22 3PS", "NP22 4PS", "NP22 5PS")
6969
#' ) |>
7070
#' postcode_data_join()
@@ -78,17 +78,15 @@ postcode_data_join <- function(tbl, .col = "postcode", include_codes = TRUE) {
7878
dplyr::left_join(tbl, api_data, by = dplyr::join_by({{ .col }} == "postcode"))
7979
}
8080

81-
82-
#' Unnest codes (wider): from a list-col to a column each
83-
#' @param tbl A data frame with ONS (etc.) codes data in a list-col
84-
#' @keywords internal
85-
unnest_codes <- function(tbl) {
86-
tbl |>
87-
dplyr::mutate(codes_names = names(.data[["codes"]])) |>
88-
dplyr::mutate(dplyr::across("codes", unlist)) |>
89-
tidyr::pivot_wider(
90-
names_from = "codes_names",
91-
names_glue = "{codes_names}_code",
92-
values_from = "codes"
93-
)
81+
tibblise_results_list <- function(results_list) {
82+
results_list |>
83+
purrr::map("result") |>
84+
purrr::list_flatten() |>
85+
purrr::map("result") |>
86+
purrr::map(purrr::compact) |>
87+
# If result contains nested codes then flatten these out and rename.
88+
# Hopefully this doesn't break things if codes are not included!
89+
purrr::map(\(x) purrr::list_flatten(x, name_spec = "{inner}_code")) |>
90+
purrr::map(tibble::as_tibble_row) |>
91+
purrr::list_rbind()
9492
}

‎man/postcode_data_join.Rd‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test-get_postcode_data.R‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ test_that("basic test of functions", {
33
codes <- c("NP22 3PS", "NP22 4PS", "NP22 5PS")
44
out1 <- get_postcode_data(codes) |>
55
expect_no_error()
6-
expect_equal(out1[["incode"]], c("3PS", "4PS", "5PS"))
7-
expect_equal(
6+
expect_identical(out1[["incode"]], c("3PS", "4PS", "5PS"))
7+
expect_identical(
88
# why test this? Just because
99
out1[["admin_ward"]],
1010
c("Tredegar", "Sirhowy", "Moriah and Pontlottyn")
@@ -13,26 +13,26 @@ test_that("basic test of functions", {
1313

1414
out2 <- get_postcode_data(codes, include_codes = FALSE) |>
1515
expect_no_error()
16-
expect_equal(out2[["incode"]], c("3PS", "4PS", "5PS"))
17-
expect_length(out2, 23L)
16+
expect_identical(out2[["incode"]], c("3PS", "4PS", "5PS"))
17+
expect_length(out2, 20L)
1818

19-
codes_df <- tibble::tibble(place = paste0("place_", 1:3), postcode = codes)
19+
codes_df <- tibble::tibble(code = paste0("code_", seq(3L)), postcode = codes)
2020
out3 <- postcode_data_join(codes_df) |>
2121
expect_no_error()
22-
codes_df <- tibble::tibble(place = paste0("place_", 1:3), codes = codes)
22+
codes_df <- tibble::tibble(code = paste0("code_", seq(3L)), codes = codes)
2323
out4 <- postcode_data_join(codes_df, .col = "codes") |>
2424
expect_no_error()
2525
out5 <- postcode_data_join(codes_df, .col = "codes", include_codes = FALSE) |>
2626
expect_no_error()
27-
expect_equal(nrow(out3), 3)
28-
expect_equal(nrow(out4), 3)
27+
expect_identical(nrow(out3), 3L)
28+
expect_identical(nrow(out4), 3L)
2929
expect_length(out4, 38L)
30-
expect_length(out5, 24L)
31-
expect_false(any(is.na(out1[["ccg"]])))
32-
expect_false(any(is.na(out2[["parish"]])))
33-
expect_false(any(is.na(out3[["nuts"]])))
34-
expect_false(any(is.na(out4[["longitude"]])))
35-
expect_false(any(is.na(out5[["lsoa"]])))
30+
expect_length(out5, 21L)
31+
expect_false(anyNA(out1[["ccg"]]))
32+
expect_false(anyNA(out2[["parish"]]))
33+
expect_false(anyNA(out3[["nuts"]]))
34+
expect_false(anyNA(out4[["longitude"]]))
35+
expect_false(anyNA(out5[["lsoa"]]))
3636

3737
expect_message(get_postcode_data("NP22 3PQ"))
3838
})

‎tests/testthat/test-requests.R‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test_that("core request function works", {
88
httr2::req_user_agent("github.com/nhs-r-community // httr2") |>
99
httr2::req_url_path_append(x)
1010
expect_s3_class(req2, "httr2_request")
11-
expect_equal(req2[["url"]], "https://api.postcodes.io/postcodes")
11+
expect_identical(req2[["url"]], "https://api.postcodes.io/postcodes")
1212
})
1313

1414

@@ -47,16 +47,16 @@ test_that("check terminated function works", {
4747
out3 <- check_terminated_possibly(pc3) |>
4848
expect_no_error()
4949
expect_true(rlang::is_list(out1))
50-
expect_length(out1, 5)
50+
expect_length(out1, 5L)
5151
expect_null(out2)
5252
expect_null(out3)
5353
})
5454

5555

5656
test_that("incomplete postcodes logic", {
5757
to_incomplete <- \(x) sub("([0-9])([[:alpha:]])[[:alpha:]]?$", "\\1\\2", x)
58-
expect_equal(to_incomplete("NP22 3PS"), "NP22 3P")
59-
expect_equal(to_incomplete("NP22 3P"), "NP22 3P")
58+
expect_identical(to_incomplete("NP22 3PS"), "NP22 3P")
59+
expect_identical(to_incomplete("NP22 3P"), "NP22 3P")
6060

6161
code <- "NP22 3P"
6262
out <- base_request() |>
@@ -65,10 +65,10 @@ test_that("incomplete postcodes logic", {
6565
pluck_result() |>
6666
expect_no_error()
6767
expect_true(rlang::is_list(out))
68-
set.seed(4567)
68+
set.seed(4567L)
6969
out2 <- autocomplete_possibly(code)
70-
expect_equal(out2, "NP22 3PD")
70+
expect_identical(out2, "NP22 3PD")
7171
out3 <- autocomplete_possibly("NP22 3M") |> # no postcodes exist
7272
expect_no_error()
73-
expect_null(out3)
73+
expect_true(is.na(out3))
7474
})

0 commit comments

Comments
 (0)