Skip to content

Instantly share code, notes, and snippets.

Avatar

David Holstius dholstius

  • Bay Area Air Quality Management District
  • San Francisco Bay Area
  • Twitter @dholstius
View GitHub Profile
@dholstius
dholstius / CES_data-examples.R
Last active Aug 29, 2015
Export tract-level CalEnviroScreen 2.0 estimates for PM25 and DieselPM to shapefile
View CES_data-examples.R
# Installation
library(devtools)
install_github("BAAQMD/CalEnviroScreen")
# Helper function: merge.SpatialPolygonsDataFrame()
source_gist("8d1aa85a1623243f8f1f")
# Data from CalEnviroScreen 2 (CES2) package
data(California, package = "CalEnviroScreen")
data(CES2, package = "CalEnviroScreen")
View read_tbl.R
read_tbl <- function (..., stringsAsFactors = FALSE, check.names = FALSE) {
require(dplyr)
as.tbl(read.csv(..., stringsAsFactors = stringsAsFactors, check.names = check.names))
}
write_tbl <- function (x, ..., row.names = FALSE) {
write.csv(as.tbl(x), ..., row.names = row.names)
}
@dholstius
dholstius / .gitignore
Last active Aug 29, 2015 — forked from mbostock/.block
California census tracts
View .gitignore
.DS_Store
build
node_modules
View titlecase.R
titlecase <- function (x) {
# TODO: use Rex package
gsub("\\b([a-z])([a-z]+)", "\\U\\1\\L\\2", tolower(x), perl = TRUE)
}
@dholstius
dholstius / stacked_area_chart.R
Last active Aug 29, 2015
Stacked area chart in ggivs
View stacked_area_chart.R
library(dplyr)
library(ggivs)
chart_data <- structure(list(CalYr = c(1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2010L,
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L), Speed = c(0, 10, 20, 30, 40, 50,
60, 0, 10, 20, 30, 40, 50, 60, 0, 10, 20, 30, 40, 50, 60, 0,
10, 20, 30, 40, 50, 60), VMT = c(323694, 940682, 25396442, 43050277,
71101181, 16987641, 105467584, 222397, 859767, 57905435, 61835130,
@dholstius
dholstius / cached.R
Last active Aug 29, 2015
file-based caching for R
View cached.R
abs_path <- function (...) {
normalizePath(file.path(...), mustWork = FALSE)
}
cache_put <- function (.expr, path, ...) {
obj <- eval(.expr)
dn <- dirname(path)
if (!file.exists(dn)) {
dir.create(dn, recursive = TRUE)
}
View keybase.md

Keybase proof

I hereby claim:

  • I am holstius on github.
  • I am dholstius (https://keybase.io/dholstius) on keybase.
  • I have a public key whose fingerprint is 6974 E4D6 0075 DA92 FE5B CA28 D2E8 C03F DDAB A245

To claim this, I am signing this object:

@dholstius
dholstius / searchable.R
Created Mar 18, 2015
Searchable columns using DT::datatable()
View searchable.R
mtcars %>%
datatable(options = list(columnDefs = list(list(searchable = TRUE, targets = 1:3))))
@dholstius
dholstius / readWKT-by_row.R
Last active Aug 29, 2015
Row-wise application of rgeos::readWKT
View readWKT-by_row.R
library(rgeos)
library(dplyr)
library(magrittr)
n_polygons <- 5
n_vertices_each <- 3
rand_coords <- function (n)
sprintf("%0.1f %0.1f", rnorm(n), rnorm(n))
@dholstius
dholstius / sp-merge-example.R
Last active Aug 29, 2015
Merging polygons without dissolving adjacent boundaries
View sp-merge-example.R
# See http://stackoverflow.com/questions/29310962/variant-of-rgeosgunion-that-wont-dissolve-adjacent-polygons
# for the relevant problem formulation.
library(sp)
unit_rect <- cbind(
x = c(0, 0, 1, 1, 0),
y = c(0, 1, 1, 0, 0))
translate <- function (spobj, delta = c(0, 0)) {