Skip to content

Instantly share code, notes, and snippets.


Matt Dray matt-dray

View GitHub Profile
matt-dray / mutate-if-any.R
Created Jan 19, 2022
Pattern for `mutate()` with `if_any()` using {dplyr}
View mutate-if-any.R
# Goal: put 1 in new col where 1 appears across other cols
df <- tibble(
x1 = c(1, NA, NA),
x2 = c(NA, 1, NA),
x3 = c(NA, 1, NA)
matt-dray / link_walk_cycle.R
Created Jan 16, 2022
R function for a Link-sprite walk-cycle (to be run in the terminal)
View link_walk_cycle.R
matt-dray / eldrow.R
Last active Jan 15, 2022
Given a known Wordle answer, backwards-engineer someone's prior guess
View eldrow.R
# I tweeted about this:
# Then I blogged about it:
# remotes::install_github("coolbutuseless/wordle")
eldrow <- function(guess, answer, words = wordle::wordle_dict){
answer <- tolower(answer)
matt-dray / twirdle.R
Last active Jan 18, 2022
Fetch Wordle results from Twitter with {rtweet} and put them in a little dataframe
View twirdle.R
# I tweeted about this:
# Then I blogged about it:
# Function to extract Wordle results from tweet data
twirdle <- function(tweets) {
g <- "\U1F7E9"
o <- "\U1F7E7"
matt-dray / gpx3d-extrude-colour.R
Last active Dec 30, 2021
Create a 3D ggplot with {ggrgl} and colour the extrusion by some variable
View gpx3d-extrude-colour.R
# Create a 3D ggplot with {ggrgl} and colour the extrusion by some variable
# Motivation via @geoff_tewierik:
# See blog about the {gpx3d} package:
# See {gpx3d} package source:
matt-dray / acnh-tinder-analysis.R
Last active Nov 30, 2021
Basic analysis of the 'ACNH Tinder' R Shiny app
View acnh-tinder-analysis.R
# Blogpost:
# App:
x <- readr::read_csv("~/Desktop/acnh-swipe_results - Sheet1.csv")
x %>%
filter(swipe %in% c("left", "right"), name != "TEST") %>%
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