Skip to content

Instantly share code, notes, and snippets.

View francisbarton's full-sized avatar

Fran Barton francisbarton

View GitHub Profile
@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 = "[email protected]",
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
@francisbarton
francisbarton / glue_strings_example.R
Last active September 16, 2020 13:10
Example of using glue strings to create a col name in dplyr
options(conflicts.policy = list(warn.conflicts = FALSE))
library(dplyr) # A Grammar of Data Manipulation
summarise_groups <- function(df, grouping_var, column_name){
df %>%
group_by({{grouping_var}}) %>%
summarise("{{column_name}}_mean" := mean({{column_name}}, na.rm = TRUE))
}
dplyr::storms %>%
@francisbarton
francisbarton / get_postcode_metadata.R
Last active September 10, 2020 18:04
Get postcode metadata from postcodes.io
# Get postcode metadata from postcodes.io
get_postcode_metadata <- function(postcodes) {
endpoint <- "https://api.postcodes.io/postcodes"
body <- list(postcodes = postcodes)
out <- httr::POST(url = endpoint, body = body, encode = "json")
# thanks to https://github.com/ropensci/PostcodesioR/blob/master/R/postcode_lookup.R for setting a good example with httr
httr::warn_for_status(out)
@francisbarton
francisbarton / get_doogal_data.R
Created September 10, 2020 14:58
Use the doogal.co.uk API to get data about a postcode
# Use the doogal.co.uk API to get data about a postcode
# Doesn't accept a vector of codes all at once, so use with purrr::map_dfr()
# along a vector to combine results into a data frame
get_doogal_data <- function(postcode) {
data_names <- c(
"postcode",
"latitude",
"longitude",
@francisbarton
francisbarton / gist:89da88d3a199df701b7203985970e86b
Last active July 13, 2020 21:25
using if_else, mutate, map and reduce to create a data frame of columns meeting conditions
``` r
library(dplyr, warn.conflicts = FALSE)
library(purrr)
library(rlang, warn.conflicts = FALSE)
library(stringr)
filenames <- c("coronavirus_cases_202007061134.csv", "coronavirus_cases_202007071134.csv", "coronavirus_cases_202007081134.csv")
cases <- c(1000, 1500, 2000)
# couple of functions