Skip to content

Instantly share code, notes, and snippets.

Avatar
®️

Matt Dray matt-dray

®️
View GitHub Profile
@matt-dray
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
library(dplyr)
df <- tibble(
x1 = c(1, NA, NA),
x2 = c(NA, 1, NA),
x3 = c(NA, 1, NA)
)
@matt-dray
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
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:
# https://twitter.com/mattdray/status/1481561924202553346
# Then I blogged about it:
# https://www.rostrum.blog/2022/01/14/wordle/
# remotes::install_github("coolbutuseless/wordle")
eldrow <- function(guess, answer, words = wordle::wordle_dict){
answer <- tolower(answer)
@matt-dray
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:
# https://twitter.com/mattdray/status/1481071849891057668?s=20
# Then I blogged about it:
# https://www.rostrum.blog/2022/01/14/wordle/
# Function to extract Wordle results from tweet data
twirdle <- function(tweets) {
g <- "\U1F7E9"
o <- "\U1F7E7"
@matt-dray
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:
# https://twitter.com/geoff_tewierik/status/1476662279437369346?s=20
# See blog about the {gpx3d} package:
# https://www.rostrum.blog/2021/12/30/gpx3d/
# See {gpx3d} package source:
# https://github.com/matt-dray/gpx3d
library(gpx3d)
@matt-dray
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:
# https://www.rostrum.blog/2020/06/06/acnh-swipe/
#
# App:
# https://mattdray.shinyapps.io/acnh-swipe/
x <- readr::read_csv("~/Desktop/acnh-swipe_results - Sheet1.csv")
x %>%
filter(swipe %in% c("left", "right"), name != "TEST") %>%
@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