Skip to content

Instantly share code, notes, and snippets.

View francisbarton's full-sized avatar
🌱
Sowing seeds for 2024

Fran Barton francisbarton

🌱
Sowing seeds for 2024
View GitHub Profile
@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 January 30, 2024 13:56
Pull full England IMD data from MHCLG (.gov.uk open data) API
imd_api_url <- paste0(
"https://services3.arcgis.com/ivmBBrHfQfDnDf8Q/arcgis/rest/services/",
"Indices_of_Multiple_Deprivation_(IMD)_2019/FeatureServer/0/query"
)
req <- httr2::request(imd_api_url) |>
httr2::req_url_query(f = "json")
# just get IDs only from API initially (no maxRecordCount for ID queries)
ids <- req |>
@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) |>
@francisbarton
francisbarton / settings.json
Last active May 11, 2023 17:52
My VS Code settings
{
"breadcrumbs.enabled": false,
"diffEditor.codeLens": true,
"editor.acceptSuggestionOnEnter": "off",
"editor.bracketPairColorization.enabled": false,
"editor.cursorBlinking": "phase",
"editor.cursorStyle": "line",
@francisbarton
francisbarton / setup.R
Last active April 25, 2024 00:11 — forked from tomjemmett/setup.R
Bootstrap a new R installation
# Apr 2024: stripped out several packages from the list that were loaded as dependencies anyway
pkgs <- c(
"usethis",
"tidyterra",
"devtools",
"tmap",
"janitor",
"afcolours",
"arrow",
"assertthat",
@francisbarton
francisbarton / keybindings.json
Last active April 29, 2023 22:48
My VSCode custom keyboard shortcuts
[
{
"key": "ctrl+d",
"command": "editor.action.deleteLines",
"when": "textInputFocus && !editorReadonly"
},
{
"key": "ctrl+shift+k",
"command": "-editor.action.deleteLines",
"when": "textInputFocus && !editorReadonly"
@francisbarton
francisbarton / .Rprofile
Last active February 22, 2024 13:04
My .Rprofile
options(
usethis.full_name = "Fran Barton",
usethis.protocol = "ssh",
usethis.description = list(
`Authors@R` = 'person(
"Fran", "Barton",
email = "francis.barton@nuh.nhs.uk",
role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-5650-1176")
)',
@francisbarton
francisbarton / rectangularise.R
Last active September 25, 2020 01:17
Rectangularise a set of list columns of different lengths
# originally written at
# https://community.rstudio.com/t/unlist-columns-with-lists-of-different-length/
# with grateful acknowledgement of those who worked it out before me :-)
## improved (well, single pipe) version, to summarise values in a df into a summary table
rectangularise <- function(df) {
df %>%
purrr::map(unique) %>%
purrr::map(sort) %>%
purrr::map(~ `length<-`(., max(lengths(map(df, ~ unique(.)))))) %>%
@francisbarton
francisbarton / r.snippets
Last active January 24, 2022 02:02
My RStudio snippets
# https://gist.github.com/francisbarton/a5907ec17154a59d5c9058d64b3012f5
# I use this pattern regularly in chatty packages
snippet inf
usethis::ui_info(
stringr::str_glue("")
)
# set up a testthat block
snippet testt