Skip to content

Instantly share code, notes, and snippets.

View francisbarton's full-sized avatar

Fran Barton francisbarton

View GitHub Profile
@francisbarton
francisbarton / acute_trusts_map.qmd
Last active September 10, 2025 18:45
Mapping NHS England Acute Trusts
---
title: NHS England Acute trust league table rankings map 2025
code-fold: true
knitr:
opts_chunk:
dev: "ragg_png"
fig-dpi: 144
fig-width: 8.2
fig-height: 11.6
---
@francisbarton
francisbarton / settings.json
Created August 28, 2025 17:48
My positron settings file
{
"workbench.startupEditor": "none",
// adds more lines to the file tab
"workbench.tree.renderIndentGuides": "always",
"workbench.colorCustomizations": {
"editorRuler.foreground": "#ff4081"
},
"editor.fontFamily": "Maple Mono NF, FiraCode Nerd Font, JuliaMono Nerd Font, Agave Nerd Font, monospace",
// "editor.fontLigatures": true,
@francisbarton
francisbarton / keybindings.json
Created August 28, 2025 17:35
My Positron keybindings file
[
// Ctrl + Up/Down to move lines
{
"key": "ctrl+up",
"command": "editor.action.moveLinesUpAction",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "alt+up",
"command": "-editor.action.moveLinesUpAction",
@francisbarton
francisbarton / tidy_coalesce.R
Created January 12, 2025 22:13
tidy_coalesce
#' A wrapper for dplyr::coalesce() that handles tidy column selections
#'
#' @param .data a data frame
#' @param ... (use tidyselect specification) a selection of cols to coalesce
#' @export
tidy_coalesce <- function(.data, ..., res = "result", .after = NULL) {
assert_that(inherits(.data, "data.frame"))
cnms <- colnames(dplyr::select(.data, ...))
nms <- rlang::data_syms(cnms)
aft <- ifnull(.after, dplyr::last(intersect(colnames(.data), cnms)))
#' Conveniently wrap a regular expr in glue::glue_data() and pass to `grepl()`
#'
#' Use {glue} expressions in grepl (and put the arguments the right way round)
#' https://glue.tidyverse.org/articles/wrappers.html
#'
#' @param x A character vector to check
#' @param rx A string that after processing by glue_data() will be used as a
#' regex pattern in `grepl()`
#' @param ... Arguments passed onto `grepl()`
#' @param g The parent frame of gregg, which must be passed through to `glue()`
@francisbarton
francisbarton / ew_holidays.R
Created August 21, 2024 13:52
Working days in EW calculation
#' Returns a vector of bank holiday dates in England and Wales
#'
#' Currently dates from 2018-2026 are available via gov.uk
#' https://www.gov.uk/bank-holidays
#'
#' @returns A vector of dates
#' @export
ew_holidays <- function() {
"https://www.gov.uk/bank-holidays/england-and-wales.ics" |>
readLines() |>
@francisbarton
francisbarton / yuckyuckyuck.md
Created February 29, 2024 19:39
Horrible dplyr data wrangling

"Please can anyone suggest some dplyr code that can turn my sample data_in into my sample data_out?"

data_in <- tibble::tribble(
  ~patient, ~delivery_date, ~obs_point, ~obs_date,
  "A", "2024-01-28", "antenatal", "2024-01-01",
  "A", "2024-01-28", "at_delivery",  "2024-01-28",
  "B", "2024-01-28", "at_delivery",  "2024-01-27",
  "B", "2024-01-28", "at_delivery",  "2024-01-28",
  "C", "2024-01-28", "at_delivery",  "2024-01-27",
@francisbarton
francisbarton / ggplot_datetime_reprex.R
Created January 30, 2024 23:45
ggplot2 labelling reprex
library(ggplot2)
library(lubridate, warn.conflicts = FALSE)
t <- lubridate::as_datetime("2021-01-01")
e1 <- lubridate::as_datetime("2021-12-31 23:59:59")
n1 <- as.numeric(lubridate::as.duration(e1 - t))
s1 <- sample(seq.int(n1), 300L)
@francisbarton
francisbarton / imd_api.R
Last active June 13, 2025 14:31
Pull England 2019 IMD data from ONS API
# https://gist.github.com/francisbarton/5d9d177978a2279cf225abdc772adef9
ons_api_base <- "https://services1.arcgis.com/ESMARspQHYMw9BZ9/arcgis/rest"
imd_dataset <- "Index_of_Multiple_Deprivation_Dec_2019_Lookup_in_England_2022"
req <- httr2::request(ons_api_base) |>
httr2::req_url_path_append("services") |>
httr2::req_url_path_append(imd_dataset) |>
httr2::req_url_path_append("FeatureServer/0/query") |>
httr2::req_url_query(f = "json")
@francisbarton
francisbarton / gp_practice_sf_points.R
Created November 6, 2023 00:52
Find longitude and latitude for GP practices in England
# https://digital.nhs.uk/data-and-information/data-collections-and-data-sets/data-collections/gp-data-available-through-sdcs
zip_url <- "https://digital.nhs.uk/binaries/content/assets/website-assets/data-and-information/data-collections/general-practice-data-collections/catchment-area-8.zip"
dl <- tempfile("catchment_files", tempdir(), ".zip")
download.file(zip_url, dl, mode = "wb")
out_dir <- paste0(tempdir(), "\\catchment_files")
if (!dir.exists(out_dir)) dir.create(out_dir)
utils::unzip(dl, junkpaths = TRUE, exdir = out_dir)
gp_data <- dir(out_dir, full.names = TRUE) |>