Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
72 changes: 33 additions & 39 deletions R/get_github_file.R
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
#' Read in a file from the NHP Outputs app GitHub repo
#' Get the direct URL to a file in the NHP Outputs GitHub repo
#'
#' @param file string. The name of the file to read in
#' @param folder string. The folder where the file is located. `"inst"` by
#' default. Set to `""` to use the root folder of the repo.
#' @returns The file contents as a stream, to be passed to a reader function
#' @param ... Pass the name of the file in via `...`
#' @keywords internal
get_outputs_gh_file <- function(file, folder = "inst") {
httr2::request("https://api.github.com") |>
httr2::req_url_path_append("repos") |>
httr2::req_url_path_append("The-Strategy-Unit") |>
httr2::req_url_path_append("nhp_outputs") |>
httr2::req_url_path_append("contents") |>
httr2::req_url_path_append(folder) |>
httr2::req_url_path_append(file) |>
httr2::req_perform() |>
httr2::resp_check_status() |>
httr2::resp_body_json() |>
purrr::pluck("content") |>
base64enc::base64decode()
get_outputs_gh_url <- function(...) {
purrr::partial(get_su_gh_file, repo = "nhp_outputs", folder = "inst")(...)
}


#' Read in a file from the TPMAs GitHub repo
#' Get the direct URL to a file in the TPMAs GitHub repo
#'
#' @param file string. The name of the file to read in
#' @param folder string. The folder where the file is located. `"reference"` by
#' default. Set to `""` to use the root folder of the repo.
#' @returns The file contents as a stream, to be passed to a reader function
#' @inheritParams get_outputs_gh_url
#' @keywords internal
get_tpmas_gh_file <- function(file, folder = "reference") {
httr2::request("https://api.github.com") |>
httr2::req_url_path_append("repos") |>
httr2::req_url_path_append("The-Strategy-Unit") |>
httr2::req_url_path_append("TPMAs") |>
httr2::req_url_path_append("contents") |>
httr2::req_url_path_append(folder) |>
httr2::req_url_path_append(file) |>
httr2::req_perform() |>
httr2::resp_check_status() |>
httr2::resp_body_json() |>
purrr::pluck("content") |>
base64enc::base64decode()
get_tpmas_gh_url <- function(...) {
purrr::partial(get_su_gh_file, repo = "TPMAs", folder = "reference")(...)
}

#' Read the pods lookup table from a YAML file in the NHP Outputs repo
read_pods_lookup <- function(file = "golem-config.yml") {
yaml12::parse_yaml(readr::read_lines(get_outputs_gh_url(file)))
}
possibly_read_pods_lookup <- \(...) purrr::possibly(read_pods_lookup)(...)

#' Read the TPMAs lookup table from a CSV file in the TPMAs repo
read_tpmas_lookup <- function(file = "tpma-lookup.csv") {
readr::read_csv(get_tpmas_gh_url(file), col_types = "-ccccc---c")
}
possibly_read_tpmas_lookup <- \(...) purrr::possibly(read_tpmas_lookup)(...)

possibly_get_outputs_gh_file <- \(...) purrr::possibly(get_outputs_gh_file)(...)
possibly_get_tpmas_gh_file <- \(...) purrr::possibly(get_tpmas_gh_file)(...)

#' Read in a file from a Strategy Unit GitHub repo
#'
#' @param repo string. The name of the repository in which to find the file
#' @param folder string. The folder where the file is located. Set to `""` to
#' use the root folder of the repo.
#' @param file string. The name of the file to read in
#' @returns The URL to the raw file contents, to be passed to a reader function
#' @keywords internal
get_su_gh_file <- function(repo, folder, file) {
raw_base_url <- "https://raw.githubusercontent.com"
su <- "The-Strategy-Unit"
file.path(raw_base_url, su, repo, "refs", "heads", "main", folder, file)
}
36 changes: 10 additions & 26 deletions R/get_principal_pods.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
#' Prepare a lookup table with activity type labels and PoD labels for each PoD
#'
#' @param use_local logical. Whether to use a local file (internal to the
#' reskit package) or attempt to pull the lookup file from GitHub. Default is
#' `FALSE`, meaning it will use the GitHub route. If you want to always use a
#' local file (for example, for fully offline working), you can set the
#' `reskit.local.lookups` option to `TRUE` using
#' `options(reskit.local.lookups = TRUE)` or `withr::with_options()`
#' @returns A tibble
#' @export
get_detailed_pods <- function(use_local = FALSE) {
use_local <- getOption("reskit.local.lookups") %||% use_local
yaml_data <- system.file("pod_measures.yml", package = "reskit") |>
yaml12::read_yaml()
yaml_data_from_gh <- NULL
if (!use_local) {
yaml_raw <- possibly_get_outputs_gh_file("golem-config.yml")
if (!is.null(yaml_raw)) {
yaml_data_from_gh <- yaml12::parse_yaml(readr::read_lines(yaml_raw)) |>
purrr::pluck("default")
}
}
yaml_data <- yaml_data_from_gh %||% yaml_data
get_detailed_pods <- function() {
yaml_data <- possibly_read_pods_lookup()
msg <- "Unable to read POD lookup file from GitHub"
azkit::check_that(yaml_data, is_not_null, msg)
yaml_data |>
purrr::pluck("pod_measures") |>
purrr::pluck("default", "pod_measures") |>
purrr::map(list_to_tbl) |>
purrr::list_rbind() |>
dplyr::mutate(
dplyr::across("pod_label", forcats::fct_inorder),
dplyr::across("activity_type_label", \(x) sub("patients$", "patient", x)),
dplyr::across("activity_type_label", \(x) {
forcats::fct(x, levels = c("Inpatient", "Outpatient", "A&E"))
forcats::fct(x, levels = c("Inpatients", "Outpatients", "A&E"))
})
)
}
Expand All @@ -38,12 +22,12 @@ get_detailed_pods <- function(use_local = FALSE) {
#' Prepare a lookup table with activity type labels and PoD labels for each PoD
#'
#' This function condenses all A&E activity to a single category - compare
#' `get_detailed_pods()` which _keeps_ all A&E categories
#' [get_detailed_pods] which _keeps_ all A&E categories
#' @rdname get_detailed_pods
#' @returns A tibble
#' @export
get_principal_pods <- function(use_local = FALSE) {
get_detailed_pods(use_local) |>
get_principal_pods <- function() {
get_detailed_pods() |>
dplyr::filter(dplyr::if_any("activity_type_label", \(x) x != "A&E")) |>
dplyr::add_row(
activity_type_label = "A&E",
Expand All @@ -53,7 +37,7 @@ get_principal_pods <- function(use_local = FALSE) {
dplyr::mutate(
dplyr::across("pod_label", forcats::fct_inorder),
dplyr::across("activity_type_label", \(x) {
forcats::fct(x, levels = c("Inpatient", "Outpatient", "A&E"))
forcats::fct(x, levels = c("Inpatients", "Outpatients", "A&E"))
})
)
}
Expand Down
56 changes: 30 additions & 26 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,36 +177,37 @@ get_tretspef_lookup <- function() {

#' Prepare a lookup table for all current TPMAs
#'
#' @param use_local logical. Whether to use a local file (internal to the
#' reskit package) or attempt to pull the lookup file from GitHub. Default is
#' `FALSE`, meaning it will use the GitHub route. If you want to always use a
#' local file (for example, for fully offline working), you can set the
#' `reskit.local.lookups` option to `TRUE` using
#' `options(reskit.local.lookups = TRUE)` or `withr::with_options()`
#' @returns A 4-column tibble, with columns `activity_type`, `change_factor`,
#' `strategy` and `tpma_label`
#' @returns A 4-column tibble, with columns `strategy`, `activity_type`,
#' `change_factor` and `tpma_label`
#' @export
get_tpma_label_lookup <- function(use_local = FALSE) {
read_cols <- "cccc----c"
use_local <- getOption("reskit.local.lookups") %||% use_local
csv_data <- system.file("mitigator-lookup.csv", package = "reskit") |>
readr::read_csv(col_types = read_cols)
csv_data_from_gh <- NULL
if (!use_local) {
csv_data_raw <- possibly_get_tpmas_gh_file("mitigator-lookup.csv")
if (!is.null(csv_data_raw)) {
csv_data_from_gh <- readr::read_csv(csv_data_raw, col_types = read_cols)
}
}
csv_data <- csv_data_from_gh %||% csv_data
get_tpma_label_lookup <- function() {
csv_data <- possibly_read_tpmas_lookup()
msg <- "Unable to read TPMA lookup table from GitHub"
azkit::check_that(csv_data, is_not_null, msg)
csv_data |>
dplyr::filter(dplyr::if_any("active_to", is.na)) |>
dplyr::select(!"active_to") |>
dplyr::rename(
change_factor = "mitigator_type",
strategy = "mitigator_variable",
tpma_label = "mitigator_name"
)
dplyr::mutate(
tpma_label = glue::glue("{tpma_name} ({tpma_subtype})"),
dplyr::across("tpma_type", \(x) tolower(sub(" ", "_", x))),
dplyr::across("activity_type", convert_activity_type),
.keep = "unused"
) |>
dplyr::rename(change_factor = "tpma_type", strategy = "tpma_variable")
}


#' Converts "In|Outpatients", "A&E" strings to "ip", "op" and "aae"
#' @param x A character vector
#' @returns A character vector
#' @keywords internal
convert_activity_type <- function(x) {
dplyr::recode_values(
x,
"A&E" ~ "aae",
"Inpatients" ~ "ip",
"Outpatients" ~ "op"
)
}


Expand All @@ -216,3 +217,6 @@ get_tpma_label_lookup <- function(use_local = FALSE) {
#' @returns A character vector: all values of x that match the regex in rx
#' @keywords internal
gregv <- \(x, rx, g = parent.frame()) grepv(glue::glue_data(g, rx), x)


is_not_null <- \(x) !is.null(x)
18 changes: 18 additions & 0 deletions man/convert_activity_type.Rd

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

14 changes: 3 additions & 11 deletions man/get_detailed_pods.Rd

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

21 changes: 0 additions & 21 deletions man/get_outputs_gh_file.Rd

This file was deleted.

15 changes: 15 additions & 0 deletions man/get_outputs_gh_url.Rd

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

23 changes: 23 additions & 0 deletions man/get_su_gh_file.Rd

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

14 changes: 3 additions & 11 deletions man/get_tpma_label_lookup.Rd

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

21 changes: 0 additions & 21 deletions man/get_tpmas_gh_file.Rd

This file was deleted.

15 changes: 15 additions & 0 deletions man/get_tpmas_gh_url.Rd

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

Loading