Skip to content

Instantly share code, notes, and snippets.

View coolbutuseless's full-sized avatar

mikefc coolbutuseless

View GitHub Profile
#!/usr/bin/env julia
# straight translation of some python code.
# python code (using pypy) takes less than 1 second/iteration (core i7 imac)
# After removing globals and typing code, julia code takes same time as pypy code.
# Example pairs.csv file: https://dl.dropboxusercontent.com/u/68676/pairs.zip
function cdf(x::Float64)
s = x
t = 0.0
@coolbutuseless
coolbutuseless / one-bit-boolean.R
Created January 20, 2018 01:05
Use the 'bit' package to do boolean vector (with NA) with 2-bits per value.
library(bit)
library(R6)
#-----------------------------------------------------------------------------
# Outline of an R6 class to wrap a logical vector that may take values: TRUE, FALSE or NA
#
# This uses the 'bit' package for 2-bit booleans (1 bit for t/f and 1bit for NA status)
#
# This uses less memory than a normal logical vector (which uses 32 bits for
@coolbutuseless
coolbutuseless / morse.R
Created July 20, 2018 10:01
morse code tweet
#rstats #morse #sos
library(purrr);
s=strsplit;
flatten(map(s('sos','')[[1]],~set_names(s(s('._ _... _._. _.. . .._. __. .... .. .___ _._ ._.. __ _. ___ .__. __._ ._. ... _ .._ ..._ .__ _.._ _.__ __..',' ')[[1]],''),letters)[[.x]]))%>%walk(~{beepr::beep((.x=='_')+1);Sys.sleep(.5)})
@coolbutuseless
coolbutuseless / capture.R
Created July 26, 2018 07:57
capturing expressions and evaluating later
library(tidyverse)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Base R styel
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
filter_base <- function(df, boolean_expression) {
# Don't evaluate what we were given. Just hold it.
captured_expression <- substitute(boolean_expression)
@coolbutuseless
coolbutuseless / unpipe.R
Created July 26, 2018 13:45
tweetable unpipe
#rstats #unpipe
library(rlang)
U=function(ee){A=call_args;N=call_name;C=call2;I=is_call
if(!I(ee)){return(ee)}
f=N(ee)
u=purrr::map(A(ee),U)
if(f=="%>%"){l=u[[1]]
r=u[[2]]
if(I(r)){C(N(r),!!!c(l,A(r)))}else{C(r,l)}}else{C(f,!!!u)}}
@coolbutuseless
coolbutuseless / braille.R
Last active July 31, 2018 10:58
brailler plotter in a tweet
#rstats
library(tidyverse)
d=c(1,5,3,11,9,7,15,13,6,14)
d=c(d,d+16,d+48,d+32)
L=setNames(c(1:22,40,23:25),letters)
D=map(d[L[strsplit('braille','')[[1]]]],~intToBits(.x)[1:6]>0)
P=data.frame(D=unlist(D),x=0:1+rep(seq(D),e=6)*3,y=rep(2:0,e=2))
ggplot(P)+geom_point(aes(x,y,size=D))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ggplot2: Bug or Feature?
#
# Problem: stat_summary is calculated after axes are transformed.
#
# Expected:
# Median at x=0 is 0, and median at x=1 is 1
# Expect line from (0, 0) to (1, 1)
#
# Actual
@coolbutuseless
coolbutuseless / gganimate-sprites.R
Created August 13, 2018 13:07
gganimate sprites
library(tidyverse)
library(raster)
library(gganimate)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Not going to directly link in to script to avoid hitting server
# https://www.spriters-resource.com/resources/sheets/12/12593.png
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sprite_sheet <- png::readPNG("12593.png")
@coolbutuseless
coolbutuseless / brisbane-standard-drawings.R
Created September 13, 2018 07:57
Download all the Brisbane City Council standard drawings
library(dplyr)
library(curl)
library(rvest)
library(xml2)
library(purrr)
# This script will download all the Brisbane City Council standard drawings.
bsd_url <- 'https://www.brisbane.qld.gov.au/planning-building/planning-guidelines-tools/planning-guidelines/standard-drawings'
@coolbutuseless
coolbutuseless / strict_case_when.R
Created September 20, 2018 08:22
Stricter version of dplyr::case_when()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Stricter version of case_when()
#' - disallows a fall-through 'TRUE' value on the LHS.
#' - disallows input values which do not match any rules.
#' - disallows input values which match more than one rule
#'
#' @param ... arguments to case_when
#'
#' @return A vector of length 1 or n, matching the length of the logical input
#' or output vectors, with the type (and attributes) of the first RHS.