Skip to content

Commit

Permalink
Changed columns to search for the name
Browse files Browse the repository at this point in the history
  • Loading branch information
Lextuga007 committed Mar 20, 2024
1 parent c1e6b12 commit 3593740
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 46 deletions.
99 changes: 54 additions & 45 deletions R/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ get_data <- function(data,
column <- rlang::as_string(column)

# Check there is corresponding type data somewhere in data frame
# Use this to allow for other column names to be used in later code
is_postcode_check <- sum(is_postcode(as.vector(t(data))), na.rm = TRUE)
is_lsoa_check <- sum(is_lsoa(as.vector(t(data))), na.rm = TRUE)

if (column == "default" && is_postcode_check == 0 && is_lsoa_check > 0) {
column <- "lsoa11"
} else if (column == "default" && url_type == "postcode") {
if ("postcode" %in% colnames(data)) {
column <- "postcode"
} else if ("lsoa11" %in% colnames(data)) {
column <- "lsoa11"
} else {
column <- rlang::eval_tidy(rlang::quo(column))
}
Expand All @@ -62,7 +63,11 @@ get_data <- function(data,
fix_invalid = fix_invalid,
var = column # Not required but doesn't cause error
)
} else if (is.atomic(data) && is_postcode_check == 0 &&
}

## Generate specific text for the url

if (is.atomic(data) && is_postcode_check == 0 &&
is_lsoa_check > 0) {
text <- paste0(
"LSOA11CD IN ('",
Expand All @@ -80,66 +85,70 @@ get_data <- function(data,
collapse = "', '"
), "')"
)
} else {
data
}

if (is_postcode_check == 0 && is_lsoa_check > 0 | url_type == "imd") {
data_out <- imd_api(text = text,
req = req)
} else if (exists("data_transformed") && url_type == "imd") {
text <- paste0(
"LSOA11CD IN ('",
paste(data_transformed$lsoa_code,
collapse = "', '"
), "')"
)
}

# Because APIs only return data where a match has been made which results in
# non matched data being dropped this joins back to the original.
# Postcode information is passed through {NHSRpostcodetools} which handles
# this but IMD is handled here.

if (exists("data_transformed") && is.data.frame(data) &&
url_type == "postcode") {
data |>
dplyr::left_join(
data_transformed
)
} else if (exists("data_transformed") && is.atomic(data) &&
url_type == "postcode") {
tibble::as_tibble(data) |>
dplyr::left_join(
data_transformed,
dplyr::join_by(value == postcode)
) |>
dplyr::rename(postcode = value)
} else if (exists("data_transformed") && is.data.frame(data) &&
url_type == "url") {
data |>
if (exists("data_transformed") && is.data.frame(data)) {
pc_data <- data |>
dplyr::left_join(
data_transformed
) |>
dplyr::left_join(
data_out,
dplyr::join_by(lsoa_code == lsoa)
)
# } else if (exists("data_transformed") && is.atomic(data) &&
# url_type == "url") {
# tibble::as_tibble(data) |>
# dplyr::left_join(
# data_transformed,
# dplyr::join_by(value == postcode)
# ) |>
# dplyr::rename(postcode = value)
} else if (is.data.frame(data)) {
data |>
} else if (exists("data_transformed") && is.atomic(data)) {
pc_data <- data_transformed
}


## IMD data

if (is_postcode_check == 0 && is_lsoa_check > 0 &&
is.data.frame(data)) {
data_out <- imd_api(
text = text,
req = req
)

imd_data <- data |>
dplyr::left_join(
data_out,
dplyr::join_by({{ column }} == lsoa11cd)
)
} else if (is.atomic(data)) {
tibble::as_tibble(data) |>
} else if (is_postcode_check == 0 && is_lsoa_check > 0 && is.atomic(data)) {
data_out <- imd_api(
text = text,
req = req
)

imd_data <- tibble::as_tibble(data) |>
dplyr::left_join(
data_out,
dplyr::join_by(value == lsoa11cd)
) |>
dplyr::rename(lsoa11 = value)
}

## Final data

if (exists("pc_data") && url_type == "imd") {
data_out <- imd_api(
text = text,
req = req
)

data_out
} else if (exists("pc_data") && url_type == "postcode") {
pc_data
} else {
data
imd_data
}
}
10 changes: 9 additions & 1 deletion tests/testthat/test-get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

postcodes <- c("HD1 2UT", "HD1 2UU", "HD1 2UV")

imd <- c("E01011107", "E01011229", "E01000002")
imd <- c("E01011107", "E01011229", "E01002")

# # Taken from
# # www.gov.uk/government/statistics/english-indices-of-deprivation-2019
Expand Down Expand Up @@ -226,6 +226,14 @@ httptest2::with_mock_dir("imd", {

# vectors

testthat::expect_equal(
nrow(get_data(imd)), n_rows
)

testthat::expect_equal(
ncol(get_data(imd)), n_col_vector
)

testthat::expect_equal(
nrow(get_data(imd, url_type = "imd")), n_rows
)
Expand Down

0 comments on commit 3593740

Please sign in to comment.