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 / tooltip-custom-text-starwars.R
Last active Apr 28, 2022
Create custom tooltip text for ggplotly by using a 'text' argument in 'geom_*(aes())'
View tooltip-custom-text-starwars.R
library(dplyr)
library(ggplot2)
library(plotly)
library(viridis)
data("starwars")
p <- ggplot(starwars) +
geom_point(
aes( # all 4 aes() arguments will print in the plotly tooltip
x = height, # arg 1
@matt-dray
matt-dray / badgr-shiny.R
Last active Apr 13, 2022
Make a shields.io README badge that links to a Shiny app, using the {badgr} package in R
View badgr-shiny.R
# Make a README badge for your R Shiny repo
# Matt Dray, March 2021
# {badgr} blog post: https://www.rostrum.blog/2020/05/08/readme-badge/
# Shiny badge blog post: https://www.rostrum.blog/2021/03/23/shiny-badge/
# {badgr} is available from GitHub via {remotes}
install.packages("remotes") # if not already installed
remotes::install_github("matt-dray/badgr") # install {badgr}
@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 / 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 / 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)