Skip to content

Instantly share code, notes, and snippets.

Avatar
®️

Matt Dray matt-dray

®️
View GitHub Profile
@matt-dray
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
# https://twitter.com/_ColinFay/status/1465253187284254724
countdown <- function(s) {
cat("\014") # clear console
for (i in rev(seq(s))) {
cat("Countdown:", i, "s\n")
Sys.sleep(1)
@matt-dray
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"))
remotes::install_github("matt-dray/badgr")
tibble::tribble(
~label, ~message,
"years", "8",
"kids", "2",
"late nights", "ridiculous",
"sleep missed", "weeks",
"pringles", "way too many",
@matt-dray
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
# https://www.rostrum.blog/2021/11/04/kanto-locator/
# 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
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
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
remotes::install_github("jimhester/itdepends")
# 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
purrr::walk(
@matt-dray
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
library(tidyverse)
library(pacman)
p_load(char = tidyverse_packages())
pkgs <- paste0("package:", tidyverse_packages())
pkgs
# [1] "package:broom" "package:cli" "package:crayon"
@matt-dray
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(
"~/Desktop/pokered-master/data/maps/objects",
pattern = ".asm$",
full.names = TRUE
)
object_names <- gsub(".asm", "", basename(files))
locations <- lapply(files, function(x) readLines(x)) |>
setNames(object_names)
@matt-dray
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
# https://shkspr.mobi/blog/2020/12/a-qr-code-built-from-emoji/
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
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:
# https://www.cedricscherer.com/2021/08/27/the-worlds-countries-colored-by-their-first-letter/
# Yes, it is a joke
library(rworldmap)
world_sp <- getMap()
levels(world_sp$REGION) <- list(
@matt-dray
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 rostrum.blog:
# https://www.rostrum.blog/2021/06/26/emojiscape/
# https://www.rostrum.blog/2021/06/28/pixel-art/
# https://www.rostrum.blog/2021/08/10/dehex/
# Install if you haven't already
install.packages("purrr") # for function iteration