Skip to content

Instantly share code, notes, and snippets.

Avatar
®️

Matt Dray matt-dray

®️
View GitHub Profile
@matt-dray
matt-dray / perlin-dungeon-map-demo.R
Last active May 11, 2022
Quick demo of perlin noise from the {ambient} R package to generate a dungeon map
View perlin-dungeon-map-demo.R
print_perlin_dungeon <- function(
m, # matrix of perlin noise via ambient::noise_perlin()
invert = FALSE # flips tile positions (use set.seed before generating noise)
) {
tile_wall = "#"
tile_floor = "."
# Standardise noise values from 0 to 1
m_bin <- round((m - min(m)) / (max(m) - min(m)))
@matt-dray
matt-dray / edit-plus-code.R
Last active Apr 30, 2022
A basic R function that 'records' changes made to a dataframe with edit() so you can reproduce them
View edit-plus-code.R
# Use R's edit() function and generate code to reproduce it
#
# This is now being developed into the tide() function of the {tide} package:
# https://github.com/matt-dray/tide
#
# Original tweet:
# https://twitter.com/mattdray/status/1518374831993069570?s=20&t=L-xQ74slFk0UtlZ-nIs3Fg
#
# Question from @erdistats re edit + code creation:
# https://twitter.com/erdirstats/status/1518529179892994049
@matt-dray
matt-dray / rename-with-from-lookup.R
Last active Apr 8, 2022
A pattern to use `rename_with()` to rename columns via a lookup of old-column-names to new-column-names
View rename-with-from-lookup.R
library(dplyr, warn.conflicts = FALSE)
x <- mtcars[1:5, 1:5]
x
#> mpg cyl disp hp drat
#> Mazda RX4 21.0 6 160 110 3.90
#> Mazda RX4 Wag 21.0 6 160 110 3.90
#> Datsun 710 22.8 4 108 93 3.85
#> Hornet 4 Drive 21.4 6 258 110 3.08
@matt-dray
matt-dray / web-dissemination-a11ytables.R
Last active Apr 5, 2022
Walkthrough of {a11ytables} given at Web Dissemination Committee, Q1 2022
View web-dissemination-a11ytables.R
# Web Dissemination Committee Q1
# Matt Dray, March 2022
# Packages ----------------------------------------------------------------
# Install {a11ytables} from GitHub
install.packages(remotes) # if not yet installed
remotes::install_github("co-analysis/a11ytables") # v0.0.0.90012 at time of demo
@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") %>%