Skip to content

Instantly share code, notes, and snippets.

View matt-dray's full-sized avatar
®️

Matt Dray matt-dray

®️
View GitHub Profile
@matt-dray
matt-dray / simple_cols_magick.R
Last active November 20, 2018 20:44
Take an image and simplify it with a pre-specified set of colours using magick::image_map()
# Simplify image colours to prespecified simple colour set
# Following Jeroen Ooms (https://jeroen.github.io/munster2018/)
library(magick)
library(dplyr)
library(tibble)
# read test image
# img_path <- "https://upload.wikimedia.org/wikipedia/commons/1/13/Suprematist_Composition_-_Kazimir_Malevich.jpg"
@matt-dray
matt-dray / shiny-checkbox-null.R
Created November 28, 2018 17:17
Shiny app with checkboxes but if all are unchecked there is no error
# Shiny app with checkboxes
# If unchecked, no error
# https://stackoverflow.com/a/38686474
# https://stackoverflow.com/a/37737182
library(shiny)
library(ggplot2)
library(dplyr)
ui <- shinyUI(fluidPage(
@matt-dray
matt-dray / virtual-env.sh
Created December 4, 2018 10:55
Set up a virtual environment for Python
# Create a virtual environment at current pwd
virtualenv venv
# Activate the virtual environment
source venv/bin/activate
# Load in module/version requirements
pip install -r requirements.txt
# Deactivate the running virtual environment
@matt-dray
matt-dray / git-snippets.sh
Last active December 11, 2018 09:26
Git snippets I want to remember
# Branch activities
git branch # current branch
git branch -a # list all branches
git branch -av # list branches with last commit
git checkout branch-name # switch to branch
# Git stashing
# For storing changes when you switch branch and come back later
# https://git-scm.com/book/en/v1/Git-Tools-Stashing
git stash push
@matt-dray
matt-dray / get-nba-table-wiki.R
Created December 18, 2018 21:41
Get the table of teams and locations from the NBA Wikipedia page
library(rvest)
library(tibble)
nba_wikipedia <- read_html("https://en.wikipedia.org/wiki/National_Basketball_Association")
nba_table <- html_nodes(
x = nba_wikipedia,
xpath = '//*[@id="mw-content-text"]/div/table[4]'
) %>%
html_table(fill = TRUE, header = NA)
@matt-dray
matt-dray / 190103-rap-companion-accessibility-tenon-report.csv
Created January 3, 2019 09:21
RAP companion accessibility report from tenon.io
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 2.
"tID","bpID","priority","certainty","errorTitle","standards","resultTitle","errorDescription","viewPortLocation","position","errorSnippet","xpath","issueID","signature","ref","url"
3,118,100,100,"The language of this page is not set.","Web Content Accessibility Guidelines (WCAG) 2.0, Level A: 3.1.1 Language of Page","Identify the natural language for the page","The language of the document has not been set. When the language is not set, screen readers and other assistive technologies have no way of being sure of the correct way to accessibily present the contents.","{""top-left"":{""x"":0,""y"":0},""bottom-right"":{""x"":1024,""y"":768},""height"":768,""width"":1024}","column: 1 , line: 1","<html><head style="""">\n\n <meta charset=""UTF-8"" style="""">\n <meta http-equiv=""X-UA-Compatible"" content=""IE=edge"" style="""">\n <title style="""">RAP Companion</title>\n <meta name=""description"" content=""A technical commu","/html","e14ae3f19fc00ece6488db9c9b459586","3cc1161591cd0a1f606898e98ddeb0d6","https:
@matt-dray
matt-dray / alluvial_demo.R
Last active January 4, 2019 17:06
Testing the alluvial function for creating Sankey-like static plots
# demo of alluvial::alluvial()
# see the vignette: https://cran.r-project.org/web/packages/alluvial/vignettes/alluvial.html
# matt dray
# july 2018
# packages
library(dplyr)
library(readr)
library(alluvial)
@matt-dray
matt-dray / save-many-plots-pwalk.R
Created February 25, 2019 14:26
Create a nested dataframe, with a column for the data in a tibble object; a plot object created from the data tibble; and a filename
# https://rstudio-pubs-static.s3.amazonaws.com/294422_099df24a1c0f46f99db848ca5c48ff6b.html
library(tidyverse)
plots <- mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(plot = map(data, ~ggplot(., aes(mpg, wt)) + geom_point()),
filename = paste0(cyl, ".pdf")) %>%
select(filename, plot)
@matt-dray
matt-dray / union-polygons-sf.R
Created February 26, 2019 09:26
Example of a geospatial union between polygons with R's {sf} package
# Example of a geospatial union between polygons with R's {sf} package
# Combine East and West Midlands to 'Midlands'
# This example uses a GeoJSON of super-generalised clipped boundaries for England's regions:
# http://geoportal.statistics.gov.uk/datasets/regions-december-2017-super-generalised-clipped-boundaries-in-england
# (click the API dropdown on this page and copy the URL for the GeoJSON)
# Packages
library(geojsonio)
library(sf)
@matt-dray
matt-dray / all-the-memes.R
Created April 16, 2019 08:47
Save all the base meme images from the {memer} package
devtools::install_github("sctyner/memer")
library(memer)
library(tidyverse)
library(magick)
memes <- tibble(meme_name = meme_list()) %>%
mutate(
image = map(meme_name, meme_get),
path = paste0(getwd(), "/", meme_name, ".jpg")
) %>%