Skip to content

Instantly share code, notes, and snippets.


Matt Dray matt-dray

View GitHub Profile
matt-dray / countdown.R
Created Nov 29, 2021
A countdown timer in the R console (beware: it clears the console first)
View countdown.R
# See Colin Fay's tweet
countdown <- function(s) {
cat("\014") # clear console
for (i in rev(seq(s))) {
cat("Countdown:", i, "s\n")
matt-dray / runcharter-badges.R
Last active Nov 9, 2021
The {runcharter} package by John MacKintosh needs more badges
View runcharter-badges.R
install.packages(c("remotes", "tibble", "purrr"))
~label, ~message,
"years", "8",
"kids", "2",
"late nights", "ridiculous",
"sleep missed", "weeks",
"pringles", "way too many",
matt-dray / kanto-points-lines.R
Last active Nov 5, 2021
Use R to locate the coordinates of places of interest on a map of Kanto
View kanto-points-lines.R
# See blogpost about this
# Function: download and plot a PNG file
plot_map <- function(png_path) {
tmp <- tempfile()
download.file(png_path, tmp, quiet = TRUE)
img <- png::readPNG(tmp)
matt-dray / iterate-zip.R
Last active Oct 26, 2021
Iteratively zip up folders of files (no subfolders) with R, where each file is some group (e.g. an organisation's results)
View iterate-zip.R
# Zip up folders programmatically
# Must getwd()/setwd() to prevent full filepaths being saved
# Example: a folder of files (no subfolders) for groups A, B, C
zip_micro <- function(group_code) {
work_dir <- getwd() # project root
out_dir <- file.path(work_dir, "output") # has folders to zip
setwd(out_dir) # switch to location with folders to zip
matt-dray / lubridate-imports.R
Created Oct 10, 2021
Use {itdepends} to count use of {lubridate} functions in CRAN packages that import {lubridate}
View lubridate-imports.R
# Info on CRAN packages
cran <- tools::CRAN_package_db()
# Extract packages that import {lubridate}
pkgs <- cran[grepl("lubridate", cran$Imports), c("Package", "Imports")]
# Install all the packages to non-default location
matt-dray / tidyverse-longest-objects.R
Created Sep 29, 2021
Find the objects with the longest names from the tidyverse suite of packages for R
View tidyverse-longest-objects.R
p_load(char = tidyverse_packages())
pkgs <- paste0("package:", tidyverse_packages())
# [1] "package:broom" "package:cli" "package:crayon"
matt-dray / pokered-objects.R
Last active Oct 10, 2021
Extract objects from locations in the pret/pokered disassembly (investigating base R v4.1)
View pokered-objects.R
files <- list.files(
pattern = ".asm$",
full.names = TRUE
object_names <- gsub(".asm", "", basename(files))
locations <- lapply(files, function(x) readLines(x)) |>
matt-dray / qr2uni.R
Last active Sep 9, 2021
R function that creates a QR code and can convert it to unicode, including emoji, thanks to {qrcode}
View qr2uni.R
# See also Terence Eden
install.packages("qrcode") # if not already installed
qr2uni <- function(string, # string to encode
uni_0 = "\U2B1C", # large white square emoji
uni_1 = "\U2B1B", # large black square emoji
plot = FALSE # plot image of the QR too
) {
matt-dray / colour-continent-by-name.R
Last active Sep 6, 2021
Colour continents based on their start/end letter using {rworldmap}
View colour-continent-by-name.R
# The world's continents coloured by their first and last letter
# Following Cédric Scherer:
# Yes, it is a joke
world_sp <- getMap()
levels(world_sp$REGION) <- list(
matt-dray / squarenicode.R
Last active Aug 19, 2021
Print a little square of multicoloured unicode blocks using R, {purrr} and {multicolor}
View squarenicode.R
# Print a square of multicoloured unicode blocks
# Matt Dray, Aug 2021
# Related posts on
# Install if you haven't already
install.packages("purrr") # for function iteration