Skip to content

Instantly share code, notes, and snippets.

View coolbutuseless's full-sized avatar

mikefc coolbutuseless

View GitHub Profile
@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 / 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.
@coolbutuseless
coolbutuseless / is_within.R
Created September 20, 2018 20:20
Stricter membership testing in #rstats
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' A strict version of '%in%' where both the in-group and out-group must be completely specified
#'
#' The membership test is strict.
#' - if 'universe' is defined, then `outgroup = setdiff(universe, ingroup)`
#' - Every value of 'x' must exist within either 'ingroup' or 'outgroup'
#' - 'ingroup' and 'outgroup' must be disjoint sets
#' - May specify only one of 'outgroup' or 'universe'
#'
@coolbutuseless
coolbutuseless / memoise-with-size-limit.R
Created September 24, 2018 11:24
memoise in rstats with a limit on how large an object can be in the cache
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' A version of 'memoise::memoise' with limits on individual object size
#'
#' @param f Function of which to create a memoised copy.
#' @param ... optional variables specified as formulas with no RHS to use as
#' additional restrictions on caching. See Examples for usage.
#' @param envir Environment of the returned function.
#' @param cache Cache function.
#' @param object_size_limit maximum size of objects stored in cache.
#' Default: 1048576 bytes (1MB)