Skip to content

Instantly share code, notes, and snippets.

@dill
dill / fw2dot.R
Created January 27, 2014 20:48
Build a GraphViz compatible file showing which functions call each other in an R package.
# use MVB's foodweb to build a graphviz-compatible "dot" file
# this can then be used to generate a svg of the graph using:
# $ dot -Tsvg file.dot -o file.svg
# example usage:
# fw2dot("mrds","mrds.dot")
library(mvbutils)
fw2dot <- function(package.name,filename){
@dill
dill / columb.R
Created August 5, 2016 03:33
Is Columus, OH the only place in the US where 1/2 the population is within 500 miles? Well, kinda
# is Columbus, OH really the only place in the US where you're within
# 500 miles of half of the US population?
# claim: https://448f59f74df57015bbb8-a9447b7dfa4ae38e337b359963d557c4.ssl.cf3.rackcdn.com/9987%20Brewdog%20EFP%20Prospectus%20USA%20A4%20v9.pdf
# "Columbus is within 500 miles of over half of the US population"
# get the US 2010 census data
# http://www2.census.gov/geo/docs/reference/cenpop2010/county/CenPop2010_Mean_CO.txt
cent <- read.csv("CenPop2010_Mean_CO.txt")
@dill
dill / the_empire_strikes_back.R
Last active August 26, 2016 03:42
End Imperialism! (in terms of units of length, at least) 📏 👍
# try to understand measurements from the past...
# possible input types:
# - 5
# - 5'
# - 5'5
# - 5'7 1/2
# - 5'7\"
# - 5'8.5
# - 5'61/2\"
@dill
dill / example.R
Last active August 27, 2016 17:00
Visualising concurvity between terms in a GAM
# example from ?mgcv::concurvity
library(mgcv)
## simulate data with concurvity...
set.seed(8);n<- 200
f2 <- function(x) 0.2 * x^11 * (10 * (1 - x))^6 + 10 *
(10 * x)^3 * (1 - x)^10
t <- sort(runif(n)) ## first covariate
## make covariate x a smooth function of t + noise...
x <- f2(t) + rnorm(n)*3
@dill
dill / mtcars-cat.R
Created November 27, 2015 15:10
categorical emojis -- like this Mark?
library(emoGG)
library(ggplot2)
# set the am variable to be different emoji
mtcars$am[mtcars$am==1] <- "1f697"
mtcars$am[mtcars$am==0] <- "1f68c"
# use am as the emoji aesthetic
ggplot(mtcars, aes(wt, mpg, emoji=am))+ geom_emoji()
@dill
dill / uncertainty-anim.R
Last active February 23, 2017 16:55
Animations of uncertainty in density maps
# little animation to illustrate uncertainty in a density map
library(dsm)
library(mvtnorm)
library(ggplot2)
library(animation)
library(viridis)
# load the models and prediction data
# using data from the Duke spatial distance sampling course
@dill
dill / gridstuff.R
Last active April 7, 2017 00:59
grids in the right place
# make a boundary
library(mapdata)
library(maptools)
library(rgeos)
library(ggplot2)
library(sp)
# build the coastline
coastline <- map("world", c("USA", "Canada"), plot=FALSE)
@dill
dill / example.R
Created February 20, 2016 20:23
display R plots in iTerm2
library(ggplot2)
library(emoGG)
source("imgcat.R")
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_emoji(emoji="1f337")
imgcat(print(p))
@dill
dill / hi_len.R
Created May 22, 2017 19:02
Uncertainty estimation and plotting in dsm
# for LJT
# this wasn't as simple as I thought, it was the summary() method that does the smart thing
# so there is a bit of split() nonsense to deal with...
library(Distance)
library(dsm)
library(ggplot2)
@dill
dill / watchr.R
Created September 19, 2012 21:21
watch a directory for changes to .Rmd (knitr+markdown files) and if any change, knit them. Best used with Marked by Brett Terpstra (set it to watch the .md files that knitr will generate).
# watch all .Rmd files in a directory, if any change, knit them
library(knitr)
# use watch from Hadley's testthat package
library(testthat)
watchr<-function(path){
# the path supplied here can just be something like "./"