Skip to content

Instantly share code, notes, and snippets.

View MichaelChirico's full-sized avatar

Michael Chirico MichaelChirico

View GitHub Profile
@MichaelChirico
MichaelChirico / rproj
Created April 16, 2020 03:30
open the .Rproj file in a directory, e.g. `rproj ~/path/to/dir`; no arguments = PWD
#!/bin/bash
if [ $# = 0 ]
then
check_dir=.
else
check_dir=$1
fi
rproj_files=$(ls -1 $check_dir/*.Rproj 2> /dev/null)
library(data.table)
library(viridisLite)
NN = 1e6
col = viridis(NN - 1L)
ll = 1
points = data.table(x = numeric(NN), y = numeric(NN))
# one cumsum accumulates the rotational angle, the other cumsum
# accumulates the accumulated angle
points[ , theta := cumsum(cumsum(rep(runif(1L, 0, pi/2), .N)))]
@MichaelChirico
MichaelChirico / ft_nyt_covid_plots.R
Last active July 23, 2020 12:20
Generate Financial Times-like COVID plots for US states by county using NYT county-level data
library(data.table)
library(colourvalues)
library(RColorBrewer)
# palette from: https://www.color-hex.com/color-palette/40131
# raw = c('#eeaf61', '#fb9062', '#ee5d6c', '#ce4993', '#6a0d83')
# bump_col = function(col, n) do.call(rgb, as.list(pmax(pmin((col2rgb(col) + n)/256, 1), 0)))
# dput(sapply(raw, function(col) c(bump_col(col, 16), bump_col(col, -16))))
sunset = c(
"#FDBE71", "#DD9E51",
"#FF9F72", "#EA8052",
@MichaelChirico
MichaelChirico / r_call_tabulation.R
Created January 16, 2020 07:56
R Call frequency
library(data.table)
# get paths to the relevant scripts into this vector
files = list.files('~/gitlab', recursive = TRUE, full.names = TRUE, pattern = '\\.[rR]$')
files = grep('\\.Rcheck|/tests/', f, value = TRUE, invert = TRUE)
# examine an expression; if it's a call, the first element is the function, and
# we continue to recursively check the other arguments for nested calls
extract_calls = function(exp) {
if (is.call(exp))
return(list(as.character(exp[[1L]]), lapply(exp[-1L], extract_calls)))
@MichaelChirico
MichaelChirico / my_locations.R
Last active December 8, 2019 23:47
Turn personal location history into a heat map
library(jsonlite)
library(data.table)
library(sf)
library(geohashTools)
library(colourvalues)
# via https://www.google.com/maps/timeline > cog > Download a copy of your data;
# mine from 5 years came out to 30Mb, roughly 21M lines
loc = read_json('Location History/Location History.json')
# initial flattening; activity is now a list column
@MichaelChirico
MichaelChirico / gist:9e88b9bbfe882bc4d11f2ae105714f8f
Created September 30, 2019 01:38
geohashTools timings vs C++
------ do_timings.sh ------
#!/bin/sh
cd ~/github/geohashTools
git checkout master && R CMD INSTALL . --preclean
Rscript timing_script.R old
git checkout move_to_c && R CMD INSTALL . --preclean
Rscript timing_script.R new
------ timing_scrip.R ------
@MichaelChirico
MichaelChirico / multiplicative_persistence.R
Created September 19, 2019 18:16
Some exploration of Multiplicative Persistence
# do persistence on integers-as-strings to sidestep 64-bit max
pers = function(n) {
sapply(strsplit(trimws(sprintf('%40.0f', n)), NULL), function(s) prod(as.integer(s)))
}
# pers0 = self, then persi = digits product applied i times
DT = data.table(pers0 = 1:1e6, key = 'pers0')
i = 0L
while (TRUE) {
@MichaelChirico
MichaelChirico / function_tree.R
Created August 27, 2019 19:58
Go through a package and create data for a functional dependence tree
PKG = 'pkgdown'
deps = tools::package_dependencies(PKG)[[1L]]
sapply(deps, library, character.only = TRUE)
library(PKG, character.only = TRUE)
pkg_env = asNamespace(PKG)
fun_re = sprintf('(?:%s:::?)?(?:%s|`[^`]+`)(?=\\()',
.standard_regexps()$valid_package_name,
"([[:alpha:]]|[.][._[:alpha:]])[._[:alnum:]]*")
fun_tree = data.table::rbindlist(sapply(ls(pkg_env, all = TRUE), function(fun) {
@MichaelChirico
MichaelChirico / pkgdown_function_tree
Last active August 27, 2019 19:47
Function tree for pkgdown
parent_function,child_function,child_package
[.tag,structure,base
[.tag,NextMethod,base
[.tag,class,base
a,ifelse,base
a,is.na,base
a,paste0,base
add_github_links,paste0,base
add_github_links,gsub,base
add_github_links,is.null,base
@MichaelChirico
MichaelChirico / hex_weights_simulated
Last active July 8, 2019 11:57
Assigning weights per an exponential kernel on a hexagonal space
library(rgeos)
library(magrittr)
library(parallel)
library(colourvalues)
# upper-left coordinates in stretched space
# (it turns out x coordinates are always multiples of 1/2,
# y coordinates are always multiples of sqrt(3)/2)
UL = cbind(rep(c(-7+6*(0:2), -4+6*(0:1)), length.out = 23L),
rep(5:-3, rep(3:2, length.out = 9L)))